jprbest
jprbest

Reputation: 737

Read entire string by the serial port in C#

HI guys

I'm trying to read a string that send by 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

Upvotes: 2

Views: 1725

Answers (2)

SirPentor
SirPentor

Reputation: 2044

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.

Upvotes: 1

jgauffin
jgauffin

Reputation: 101192

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)

Upvotes: 2

Related Questions