开发者

Read entire string by the serial port in C#

开发者 https://www.devze.com 2023-02-25 13:07 出处:网络
HI guys I\'m trying to read a string that send by开发者_StackOverflow the serial port, I was using readline() function first but as the string has multiple lines, I have to loop, How do i determent

HI guys

I'm trying to read a string that send by开发者_StackOverflow the serial port, I was using readline() function first but as the string has multiple lines, I have to loop, How do i determent that i reached end of the string, or is there a way i can read the entire string using other then readline that not depending on the new line.

Thank you

Jp


There are no magic ways other than protocols used over the wire.

If you are sending from the other endpoint, you need to add something at the end of the string that tells the receiving end point that everything have been sent.

If you have not developed the other end point, you need to ask the manufacture/developer how you can determine then end of string.

A pretty standard way to mark beginning and ending of serial messages is to use the ASCII characters STX/ETX (0x2 and 0x3 in the ASCII table)


ETA: This isn't a good idea, since you don't know when you reach the end of the string.

I don't know about the serial port aspect, but assuming you have a Stream and all the data is available, you can wrap it in a StreamReader and call ReadToEnd.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号