C#中通过串口读取整个字符串
大家好,
我正在尝试读取通过串行端口发送的字符串,我首先使用 readline() 函数,但由于字符串有多行,我必须循环,如何阻止我到达字符串末尾,或者有没有一种方法可以使用其他 readline 来读取整个字符串,而不依赖于新行。
谢谢你
Jp
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了通过网络使用协议之外,没有什么神奇的方法。
如果您从另一个端点发送,则需要在字符串末尾添加一些内容,告诉接收端点所有内容都已发送。
如果您还没有开发出另一个端点,您需要询问制造商/开发商如何确定字符串的末尾。
标记串行消息开始和结束的一种非常标准的方法是使用 ASCII 字符 STX/ETX(ASCII 表中的 0x2 和 0x3)
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:这不是一个好主意,因为您不知道何时到达字符串的末尾。
我不知道串行端口方面,但假设您有一个 Stream 并且所有数据都可用,您可以将其包装在 StreamReader 中并调用 ReadToEnd。
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.