如何在 C# 程序中发送 ASCII 字符
为了让我的 C# 程序用作串行端口接口,我需要发送“ENQ”并期望返回“ACK”。
根据我在本论坛中对之前问题的答复中的理解,我认为系统希望我发送“5”并返回“6”(ENQ 和 ACK 的 ASCII 等效项)。
因此,我可以发送一个字符“5”,而不是发送“ENQ”字符串吗?
感谢您的所有回答,它们指出 '5' 不是 C# 中的 ASCII 5。
好吧,我现在读了评论,我会尝试:
serialPort.Write(new byte[]{5},0,1);
For my C# program to work as a serial port interface, I need to send "ENQ" and expect back "ACK".
From what I understood in replies to my earlier questions in this forum, I think the system expects me to send '5' and give back a '6' (ASCII equivalents of ENQ and ACK).
So instead of sending a string which is "ENQ", can I send a character which is '5'?
Thanks for all the answers, which pointed out that '5' is not ASCII 5 in C#.
Okay I read the comments now, I will try:
serialPort.Write(new byte[]{5},0,1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过串行端口发送一个无符号字节,值为 5。
您需要检查、验证,但所有内容可能都是无符号的,因此您可以获得 0-255 作为值。
Send an unsigned byte through the serial port, with the value of 5.
You will need to check, to verify, but everything is probably going to be unsigned, so you can get 0-255 as values.
如果您使用 SerialPort 对象和 WriteLine 方法,请尝试以下操作:
要解决这些注释,您可以将
SerialPort.NewLine
属性更改为“结束传输”字符,在本例中,可能是字符 5。我针对一些使用 ETX 作为其“消息结束”字符的机器进行了开发。如果您不断使用Write
,然后需要写入字符 5 来结束消息,请考虑此路线。If you're using the SerialPort object and the WriteLine method, try this:
To address the comments, you can change the
SerialPort.NewLine
property to be your "end transmission" character which, in this case, may be char 5. I've developed against a few machines that used ETX as its "end of message" character. If you're continually usingWrite
and then need to write the char 5 to end your message, consider this route.您不能发送 5 个字符,在 C# 中,字符保存 unicode 16 位字符。
不过,您可以发送值为 5 的无符号字节。
You can't send a character that's 5, in C# chars hold unicode 16-bit character.
You can send a n unsigned byte with value 5 though.