Read serial data without high CPU use

后端 未结 1 1861
一向
一向 2021-02-06 11:02

I want to read messages sent from an Arduino via the FTDI (serial) interface in a simple C or C++ program under Linux. The Arduino sends a two character \'header\', a command by

相关标签:
1条回答
  • 2021-02-06 11:46

    The OP has probably long since solved this, but for the sake of anyone who gets here by google:

    #include <sys/poll.h>
    
    struct pollfd fds[1];
    fds[0].fd = serial_fd;
    fds[0].events = POLLIN ;
    int pollrc = poll( fds, 1, 1000);
    if (pollrc < 0)
    {
        perror("poll");
    }
    else if( pollrc > 0)
    {
        if( fds[0].revents & POLLIN )
        {
            char buff[1024];
            ssize_t rc = read(serial_fd, buff, sizeof(buff) );
            if (rc > 0)
            {
                /* You've got rc characters. do something with buff */
            }
        }
    }    
    

    Make sure the serial port is opened in nonblocking mode as poll() can sometimes return when there are no characters waiting.

    0 讨论(0)
提交回复
热议问题