C # serial port receive data in Serialport.close () deadlock

Source: Internet
Author: User

Tag: message ODI ISP program code data tput Get box time

Recently in a high-speed rail simulator display system of the client program, in this program to use the serial port SerialPort transmission data, because each time the receiving data to update the UI interface, so the use of the Invoke, the update UI program code into a method, And then through the Incoke call, the program runs without any problems, but when you execute Serialport.close () is the program will have a deadlock, the whole program card is not moving. On the internet to check a lot of information, there are various such statements, and some say that the definition of a sign to receive data, if the execution of the shutdown program is to judge, if the data received after the closure of the serial port, no words continue to execute, but after personal testing and no egg use, In the end, when I studied invoke, I found that there were BeginInvoke, but also found that the difference between them, BeginInvoke for the background to update the UI data without waiting for the situation, and invoke for the background update UI data no need to wait for the situation, Figure out the difference between the two before you understand the original execution serialport.close () The cause of the deadlock is invoke in the trouble, change into BeginInvoke will not appear thinking problems. Directly on the code:

SerialPort serialPort1 = new SerialPort (configcomstring, 115200, Parity.none, 8, Stopbits.one); Initialize the serial port settings
Defining delegates
public delegate void DisplayDelegate (byte[] inputbuf);
byte[] Outputbuf = new Byte[8];
Public DisplayDelegate disp_delegate;

Receive data delegates
Disp_delegate = new DisplayDelegate (DISPUI);
serialport1.datareceived + = new Serialdatareceivedeventhandler (comm_datareceived);

Serial port reading data processing function
public void Comm_datareceived (object sender, Serialdatareceivedeventargs e)
{

byte[] Inputbuf = new Byte[8];

Try
{
Serialport1.read (inputbuf, 0, Serialport1.bytestoread); Reads the buffer's data until "}" i.e. 0x7d is the Terminator
System.Threading.Thread.Sleep (100);
This. BeginInvoke (Disp_delegate, inputbuf);//disp_delegate is a defined delegate event that invokes a program that modifies the UI in a delegate event
}
catch (TimeoutException ex)//timeout processing
{
MessageBox.Show (ex. ToString ());
}

}

Updating the UI interface
public void Dispui (byte[] inputbuf)
{

String str = System.Text.Encoding.Default.GetString (INPUTBUF);
Console.WriteLine (str);
string strw = str. Substring (0, 2);//intercept a substring of STR, starting from index=0 to intercept a string of length 2
int outstrw = Int. Parse (STRW);
String STRs = str. Substring (2, 2);//intercept a substring of STR, starting from index=2 to intercept a string of length 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 ();
}

C # serial port receive data in Serialport.close () deadlock

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.