How do i send an ASCII character in a C# program
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 开发者_JS百科'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);
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.
If you're using the SerialPort object and the WriteLine method, try this:
SerialPort serialPort = ...;
...
string message = ((char) 5).ToString();
serialPort.WriteLine(message);
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 using Write
and then need to write the char 5 to end your message, consider this route.
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.
精彩评论