Home > Net >  A serial port triggered two receive events, for the first time to receive the complete, correct data
A serial port triggered two receive events, for the first time to receive the complete, correct data

Time:03-02

SerialPortReceived serial port receive events code:

Int count=PGPort. BytesToRead;
Byte [] buff=new byte (count);
PGPort. Read (buff, 0, count);
String re=(BitConverter. ToString (buff). Replace (" - ", "");

Do
{
//business code
}

Trigger for the first time to receive the correct data, the second is "", namely the count=0;
Have bosses encountered such a problem, I send and receive hexadecimal all use

CodePudding user response:

Stick the data package
PC program is generally true
1. A serial port/network receives data to make one thing: in the cache
2. Data unpacking, detection: baotou, length... Check, check to ensure that a complete package received, will be a complete package of data into another cache
3. The cached data packets unpacking
4. Through multiple threads or thread pool to start the business logic

CodePudding user response:

https://docs.microsoft.com/zh-cn/dotnet/api/system.io.ports.serialport.datareceived? View=dotnet - platt - ext - 5.0
See comments

The
reference
data events may be caused by any item in the enumeration SerialData , triggered by the operating system to determine whether the incident, so as not to report all parity error,
Eof method DataReceived if received character, can trigger events, without considering the number of bytes in the internal input buffer and attribute value of ReceivedBytesThreshold,
PinChangedDataReceived ErrorReceived can call in sequence, and events, and basic flow report errors and perform there may be a slight delay between the event handler, a can only perform an event handler,
method DataReceived does not guarantee that trigger events for each received byte, use BytesToRead attributes can be sure to read the data in the buffer amount ,
Method DataReceived when the data is from the object on the auxiliary thread SerialPort trigger events, because this event is triggered on the auxiliary thread, rather than on the main thread, so try to modify the main thread of certain elements (such as UI elements) may lead to abnormal thread, if need to modify the main or the elements in the Form, please Control use will change request back to Invoke, it would finish the work in the appropriate thread,
Detailed information about the event handling, please refer to the processing and trigger events,


Microsoft made it very clear, I don't guarantee anything, only to ensure if there are data, I'll decide according to ReceivedBytesThreshold trigger (except if Eof, Eof when a time, no matter how ReceivedBytesThreshold Settings I can trigger)

Ps: there is no received the correct data for the first time, because Microsoft doesn't know how to conclude a deal both of you, so he won't consider what the correct data, he just according to the above the red bold fixed rules to deal with
  •  Tags:  
  • C#
  • Related