Send Function keys through serial port Serial.Write()

耗尽温柔 提交于 2019-12-11 19:51:25

问题


Can any one guide me how to send Function keys (F1 - F11) through serial port:

    string read_line;
    read_line = Console.ReadLine();
    SerPort.WriteLine(read_line);

回答1:


There is no standard way of sending the function keys to the serial port. You need to define your own id's for the keys (string or byte), fill it accordingly to user's current choice and send it. On the other side of the serial port whatever is listening/reading should know how to handle the input - this will be your own communication interface.

The function keys are special keys, so you need to use Console.ReadKey instead of ReadLine. A possible solution could look like this:

var key = Console.ReadKey(true);
string keyInfo = string.Empty;
byte keyInfoId = 0;
switch (key.Key)
{
    case ConsoleKey.F3: Console.WriteLine("F3 hit ..."); 
                        keyInfo = "F3"; 
                        keyInfoId = 0x3; 
                        break;
    case ConsoleKey.F5: Console.WriteLine("F5 hit ..."); 
                        keyInfo = "F5"; 
                        keyInfoId = 0x5; 
                        break;
    // ...
    default: Console.WriteLine("Not a function key"); break;
}
using (var serialPort = new SerialPort())
{
    serialPort.Open();
    serialPort.WriteLine(keyInfo);
    serialPort.Write(new byte[] { keyInfoId }, 0, 1);
    serialPort.Close();
}

You can send the the information to the serial port using SerialPort.Write or SerialPort.WriteLine.



来源:https://stackoverflow.com/questions/20143378/send-function-keys-through-serial-port-serial-write

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!