This article from: http://www.cnblogs.com/showlie/articles/751737.html
The reading and writing of serial data in SerialPort are quite different. Since the serial port does not know when data will arrive, there are two methods to read data from the serial port. I. Real-time reading of serial ports by threads; II. event triggering.
Because the real-time reading of serial ports by threads is not very efficient, it is better to trigger events. There is a datareceived event in the SerialPort class. When data in the read cache of the serial port arrives, the datareceived event is triggered, in which the SerialPort. the receivedbytesthreshold attribute determines the number of data items in the serial port read cache before the datareceived event is triggered. The default value is 1.
In addition, the SerialPort. datareceived event runs in a special way. It runs in a secondary thread and cannot directly transmit data with the display data control in the main thread. It must be implemented indirectly. As follows:
SerialPort spsend; // spsend. spreceive is connected through a virtual serial port, and data can be transmitted between them. Spsend sends data
SerialPort spreceive; // spreceive receives data
Textbox txtsend; // sending area
Textbox txtreceive; // receiving area
Button btnsend; // data sending button
Delegate void updatetexteventhandler (string text); // delegate, which is the focus
Updatetexteventhandler updatetext;
Public void initclient () // The form control has been initialized.
{
Updatetext = new updatetexteventhandler (updatetextbox); // instantiate the delegate object
Spsend. open (); // the SerialPort object is inProgramIt must be disabled before the end.
Spreceive. datareceived + = new ports. serialdatareceivedeventhandler (spreceive_datareceived );
Spreceive. open ();
}
Public void btnsend_click (Object sender, eventargs E)
{
Spsend. writeline (txtsend. Text );
}
Public void spreceive_datareceived (Object sender, ports. serialdatareceivedeventargs E)
{
// Byte [] readbuffer = new byte [spreceive. readbuffersize];
// Spreceive. Read (readbuffer, 0, readbuffer. Length );
// This. Invoke (updatetext, new string [] {encoding. Unicode. getstring (readbuffer )});
String readstring = spreceive. readexisting ();
This. Invoke (updatetext, new string [] {readstring });
}
Private void updatetextbox (string text)
{
Txtreceive. Text = text;
}
The reading and writing of serial data in SerialPort are quite different. Since the serial port does not know when data will arrive, there are two methods to read data from the serial port. I. Real-time reading of serial ports by threads; II. event triggering.
Because the real-time reading of serial ports by threads is not very efficient, it is better to trigger events. There is a datareceived event in the SerialPort class. When data in the read cache of the serial port arrives, the datareceived event is triggered, in which the SerialPort. the receivedbytesthreshold attribute determines the number of data items in the serial port read cache before the datareceived event is triggered. The default value is 1.
In addition, the SerialPort. datareceived event runs in a special way. It runs in a secondary thread and cannot directly transmit data with the display data control in the main thread. It must be implemented indirectly. As follows:
SerialPort spsend; // spsend. spreceive is connected through a virtual serial port, and data can be transmitted between them. Spsend sends data
SerialPort spreceive; // spreceive receives data
Textbox txtsend; // sending area
Textbox txtreceive; // receiving area
Button btnsend; // data sending button
Delegate void updatetexteventhandler (string text); // delegate, which is the focus
Updatetexteventhandler updatetext;
Public void initclient () // The form control has been initialized.
{
Updatetext = new updatetexteventhandler (updatetextbox); // instantiate the delegate object
Spsend. open (); // the SerialPort object must be closed before the end of the program.
Spreceive. datareceived + = new ports. serialdatareceivedeventhandler (spreceive_datareceived );
Spreceive. open ();
}
Public void btnsend_click (Object sender, eventargs E)
{
Spsend. writeline (txtsend. Text );
}
Public void spreceive_datareceived (Object sender, ports. serialdatareceivedeventargs E)
{
// Byte [] readbuffer = new byte [spreceive. readbuffersize];
// Spreceive. Read (readbuffer, 0, readbuffer. Length );
// This. Invoke (updatetext, new string [] {encoding. Unicode. getstring (readbuffer )});
String readstring = spreceive. readexisting ();
This. Invoke (updatetext, new string [] {readstring });
}
Private void updatetextbox (string text)
{
Txtreceive. Text = text;
}