如何使用 .NET Core 在 Linux 上以非规范模式打开 tty 设备 [英] How to open a tty device in noncanonical mode on Linux using .NET Core

查看:25
本文介绍了如何使用 .NET Core 在 Linux 上以非规范模式打开 tty 设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在嵌入式 Linux 平台上使用 .NET Core 并取得了成功.不过,我在尝试以原始(非规范模式)打开 tty 设备时遇到了问题.如果我使用的是常规 C 或 C++,我会在打开设备后调用 cfmakeraw(),但如何从 .NET Core 应用程序执行此操作?

I'm using .NET Core on an embedded Linux platform with good success so far. I just ran into a problem with trying to open a tty device in raw (noncanonical mode) though. If I was using regular C or C++ I would call cfmakeraw() after opening the device, but how do I do that from a .NET Core app?

我需要使用的设备是用于 USB 客户端连接器的 CDC ACM 功能驱动程序,即它是一个虚拟 COM 端口.它在我的系统中显示为 /dev/ttyGS0.我可以打开设备,然后使用以下代码从中读取和写入:

The device I need to work with is a CDC ACM function driver for the USB client connector, i.e. it's a virtual COM port. It appears in my system as /dev/ttyGS0. I can open the device and then read from it and write to it using this code:

FileStream vcom = new FileStream("/dev/ttyGS0", FileMode.Open);

因为默认情况下 tty 设备以规范模式打开,所以在用户在文本行末尾发送回车符之前,我不会收到任何字符.我需要在发送时接收每个字符,而不是等到发送回车,即我需要对 tty 设备使用原始模式.

Because the tty device opens in canonical mode by default I don't receive any characters until the user sends the carriage return character at the end of the line of text. I need to receive each character as it is sent, rather than waiting untill the carriage return is sent, i.e. I need to use raw mode for the tty device.

下面的代码不起作用,因为.NET Core没有意识到这个设备是一个虚拟串口,所以当我尝试用这种方式打开它时会抛出异常.当我使用 SerialPort 打开真正的 UART 设备时,它们会按预期在原始模式下运行.

The code below does not work because .NET Core does not realize that this device is a virtual serial port, so it throws an exception when I try to open it this way. When I open the real UART devices using SerialPort then they do behave in raw mode as expected.

SerialPort serialPort = new SerialPort("/dev/ttyGS0);

推荐答案

由于您有终端设备,您可以在实际使用之前尝试更改其 termios 配置.
在运行程序之前尝试发出 shell 命令 stty -F/dev/ttyGS0 raw.

Since you have a terminal device, you could try to alter its termios configuration prior to actually using it.
Try issuing the shell command stty -F /dev/ttyGS0 raw before you run your program.

raw 设置将对非规范模式进行以下 termios 更改(根据 stty 手册页):

The raw setting will make the following termios changes (according to the stty man page) for noncanonical mode:

-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixany -ixoff -imaxbel 
-opost 
-icanon -isig -xcase -iuclc  
min 1 time 0

请注意,raw 设置不会更改 c_cflag 属性(例如波特率、奇偶校验、字符大小)和回声属性(如您所知).

Note that no c_cflag attributes (e.g. baudrate, parity, character size) nor echo attributes (as you already know) are changed by the raw setting.

为了比较 libc cfmakeraw() 例程您提到进行以下 termios 设置:

For comparison the libc cfmakeraw() routine that you mention makes the following termios settings:

  t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  t->c_oflag &= ~OPOST;
  t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  t->c_cflag &= ~(CSIZE|PARENB);
  t->c_cflag |= CS8;
  t->c_cc[VMIN] = 1;      /* read returns when one char is available.  */
  t->c_cc[VTIME] = 0;

<小时>

您可以使用 stty -F/dev/ttyGS0 sane 将终端恢复到默认的 termios 配置.


You can use stty -F /dev/ttyGS0 sane to restore the terminal to a default termios configuration.

这篇关于如何使用 .NET Core 在 Linux 上以非规范模式打开 tty 设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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