简体   繁体   English

在 ttyS0 串行端口上获取 termios 属性时出现输入/输出错误

[英]Input/Output error when getting termios attributes on ttyS0 serial port

I am writing a simple program to open the serial port /dev/ttyS0 which is visible in /dev .我正在编写一个简单的程序来打开 /dev 中可见的串行端口 / /dev /dev/ttyS0

The code opens the serial port no problems on my PC at home but on my work machine I run into an error that returns "Input/Output Error".该代码在家中的 PC 上打开串行端口没有问题,但在我的工作机器上,我遇到返回“输入/输出错误”的错误。 The error appears as a result of tcgetattr failing but I am unsure why as the serial port is visible.该错误是由于tcgetattr失败而出现的,但我不确定为什么串行端口是可见的。 I added a verbose error printout via libexplain and it reported to me.我通过libexplain添加了一个详细的错误打印输出并报告给我。

tcgetattr(fildes = 3 "/dev/ttyS0", data = 0x7FFEEA8CEEB0) failed, Input/output error (5, EIO) tcgetattr(fildes = 3 "/dev/ttyS0", data = 0x7FFEEA8CEEB0) 失败,输入/输出错误 (5, EIO)

I am not sure what other information is relevant that I can provide.我不确定我可以提供哪些其他相关信息。 It is an Arch Linux system with a 5.3.8 kernel.它是带有 5.3.8 kernel 的 Arch Linux 系统。

const char *port_name = "/dev/ttyS0";

int main(int argc, char *argv[])
{
   int serial_fd, file_status;
    struct termios termSettings;
    struct sigaction act = { 0 };

    serial_fd = open(port_name, O_RDWR | O_NOCTTY | O_NONBLOCK);

    if (serial_fd < 0) {
        perror("Opening serial port failed");
        return -1;
    }

    if (tcgetattr(serial_fd, &termSettings) < 0) {
        perror("Getting terminal attributes failed");
        printf("Error reason: %s\n",  explain_tcgetattr(serial_fd, &termSettings));
        goto error;
    }
   ...
}

The complete source is here完整的源码在这里

The dmesg(1) output you show in the comments to your question shows UART: unknown .您在问题的评论中显示的dmesg(1) output 显示UART: unknown It seems this can be the problem.看来这可能是问题所在。 It is not recognizing any uart in port 0x3f8 , so you seem not to have an uart there (at least, not a standard or compatible one)它无法识别端口0x3f8中的任何 uart,因此您似乎在那里没有 uart (至少,不是标准或兼容的)

The message normally shows UART: ns16550a or similar, giving you info about the chipset installed there.该消息通常显示UART: ns16550a或类似信息,为您提供有关安装在那里的芯片组的信息。 In your case, the other parameters, show as reserved by the kernel so no other device can use those.在您的情况下,其他参数显示为 kernel 保留,因此其他设备无法使用这些参数。 I don't know the exact reason why the serial driver does not deallocate the resources and continues, but that's probably some legacy issue also.我不知道串行驱动程序不释放资源并继续运行的确切原因,但这也可能是一些遗留问题。

PC based uarts are normally recognized by hardwired configuration only at fixed locations in the system, and are exercised (some inocuous command is sent to see if it responds to it) to be recognized, as they are legacy devices , predating from PnP or PCI devices, so they must be probed by software at well known places.基于 PC 的 uart 通常仅在系统中的固定位置通过硬连线配置识别,并被执行(发送一些无害的命令以查看它是否响应)以被识别,因为它们是旧设备,早于 PnP 或 PCI 设备,因此它们必须在众所周知的地方通过软件进行探测。 This is what the software is doing.这就是软件正在做的事情。

If you know there's a physical port device installed, try to use BIOS SETUP to check if the serial port has been enabled in BIOS (if it hasn't you'll not see it at all).如果您知道安装了物理端口设备,请尝试使用 BIOS SETUP 检查串行端口是否已在 BIOS 中启用(如果没有,您将根本看不到它)。 Enable it if it hasn't been, and try again.如果尚未启用,请启用它,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM