Work industry communications have been around for a long time, especially serial port (232/485) and serial port operations for various versions of VB/VC/CCodeThese codes have also been tested on site for many years. It should be said that they are relatively robust code, but currently they do not have C # relatively mature serial port operation code, recently, the serial port operation code based on wince5.0 was developed using moxa devices, so I expanded and improved the serial port operations, especially the sendcommand function. This is a commonly used master-slave communication code, if you do not like to use events or threads to access data, you can directly cyclically determine the data to be received within the specified timeout period.
The specific code is as follows:
Public Class Portdata
{
Public Event Portdatareceivedeventhandle received;
Public Event Serialerrorreceivedeventhandler error;
Public SerialPort port;
Public Bool Receiveeventflag = False ; // Whether the received event is valid. False indicates that the event is valid.
Public Portdata ( String Sportname, Int Baudrate, parity, serialinterface. serialmode)
{
Port = New SerialPort (sportname, baudrate, parity, 8 , Stopbits. One );
Port. rtsenable = True ;
Port. readtimeout = 3000 ;
Port. datareceived + = New Serialdatareceivedeventhandler (datareceived );
Port. errorreceived + = New Serialerrorreceivedeventhandler (errorevent );
}
~ Portdata ()
{
Close ();
}
Public Void Open ()
{
If ( ! Port. isopen)
{
Port. open ();
}
}
Public Void Close ()
{
If (Port. isopen)
{
Port. Close ();
}
}
// Data Transmission
Public Void Senddata ( Byte [] Data)
{
If (Port. isopen)
{
Port. Write (data, 0 , Data. Length );
}
}
Public Void Senddata ( Byte [] Data, Int Offset, Int Count)
{
If (Port. isopen)
{
Port. Write (data, offset, count );
}
}
// Send command
Public Int Sendcommand ( Byte [] Senddata, Ref Byte [] Receivedata, Int Overtime)
{
If (Port. isopen)
{
Receiveeventflag = True ; // Disable event reception
Port. discardinbuffer (); // Clear the receiving buffer
Port. Write (senddata, 0 , Senddata. Length );
Int Num = 0 , RET = 0 ;
While (Num ++ < Overtime)
{
If (Port. bytestoread > = Receivedata. length) Break ;
System. Threading. thread. Sleep ( 1 );
}
If (Port. bytestoread > = Receivedata. length)
RET = Port. Read (receivedata, 0 , Receivedata. Length );
Receiveeventflag = False ; // Open event
Return RET;
}
Return - 1 ;
}
Public Void Errorevent ( Object Sender, serialerrorreceivedeventargs E)
{
If (Error ! = Null ) Error (sender, e );
}
// Receive data
Public Void Datareceived ( Object Sender, serialdatareceivedeventargs E)
{
// Disable direct exit when receiving events
If (Receiveeventflag) Return ;
Byte [] Data = New Byte [Port. bytestoread];
Port. Read (data, 0 , Data. Length );
If (Received ! = Null ) Received (sender, New Portdatareciveeventargs (data ));
}
Public Bool Isopen ()
{
Return Port. isopen;
}
}
Public Delegate Void Portdatareceivedeventhandle ( Object Sender, portdatareciveeventargs E );
Public Class Portdatareciveeventargs: eventargs
{
Public Portdatareciveeventargs ()
{
This . Data = Null ;
}
PublicPortdatareciveeventargs (Byte[] Data)
{
This. Data=Data;
}
Private Byte[] Data;
Public Byte[] Data
{
Get{ReturnData ;}
Set{Data=Value ;}
}
}
[note] 1 ~ 9. The serial port name is "comx:",> 9 \\\\. \ comx: it is easy to use, but it cannot be used on the moxa 661 device. The format is "$ device \ com" + portno. tostring () + "\ 0", maybe this is the corresponding serial port driver modified by moxa.