Venkat Raju
Venkat Raju

Reputation: 1

Send Function keys through serial port Serial.Write()

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

Upvotes: 0

Views: 1601

Answers (1)

keenthinker
keenthinker

Reputation: 7830

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.

Upvotes: 1

Related Questions