Home > Net > Help!!!!! On the c # serial communication trouble stuck close a serial port
Help!!!!! On the c # serial communication trouble stuck close a serial port
Time:11-25
Recently, as the topic, do a system scan qr code, lower camera qr code sent to the PC, images, and then identify image is to be able to show and identify normal now, but encountered a problem, made the first qr code to shoot the second qr code, images are not displayed correctly,
Going to create a new button events at the beginning, the array ReceivedData for receiving data [I] is zero, but because the array is a local variable in a serial port function, so the operation failure, Then after only identifies an image close the serial port, and then open and close a serial port, however had stuck status, access to the Internet to find relevant information, to Invoke changed the BeginInvoke, although change after not stuck but error, according to "port is closed," serial part of the program should be how to change to make, please close the serial port is not wrong?
Attach a program:
private void sp_DataReceived (object sender, SerialDataReceivedEventArgs e) { System. Threading. Thread. Sleep (9000);//delay 9000 ms after waiting for receiving data //this. Invoke is across threads access UI method and is an example of this article The BeginInvoke (new EventHandler (delegate { Byte [] ReceivedData=https://bbs.csdn.net/topics/new Byte [sp. BytesToRead];//create the receiving an array of bytes Sp. Read (ReceivedData, 0, ReceivedData. Length);//read the received data String RecvDataText=null; If (false==isHex) { for (int i=0; I & lt; ReceivedData. Length; I++) { RecvDataText +=ReceivedData [I]; } //byte type into type string RecvDataText=System. Text. Encoding. The Default. Get string (ReceivedData); TbxRecvData. Text +=RecvDataText;//update the data in the receiving box TbxRecvLength. Text=tbxRecvData. TextLength. ToString ();//update the receiving box } The else { for (int i=0; I & lt; ReceivedData. Length; I++) {
BarCodeImg. Image=OvImage; TbxRecvData. Text +=RecvDataText;//update the data in the receiving box TbxRecvLength. Text=(tbxRecvData. TextLength/3). The ToString ();//update the data in the receiving box length
}
Sp. DiscardInBuffer ();//discarding the receive buffer data
})); }
CodePudding user response:
There are a few points need to optimize 1. Sp_DataReceived is event trigger, so don't use sleep (9000) way to collect data, such as 2. Data reception and data parsing the best separation, Otherwise may timeout due to parse long timescales, Is the business process affected the serial port communication,
CodePudding user response:
Don't in method DataReceived event handling, only receives the data, then shut down, before you can cancel the registered to receive events, the release of the input and output buffer, then shut down
CodePudding user response:
Don't go in the BeginInvoke serial port to receive the data read, In the event of an sp_DataReceived read a serial port receives the data, Byte [] ReceivedData=https://bbs.csdn.net/topics/new Byte [sp. BytesToRead]; ReceivedData is then read data from another thread processing,
The BeginInvoke entrust the UI thread, but not executed immediately, At that moment, you close the serial port, and then wait to perform, port has been closed early, so an error,
CodePudding user response:
Upstairs said is right, you want to synchronize to receive asynchronous resolution,
CodePudding user response:
A serial port receives can write a class includes receiving data parsing, and then passed out in the form of commissioned don't need sleep this way