tags:

views:

422

answers:

3

Hi,

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", i can send a character which is '5', am i right ?

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); Thanks.

+4  A: 

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.

James Black
Something likeserialPort.Write(new byte[]{5},0,1);if you're using the SerialPort class from .Net
Yann Schwartz
Except that 'new byte' would be signed, and most likely it should be unsigned. I had a problem with an SPI implementation where the driver used bytes and ints and it took me a bit to track down that signed can be very, very bad.
James Black
byte is not signed.
Yann Schwartz
int (System.Int32) is, though. That's what must have been a problem in your case.
Yann Schwartz
@Yann - Oops you are right. I forgot that byte is unsigned in C#. The driver code was in C++, so I had to use ubyte to solve my problem.
James Black
+1  A: 

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.

nos
+1  A: 

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.

Austin Salonen
That way he would send two bytes, not one.
EricSchaefer
and WriteLine would send a CRLF after also which may not be what you want
tjmoore
Actually this fine. SerialPort encodes as ASCIIEncoding by default and anything greater than 127 is encoded as the value 63 (http://msdn.microsoft.com/en-us/library/y2sxhat8.aspx). The CRLF still stands, but maybe that's what you'd want. You'd have to check.
tjmoore