Home > Net >  A serial port should adopt what method receives a message?
A serial port should adopt what method receives a message?

Time:10-28

Using the SerialPort class receives a serial port communication message, message format is 0 xf3 xx xx xx,,, xx xx 0 XFD, the header is the F3, the tail is FD, xx is data, message length is not fixed, interval between two adjacent message about 500 milliseconds, consider the following several ways of judgment of message, but didn't succeed:
1, one of the most common method, set the port attribute. ReceivedBytesThreshold, after receiving a set number of bytes of data receive events triggered port. The method DataReceived, because the message length is not fixed, so I can't use this method,
Port 2, use the read method. ReadTo (Encoding. The ASCII. Get string (new byte [1] XFD {0})), when read newspaper tail, stop debugging found that could not read the data, to estimate the reason is the ASCII value of 0 XFD corresponding not a printable characters,
3, the feeling is the most reasonable method should be: when a serial port for some time, for example 100 ms, not received the data, all data is read buffer, and determine the header to tail, but this kind of method how to write code? SerialPort ReadTimeout doesn't seem right, is to open a 100 ms timer?
4, because a message between two adjacent bytes of data, the time interval is almost equal to zero, so each receive a byte to read and judge once, this way too late time,

Which method should be used? Directly, thanks!

CodePudding user response:

Unlimited listening to receive this is for you, will receive the data in a queue, then the asynchronous extraction fragments from the queue,

CodePudding user response:

reference 1/f, morning to evening reply:
unlimited listening to receive this is for you, will receive the data in a queue, asynchronous extraction fragments from the queue and then parse,

Thank you for reply!
"Infinite listening to receive, will receive the data in a queue", should be how to write code, can be broadly say please, thank you

CodePudding user response:

reference 1/f, morning to evening reply:
unlimited listening to receive this is for you, will receive the data in a queue, asynchronous extraction fragments from the queue and then parse,

After each get the message from the other device, I will reply in time, so cannot receive a lot of data, later again slowly to parse,

CodePudding user response:

After receiving set the number of bytes of data to trigger receive events, to keep the data read out to the queue, and then parsing in the queue, resolves to a complete frame is called processing code

CodePudding user response:

With serial assistants try, send how many milliseconds to return data, two packets 500 milliseconds apart enough, unless the data returned is particularly slow

CodePudding user response:

While (! Stop)
{
Byte [] data=https://bbs.csdn.net/topics/new byte [1024].//1024 set a maximum length for the data, if the performance requirements is not high, with a List , easy... , preferably under the data contained in the header data length, easy to receive when application array length
While (not a terminator) {
To join in the data read from a byte;
}
Processing the data;
}

CodePudding user response:

Data protocol typically contain baotou, data packet tail three parts, baotou general processing data information such as length, package tail generally contains information check and terminator etc, complete data protocol can better deal with all kinds of anomalies.

CodePudding user response:

refer to 6th floor ziqi0716 response:
While (! Stop)
{
Byte [] data=https://bbs.csdn.net/topics/new byte [1024].//1024 set a maximum length for the data, if the performance requirements is not high, with a List , easy... , preferably under the data contained in the header data length, easy to receive when application array length
While (not a terminator) {
To join in the data read from a byte;
}
Processing the data;
}

Pseudo code is not very serious, general train of thought

CodePudding user response:

1. You are receiving data hexadecimal data not ASCII and can be used as follows:
Comm. Read (buf, 0, n);
Foreach (byte b in buf)//to save message to string
{
//in turn of joining together the hexadecimal string
Builder. Append (b.T oString (" X2 "));

}
Data. The AddRange (buf);//or save message to byte [],
2. Open a thread analysis byte []
From the first byte analysis F3, not delete
Find 0 XFD, start a message, after the completion of the delete
Continue to
  •  Tags:  
  • C#
  • Related