Send CR LF via Serial port c#

Asked

Viewed 1,464 times

2

I’m communicating by Serial (SerialPort) with a device that requires me to send commands CR LF at the end of each instruction for the device to understand that an instruction has been sent to it.

The instructions I must send are all in ASCII.

I know that in the ASCII table there are the commands Line-Feed - Feed line (10) and Carriage-Return - Car return (13).

The communication is working perfectly, but the device always returns me invalid command because I need to send these characters at the end of each instruction.

How do I write along on Port.Write or in the Port.WriteLine at the end of each instruction these characters in ASCII?

  • 4

    Would write the string "\r\n"?

  • @The device sends me through the door WITH "UNKNOW COMMAND"

1 answer

4


To send the CR (Carriage Return) and LF (Line Feed) commands together with the command would just add the commands \r and \n at the end of the command within the method to send a command through the COM port.

string command = "myCommand";

port.write(string.format("{0}\r\n", command));

\r\n = CR + LF Used as a new line character in Windows

Browser other questions tagged

You are not signed in. Login or sign up in order to post.