Sending “ENTER” key through serial port

后端 未结 8 1948
独厮守ぢ
独厮守ぢ 2020-12-17 00:11

Hi I want to send some command to my device which is connected via serial port. How to send it?

For example i found this on google search but for me it\'s useless.

8条回答
  •  悲&欢浪女
    2020-12-17 00:52

    To send the enter key, you would have to use SerialPort.NewLine Property - A value that represents the end of a line

    _serialPort = new SerialPort();
    // ... this COM port parameters
    _serialPort.NewLine = "\r"; // "\r" - CR (0x0D); "\r\n" - CRLF (0x0D 0x0A)
    try
    {
        _serialPort.Open();
    }
    catch (Exception ex)
    {
      Console.Write(ex.Message);
      return;
    }
    _serialPort.WriteLine("Send string"); // Writes `Send string` string and the `NewLine` value to serial port
     // or
    _serialPort.WriteLine((char)2 + "VWD:040" + (char)3); // Writes `VWD:040` string and the `NewLine` value to serial port
    

    For a full example of working with serial port, see here.

提交回复
热议问题