C # serialPort. close () deadlock in the serial port receiving data,

Source: Internet
Author: User

C # serialPort. close () deadlock in the serial port receiving data,

Recently, I am working on a client program for the high-speed rail analog warehouse display system. In this program, I need to use the serial serialPort to transmit data, because the UI interface should be updated after each data reception, therefore, the Invoke is used to encapsulate the updated UI program code into a method, and then call the code through Incoke. The program runs normally, but when you execute serialPort. close () is a deadlock in the program, and the whole program cannot be moved. I checked a lot of information online. There are various such sayings. Some may define a mark for receiving data. If the program is executed to close the program, the serial port will be closed if the data is received, if not, continue the execution. However, after the test, there is no egg. Finally, When I study invoke myself, I find that there is Begininvoke and the differences between them, begininvoke is used to update UI data in the background without waiting, while invoke is used to update UI data in the background without waiting. Only after understanding the differences between the two can we understand the original execution of serialPort. the cause of the deadlock in close () Is that invoke is working. If it is changed to begininvoke, there will be no problem of thinking. Directly run the Code:

 

SerialPort serialPort1 = new SerialPort (configComString, 115200, Parity. None, 8, StopBits. One); // initialize the serial port settings
// Define the delegate
Public delegate void Displaydelegate (byte [] InputBuf );
Byte [] OutputBuf = new Byte [8];
Public Displaydelegate disp_delegate;

// Receives data Delegation
Disp_delegate = new Displaydelegate (DispUI );
SerialPort1.DataReceived + = new SerialDataReceivedEventHandler (Comm_DataReceived );

// Read data processing functions through the serial port
Public void Comm_DataReceived (object sender, SerialDataReceivedEventArgs e)
{

Byte [] InputBuf = new Byte [8];

Try
{
SerialPort1.Read (InputBuf, 0, serialPort1.BytesToRead); // read the data in the buffer until "}", that is, 0x7D is the end character.
System. Threading. Thread. Sleep (100 );
This. BeginInvoke (disp_delegate, InputBuf); // disp_delegate is a defined delegate event that calls the program for modifying the UI in the delegate event.
}
Catch (TimeoutException ex) // timeout Processing
{
MessageBox. Show (ex. ToString ());
}

}

// Update the UI
Public void DispUI (byte [] InputBuf)
{

String str = System. Text. Encoding. Default. GetString (InputBuf );
// Console. WriteLine (str );
String strW = str. Substring (0, 2); // truncate the Substring of str, starting from index = 0 to truncate a string of 2
Int OutStrW = int. Parse (strW );
String strS = str. Substring (2, 2); // truncate the Substring of str, which starts from index = 2.
Int OutStrS = int. Parse (strS );
OutstrWen = (OutStrW-4). ToString ();
TextBox8.Text = strW;
TextBox9.Text = (OutStrW-4). ToString ();
TextBox10.Text = strS;
TextBox11.Text = (OutStrS-10). ToString ();
}

 

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.