Linux serial port programming, ttyS0

I am trying to learn how to program the ttyS0 serial port on Linux using C. I have another machine connected to my serial port sending 5f and 6f hexadecimal variables every two seconds. I have checked with other port monitoring applications that these values ​​appear in the port. In my code, I use read () lock in a buffer of length 10 char. Even though my other machine is still sending data, read () locks are 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, that is, the data was not in the UART buffer, and my loop code just prints out random values ​​that are in the buffer. In short, I assume that my code does not read ttyS0, and I have no idea why. Below is my code, I hope someone sees the cause of my problem and puts me straight. By the way, I use Scientific Linux, I believe that ttyS0 is com-port 1, as in RedHat and Fedora. The aslo below is the result when I run the code. It seems that he writes to the COM port without problems, but for reading he says that he is not available. It is also clear that the buffer I'm printing is just random values, not the data that was read. thank

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

the 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);
}
+3
source share
1

:

options.c_cflag &= CSTOPB;

reset c_cflag.

1 , :

options.c_cflag &= ~CSTOPB;

2 , :

options.c_cflag |= CSTOPB;

EDIT:

:

fcntl(fd, F_SETFL, 0);

reset .

+4

All Articles