In the C # serial communication development process, some home only provide communication protocol, this time developers to their own according to the protocol to encapsulate the method, some of the home more humane to provide a packaged communication protocol method for developers to call.
1. Provide only communication protocols (for example, a note-issuing machine developed earlier this year):
Such as:
<summary>
Banknote Dispense (0x45)
</summary>
public void Chuchao ()
{
Log ("Note-issuing equipment");
if (!SP. IsOpen)
{
Sp. Open ();
}
Try
{
var send = new byte[] {Eot, Id, Stx, Dipsense, Etx, 0x01};
int bcc = 0;
for (int i = 0; i < send. Length-1; i++)
Bcc ^= send[i];
Send[send. Length-1] = (byte) bcc;
Sp. Write (send, 0, send. Length);
Log ("Operation command:" + getbytesstring (send, 0, send.) Length, ""));
for (int i = 0; i < 3; i++)
{
var r = Sp. ReadByte ();
Log ("Send command, receive feedback. "+ R);
if (r! = Ack)
{
Sp. Write (send, 0, send. Length);
}
Else
{
Break
}
}
byte[] recive = new BYTE[14];
for (int i = 0; i < 3; i++)
{
bcc = 0;
recive = GetData (recive. Length);
for (int j = 0; J < recive. Length-1; J + +)
{
Bcc ^= recive[j];
}
if (bcc! = recive[recive. LENGTH-1])
{
Sp. Write (new byte[] {Nck}, 0, 1);
Log ("received message:" + getbytesstring (recive, 0, recive. Length, "") + ", Receive identity: NCK: Failed");
}
Else
{
Sp. Write (new byte[] {Ack}, 0, 1);
Log ("received message:" + getbytesstring (recive, 0, recive. Length, "") + ", Receive identity: ACK: Success");
Break
}
}
Error ErrorCode = new error ();
Errorcode.code = recive[9];
Log ("received command:" + recive[3] + ", received error code:" + Errorcode.code + "--" + errorcode.errormsg);
if (recive[3]! = Dipsense | | errorcode.code > 0X31)
{
throw new Exception ("Error in issuing money");
}
}
catch (Exception e)
{
Log (e.tostring ());
Throw
}
Finally
{
if (sp. IsOpen)
{
Sp. Close ();
}
}
}
2, the production home provides communication protocol method (for example, the bank card payment machine developed earlier this year):
At this point you just add the DLL provided by the bin/debug below your project, and then, again:
/// <summary> //Open the serial port//</summary>// <param name= "Port" >String Number Strings</param> //<returns>//serial port file handle/// Note: You must call this function first to obtain the serial port file handle for the specified serial port before calling other functions. /// can open more than one serial port at the same time, get multiple serial port file handle, but cannot open the same serial port multiple times. /// after the use is complete, you must call Commclose () to close the serial port. // </returns>[DllImport("Crt_310.dll", EntryPoint ="Commopen", SetLastError =true, CharSet =CharSet. Ansi, exactspelling =true, CallingConvention =callingconvention. stdcall)]Public static externIntPtrCommopen (stringPort);/// <summary> //opens the serial port at the specified baud rate (function done by the function = Commopen function + commsetting function)//</summary>// <param name= "Port" >String Number Strings</param>//<param name= "Data" >Specify baud rate/// baud rate =1200,2400,4800,9600,19200,38400. /// For example: Commopen ("Com1", 9600); //</param>// <returns>serial port file handle/// Note: You must call this function first to obtain the serial port file handle for the specified serial port before calling other functions. /// can open more than one serial port at the same time, get multiple serial port file handle, but cannot open the same serial port multiple times. /// after the use is complete, you must call Commclose () to close the serial port. </returns>[DllImport("Crt_310.dll", EntryPoint ="Commopenwithbaut", SetLastError =true, CharSet =CharSet. Ansi, exactspelling =true, CallingConvention =callingconvention. stdcall)]Public static externIntPtrCommopenwithbaut (stringPortUINTdata);
Then you can call it like this:
Public StringCardboxpositiontoread () {varComhandle =NewIntPtr();varCardstates =New byte[2];varRecordInfo =New byte[200];int? DataTry{Comhandle =Packagek100dll. M100a_commopenwithbaud (Comport, baudrate);if(Comhandle.toint32 () = = 0) {Packagek100dll. M100a_commclose (Comhandle);return"failed to open the serial port!" "; } data =Packagek100dll. M100a_checkcardposition (Comhandle,false, 0, Cardstates, recordinfo);if(Data! = 0) {Packagek100dll. M100a_commclose (Comhandle);return"failed to read card position"; }Switch(Cardstates[0]) {//channel without card Case48: Break; Case49: Case50:data =Packagek100dll. M100a_movecard (Comhandle,false, 0, 0x34, recordinfo);if(Data! = 0) {Packagek100dll. M100a_commclose (Comhandle);return"Move Card location failed"; } Break;//channel with carddefault:Packagek100dll. M100a_commclose (Comhandle);return"Please take the card or the business is in progress, please wait!" "; }Switch(Cardstates[1]) { Case48:Packagek100dll. M100a_commclose (Comhandle);return"card box without card";default://Move the card slot card to the read/write card locationdata =Packagek100dll. M100a_movecard (Comhandle,false, 0, 0x30, recordinfo);if(Data! = 0) {Packagek100dll. M100a_commclose (Comhandle);return"Move Card location failed"; }Packagek100dll. M100a_commclose (Comhandle);return"true"; } }Catch(Exception) {Packagek100dll. M100a_commclose (Comhandle);return"Exception occurred"; } }
C # Serial Communication summary