【问题标题】:Can write to, but can't read from serial port ttyS0 in linux C program可以写入,但不能从 linux C 程序中的串行端口 ttyS0 读取
【发布时间】:2012-04-21 04:08:54
【问题描述】:

我正在尝试学习如何使用 C 在 Linux 中对 ttyS0 串行端口进行编程。我有另一台机器连接到我的串行端口,大约每两秒发送 5f 和 6f 的交替十六进制值。我已经通过其他端口监控应用程序验证了这些值出现在端口上。在我的代码中,我使用阻塞 read() 进入 10 字符长度的缓冲区。即使我的另一台机器仍在发送数据,read() 也会永远阻塞。如果我包含将 read() 设置为非阻塞 read() 的行 fcntl(fd, F_SETFL, FNDELAY); 总是返回值 -1,这意味着 UART 缓冲区中没有数据,而我的 for 循环代码只是打印出缓冲区中的随机值。所以简而言之,我的假设是我的代码没有读取 ttyS0,我不知道为什么。下面是我的代码,希望有人会看到是什么导致了我的问题并让我直截了当。顺便说一句,我使用的是 Scientific Linux,我相信 ttyS0 是 com 端口 1,就像在 RedHat 和 Fedora 中一样。下面是我运行代码时的输出。它似乎正在毫无问题地写入 COM 端口,但读取它说它不可用。另外很明显,我打印出来的缓冲区只是随机值,而不是读入的数据。谢谢

控制台输出

hello world
hi again
write error: : Success
 wrote 4 bytes
number of bytes read is -1
read error:: Resource temporarily unavailable
4  8  120  -99  -73  -65  41  -120  4  8  
should of put something out

代码

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

int main()
{
    printf("hello world\n");
    int n;
    int fd;
    char c;
    int bytes;

    char buffer[10];
    char *bufptr;
    int nbytes;
    int tries;
    int x;
    struct termios options;


    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
    if(fd == -1)
    {
        perror("open_port: Unable to open:");
    }
    else
    {
        fcntl(fd, F_SETFL, 0);
        printf("hi again\n");
    }

    tcgetattr(fd, &options);

    cfsetispeed(&options, B115200);
    cfsetospeed(&options, B115200);
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    options.c_cflag &= ~( ICANON | ECHO | ECHOE |ISIG );
    options.c_iflag &= ~(IXON | IXOFF | IXANY );
    options.c_oflag &= ~OPOST;

    tcsetattr(fd, TCSANOW, &options);


    write(fd, "ATZ\r",4);
    printf(" wrote\n");
    bufptr = buffer;


    fcntl(fd, F_SETFL, FNDELAY);
     bytes = read(fd, bufptr, sizeof(buffer));
    printf("number of bytes read is %d\n", bytes);
    perror ("read error:");

    for (x = 0; x < 10 ; x++)
    {
        c = buffer[x];
        printf("%d  ",c);
    }
    close(fd);

    //puts(buffer[0]);
    printf("\nshould of put something out \n");

    return (0);
}

【问题讨论】:

  • 一种可能性是另一个应用程序可能正在同时读取数据。前段时间我也遇到过同样的问题。那时我正在其他 shell 上执行 cat /dev/ttyS0 并尝试使用其他 shell 上的程序进行读写。
  • 这看起来像是stackoverflow.com/questions/10242648/…的重新发布/复制/修订

标签: c linux serial-port


【解决方案1】:

尝试设置MIN 和/或TIME 值:

/*...*/
options.c_cc[VMIN] = 1; //read() will return after receiving 1 character
options.c_cc[VTIME] = 0; // == 0 - infinite timeout, != 0 - sets timeout in deciseconds
/*...*/
tcsetattr(fd, TCSANOW, &options);

给定的示例将设置您的 read() 在获取任何符号后返回并无限期地等待输入。当然,您可以根据需要使用这些参数(例如,如果您愿意,可以将 MIN 设置为 10)。

您可能需要删除 fcntl(fd, F_SETFL, FNDELAY); 调用才能使其正常工作。

保存以前的 termios 设置并在离开程序之前恢复它们也是明智之举。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    相关资源
    最近更新 更多