In C #2.0, how does SerialPort read serial port data and display it on textbox?

Source: Internet
Author: User

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.