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++)
{

//byte image part program


RecvDataText +=(ReceivedData [I]. ToString (" X2 ") + "");

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

CodePudding user response:

reference 1st floor xian_wwq response:

there are a few points need optimization1. 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,
ok??

CodePudding user response:

reference likelinsiyuan reply: 3/f
don't to read a serial port receives the data in the BeginInvoke,
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,
ok I try to change

CodePudding user response:

Sleep code written in the program of events/asynchronous communication itself is wrong, don't have to consider other temporarily, the communication mechanism of the basic first clear, Sleep first, delete the data from receiving, accept the logic to deal with sticky package subcontract, this is at least programming techniques, how do you know how to block 9000 rather than 900 nor 8000 and not 100 or 9900? This card dead 9 seconds dynamic do completely is a guess, and no disease is to be stuck,

CodePudding user response:

If you see some blogs, if events/asynchronous communication program inside wrote a Sleep, even 10 milliseconds, throw away such junk code directly, otherwise learned harmful,

CodePudding user response:

reference likelinsiyuan reply: 3/f
don't to read a serial port receives the data in the BeginInvoke,


Mostly because lz started writing Sleep (9000), and then run counter to themselves,

CodePudding user response:

references 9/f, interacting with professional developers response:
Sleep code written in the program of events/asynchronous communication itself is wrong, don't have to consider other temporarily, the communication mechanism of the basic first clear, Sleep first, delete the data from receiving, accept the logic to deal with sticky package subcontract, this is at least programming techniques, how do you know how to block 9000 rather than 900 nor 8000 and not 100 or 9900? This card dead 9 seconds dynamic do completely is a guess, and no disease is to be stuck,
this time going to 9 seconds to receive complete data so adds a delay... Looked after such logic also don't feel too later on but also not too will change it before... After Posting for help at the advice of the great god a lot of harvest

CodePudding user response:

references to the tenth floor interacting in a professional developer response:
if you see some blogs, if events/asynchronous communication program inside wrote a Sleep, even 10 milliseconds, throw away such junk code directly, otherwise learned harmful,
ok thank god

CodePudding user response:

Why use sleep, behind calls asynchronous itself is not the future receiving efficiency, and DiscardInBuffer should also be careful to use, don't put the when processing logic from the latest valid data to throw away

CodePudding user response:

Serial port closed to open this operation is best not to have, or better to always open
 
Void serialPort_DataReceived (object sender, SerialDataReceivedEventArgs e)
{

//receiving data
String STR="";
Do
{
Int count=serialPort. BytesToRead;
If (count & lt;=0)
break;
Byte [] readBuffer=new byte (count);

Application.doevents ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related