【问题标题】:C Failing to Read Data from Serial Port in LinuxC Linux 无法从串口读取数据
【发布时间】:2013-10-22 03:44:27
【问题描述】:

我正在使用从这里借来的以下 C 代码:how to open, read, and write from serial port in C

稍作修改,使程序等待键盘输入,然后将此数据写入串行端口并等待读回。这是我的代码:

#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>



int
set_interface_attribs (int fd, int speed, int parity)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                error_message ("error %d from tcgetattr", errno);
                return -1;
        }

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // ignore break signal
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                error_message ("error %d from tcsetattr", errno);
                return -1;
        }
        return 0;
}

void
set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                error_message ("error %d from tggetattr", errno);
                return;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
                error_message ("error %d setting term attributes", errno);
}


int main{

    char *portname = "/dev/tty0"
    int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0)
    {
            error_message ("error %d opening %s: %s", errno, portname, strerror (errno));
            return;
    }

    set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
    set_blocking (fd, 0);                // set no blocking

    while (1){
        char data[20];
        if (fgets(data, sizeof data, stdin)){
            int m = write(fd, data, sizeof data);
            printf("%d chars written\n", m);
            char buf[100];
            sleep(10);
            int n = read (fd, buf, sizeof buf);
            printf("%d chars read", n);
        }
    }
}

问题是,虽然键盘输入成功写入串行文件,但在休眠 10 秒后,程序仍然报告它已读取 0 个字符。为什么是这样?我是否需要在端口的另一端实际等待一个物理设备来接收数据并返回一些东西,或者使用相同的程序写入和读取串行端口是否可以?提前感谢您的帮助!

编辑:

好的,现在我创建了第二个程序,它等待上述程序发送的串行端口上的数据,但是这个程序仍然无法读取发送给它的数据。代码如下:

#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>



int
set_interface_attribs (int fd, int speed, int parity)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                error_message ("error %d from tcgetattr", errno);
                return -1;
        }

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // ignore break signal
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                error_message ("error %d from tcsetattr", errno);
                return -1;
        }
        return 0;
}

void
set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                error_message ("error %d from tggetattr", errno);
                return;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
                error_message ("error %d setting term attributes", errno);
}


int main{

    char *portname = "/dev/tty0"
    int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0)
    {
            error_message ("error %d opening %s: %s", errno, portname, strerror (errno));
            return;
    }

    set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
    set_blocking (fd, 0);                // set no blocking


    char buf[20];
    int n = 0;
    while (n == 0){
        n = read(fd, buf, sizeof buf);
    }
    printf("%d chars read\n", n);

    return 0;

}

当我运行此程序并随后运行第一个程序并将一些数据写入串行端口时,此程序将永远停留在 while 循环中,从不读取任何数据。帮忙?

【问题讨论】:

  • “我是否真的需要在端口的另一端等待物理设备”?你是说现在串口什么都没有连接?
  • 没错。我的印象是串行端口的行为类似于文件,您可以简单地对其进行写入和读取。
  • 啊哈。不,串行端口没有存储数据的存储芯片,因此您可以将其读回。它用于向电缆另一端的设备发送数据,并从中读取数据。
  • 说清楚了,谢谢!
  • 建议使用已知的终端程序来验证数据是否真正被接收。 然后运行你的程序,看看它是否正常。

标签: c linux serial-port


【解决方案1】:

,您实际上不需要在接收数据的端口的另一端等待物理设备。

是的,您实际上需要有一个物理设备在端口的另一端等待返回一些东西。 (它可能是一个有线循环发送和接收(9 针上的针脚 2 和 3。)

是的,可以用同一个程序读写串口。

至于“为什么是这样的?”:没有任何东西通过串行接口向您的程序发送数据。

【讨论】:

  • 好的,我现在对我的问题进行了一些修改,请看一下。
  • 在主要评论部分提出了各种想法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多