Having problems reading data from Serial port using the following method which consists of two threads which access the same serial port.
While tx thread works reliable due, there is always data to be sent, the rx does not work if data is not present (RX/received) soon after application starts
Both Threads have similar structure to use the "select" for transmitting or receiving data. Any suggestion to debug further why the select does not work over time for receiving data.
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)
{
}
}
}