编程Linux串口,ttyS0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10242648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:54:38  来源:igfitidea点击:

Programming Linux serial port, ttyS0

clinuxserial-porttty

提问by Frank Dejay

I'm trying to learn how to program the ttyS0 serial port in Linux using C. I have another machine connected to my serial port sending alternating hex values of 5f and 6f about every two seconds. I've verified with other port monitoring apps that these values are appearing at the port. In my code I'm using a blocking read()into a 10 char length buffer. Even though my other machine is still sending data, read()blocks forever. If I include the line fcntl(fd, F_SETFL, FNDELAY);which sets read() to non-blocking read() always returns with a value of -1, meaning no data was in the UART buffer, and my for loop code just prints out random values that are in the buffer. So in short my assumption is that my code is not reading ttyS0 and I have no idea why. Below is my code, hopefully someone will see what's causing my problem and set me straight. By the way, I'm using Scientific Linux, I believe ttyS0 is com port 1, as it is in RedHat and Fedora. Aslo below is the output when i run the code. It seems to be writing to the COM port with no problems, but for a read it says its unavailable. Also it's clear that the buffer I'm printing out is just random values not data that's been read in. Thanks

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

console output

控制台输出

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

Code

代码

#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);
}

回答by SKi

The following line will cause problems:

以下行会导致问题:

options.c_cflag &= CSTOPB;

It will reset all other bits of the c_cflag.

它将重置 c_cflag 的所有其他位。

If you want to use 1 stop bit, then use:

如果要使用 1 个停止位,请使用:

options.c_cflag &= ~CSTOPB;

If you want to use 2 stop bits, then use:

如果要使用 2 个停止位,请使用:

options.c_cflag |= CSTOPB;

EDIT:

编辑:

Also the following line cause problems:

以下行也会导致问题:

fcntl(fd, F_SETFL, 0);

It will reset several important flags.

它将重置几个重要的标志。