0

使用以下方法从串行端口读取数据时遇到问题,该方法由访问同一串行端口的两个线程组成。

虽然 tx 线程工作可靠,但始终有数据要发送,如果应用程序启动后不久数据不存在(RX/接收),则 rx 不起作用

两个线程具有相似的结构来使用“选择”来传输或接收数据。任何进一步调试的建议,为什么选择不能随着时间的推移接收数据。

void SerialEngine:: thread_rx()
{

    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    int count_tmp=0;
    uint8_t read_serial_byte[1];
    uint8_t data;
    int serial_index = 0, rx_state = 0, gps_index = 0, hmi_index = 0 , GPS_HMI = 0;
    uint8_t read_serial[2500]={0};

    rfd = open(portname, O_RDWR | O_NOCTTY | O_SYNC | O_NDELAY | O_NONBLOCK);
    if (rfd < 0) {
        printf("Error opening  read %s: %s\n", portname, strerror(errno));}

    

    while(!signaled)
    {
        //printf("Recval1 is %d \n", recval1);
        FD_ZERO(&readfds);
        FD_SET(rfd, &readfds);
        //printf("Xbee Rx trying again \n");
        recval1 = select(rfd + 1, &readfds, NULL, NULL, &tv); //Reading Data from Xbee

        if ((recval1 != -1) && (recval1 != 0))
        {
            if (FD_ISSET(rfd, &readfds))
            {
                        int read_index = read(rfd, read_serial_byte,1);

                        //printf("Read Index is %d \n", read_index);

            }
        }
        else if (recval1== -1)
        {
        
        }
        else if (recval1 == 0)
        {
        
        }
        
    }
}

void SerialEngine:: thread_serial_tx()
{

    struct timeval tv;
    tv.tv_sec = 5;
    tv.tv_usec = 0;

    wfd = open(portname, O_RDWR | O_NOCTTY | O_SYNC | O_NDELAY | O_NONBLOCK);
    if (wfd < 0) {
        printf("Error opening write %s: %s\n", portname, strerror(errno));}
    Set_Serial_Atributes.set_interface_attribs(wfd, B115200);

    FD_ZERO(&writefds);
    FD_SET(wfd, &writefds);
    recval2 = select(wfd + 1, NULL, &writefds, NULL, &tv); //Writing Data to Xbee


    while(!signaled)
    {


        if ((recval2 != -1) && (recval2 != 0))
        {
            if((!tx_queue.empty()))
            {
                tx_string_check = tx_queue.front();
                int tx_str_len = tx_string_check.size();
                int bytes_written = write(wfd,hmi_serial_check, tx_str_len);
                tx_queue.pop();                                                                                                                                                               486,2         57%
            }
        }
        else if (recval1== -1)
        {
        
        }
        else if (recval1 == 0)
        {
        
        }
        
    }
}
4

0 回答 0