通过串口Serial.Write()发送功能键 [英] Send Function keys through serial port Serial.Write()

查看:1050
本文介绍了通过串口Serial.Write()发送功能键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以指导我如何通过串行端口发送功能键(F1-F11):

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

推荐答案

没有将功能键发送到串行端口的 standard 方法.您需要为密钥(字符串或字节)定义自己的ID,并根据用户的当前选择相应地填写并发送.在串行端口的另一侧,无论正在收听/阅读的内容,都应该知道如何处理输入-这将是您自己的通信接口.

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();
}

您可以使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆