Sending “ENTER” key through serial port

后端 未结 8 1911
独厮守ぢ
独厮守ぢ 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:38

    To send the enter key, you would have to use

    serial.Write(new byte[]{13,10}, 0, 2);
    

    Assuming your syntax for Control + E is correct. The enter key is interpreted and usually saved in a file as CR-LF. However, depending on your device, it may only require CR=13, or LF=10. You should try all 3 combinations with your device to see what it expects.

    If you are looking for the actual scan code of the enter key, it's "43" on a PC 102/104 key keyboard. Depending on the actually computer you are using, it may be different. For instance on a Commodore 64 the scan code for the Return key is "1", which has the equivalent use of Enter on a PC keyboard.

    0 讨论(0)
  • 2020-12-17 00:42

    The microsoft version of enter or new line is \r\n which is 0x0d 0x0a in hex.

    • \r is the carriage return

      In a shell or a printer this would put the cursor back to the beginning of the line.

    • \n is the line feed

      Puts the cursor one line below, in some shells this also puts the cursor to the beginning of the next line. a printer would simply scroll the paper a bit.

    So much for the history lesson. Current windows systems still use these characters to indicate a line ending. Dos generated this code when pressing enter.

    The key code is a bit different. Beginning with the esc key being the 1. Enter is 28.

    Source: linux hlkeycodes from www.comptechdoc.org

    0 讨论(0)
  • 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 `<HEX 0x02>VWD:040<HEX 0x03>` string and the `NewLine` value to serial port
    

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

    0 讨论(0)
  • 2020-12-17 00:56

    I appended "\r\n" in string and call Write() method and it works for me. For Example,

    serial.Write("abcd\r\n");

    0 讨论(0)
  • 2020-12-17 00:57

    You need send the commands CR (Carriage Return) and LF (Line Feed, or new line).

    For this is just to send your command plus the the CR and LF like this:

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

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

    0 讨论(0)
  • 2020-12-17 00:58

    Thanks guys.

    This works:

    serial.Write("\r\n") 
    

    Note: if you want to send a command through serial port, I use the line below works for me.

    serial.Write("your_command\r\n");
    
    0 讨论(0)
提交回复
热议问题