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

查看:269
本文介绍了如何使用.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设备使用raw模式.

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 属性(例如,波特率,奇偶校验,字符大小)或echo属性(如您所知).

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天全站免登陆