How to open, read, and write from serial port in C?

前端 未结 2 625
说谎
说谎 2020-11-22 01:10

I am a little bit confused about reading and writing to a serial port. I have a USB device in Linux that uses the FTDI USB serial device converter driver. When I plug it in,

2条回答
  •  感情败类
    2020-11-22 01:33

    For demo code that conforms to POSIX standard as described in Setting Terminal Modes Properly and Serial Programming Guide for POSIX Operating Systems, the following is offered.
    This code should execute correctly using Linux on x86 as well as ARM (or even CRIS) processors.
    It's essentially derived from the other answer, but inaccurate and misleading comments have been corrected.

    This demo program opens and initializes a serial terminal at 115200 baud for non-canonical mode that is as portable as possible.
    The program transmits a hardcoded text string to the other terminal, and delays while the output is performed.
    The program then enters an infinite loop to receive and display data from the serial terminal.
    By default the received data is displayed as hexadecimal byte values.

    To make the program treat the received data as ASCII codes, compile the program with the symbol DISPLAY_STRING, e.g.

     cc -DDISPLAY_STRING demo.c
    

    If the received data is ASCII text (rather than binary data) and you want to read it as lines terminated by the newline character, then see this answer for a sample program.


    #define TERMINAL    "/dev/ttyUSB0"
    
    #include 
    #include  
    #include 
    #include 
    #include 
    #include 
    #include 
    
    int set_interface_attribs(int fd, int speed)
    {
        struct termios tty;
    
        if (tcgetattr(fd, &tty) < 0) {
            printf("Error from tcgetattr: %s\n", strerror(errno));
            return -1;
        }
    
        cfsetospeed(&tty, (speed_t)speed);
        cfsetispeed(&tty, (speed_t)speed);
    
        tty.c_cflag |= (CLOCAL | CREAD);    /* ignore modem controls */
        tty.c_cflag &= ~CSIZE;
        tty.c_cflag |= CS8;         /* 8-bit characters */
        tty.c_cflag &= ~PARENB;     /* no parity bit */
        tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
        tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */
    
        /* setup for non-canonical mode */
        tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
        tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
        tty.c_oflag &= ~OPOST;
    
        /* fetch bytes as they become available */
        tty.c_cc[VMIN] = 1;
        tty.c_cc[VTIME] = 1;
    
        if (tcsetattr(fd, TCSANOW, &tty) != 0) {
            printf("Error from tcsetattr: %s\n", strerror(errno));
            return -1;
        }
        return 0;
    }
    
    void set_mincount(int fd, int mcount)
    {
        struct termios tty;
    
        if (tcgetattr(fd, &tty) < 0) {
            printf("Error tcgetattr: %s\n", strerror(errno));
            return;
        }
    
        tty.c_cc[VMIN] = mcount ? 1 : 0;
        tty.c_cc[VTIME] = 5;        /* half second timer */
    
        if (tcsetattr(fd, TCSANOW, &tty) < 0)
            printf("Error tcsetattr: %s\n", strerror(errno));
    }
    
    
    int main()
    {
        char *portname = TERMINAL;
        int fd;
        int wlen;
        char *xstr = "Hello!\n";
        int xlen = strlen(xstr);
    
        fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
        if (fd < 0) {
            printf("Error opening %s: %s\n", portname, strerror(errno));
            return -1;
        }
        /*baudrate 115200, 8 bits, no parity, 1 stop bit */
        set_interface_attribs(fd, B115200);
        //set_mincount(fd, 0);                /* set to pure timed read */
    
        /* simple output */
        wlen = write(fd, xstr, xlen);
        if (wlen != xlen) {
            printf("Error from write: %d, %d\n", wlen, errno);
        }
        tcdrain(fd);    /* delay for output */
    
    
        /* simple noncanonical input */
        do {
            unsigned char buf[80];
            int rdlen;
    
            rdlen = read(fd, buf, sizeof(buf) - 1);
            if (rdlen > 0) {
    #ifdef DISPLAY_STRING
                buf[rdlen] = 0;
                printf("Read %d: \"%s\"\n", rdlen, buf);
    #else /* display hex */
                unsigned char   *p;
                printf("Read %d:", rdlen);
                for (p = buf; rdlen-- > 0; p++)
                    printf(" 0x%x", *p);
                printf("\n");
    #endif
            } else if (rdlen < 0) {
                printf("Error from read: %d: %s\n", rdlen, strerror(errno));
            } else {  /* rdlen == 0 */
                printf("Timeout from read\n");
            }               
            /* repeat read to get full message */
        } while (1);
    }
    

提交回复
热议问题