I write a uart program using c++ on Jetson Nx(Jetpack 4.6.1,Ubuntu version 18.04 LTS) to communicate with a PC. On PC there’s a uart simulator( as below figure 1) sending data at a period of one second, 30 bytes data are : EB90021112131415161718191A1B1C1D1E1F202122232425262728292A2B
When my program receive the data, it will lost the first byte “EB”, and sometimes more bytes are lost. as show in figure 2:
My c code is as below.
#include <iostream>
#include <unistd.h>
#include <sys/fcntl.h>
#include <termios.h>
#include <string.h>
using namespace std;
int TestUart(const char * portName, int nSpeed, int nBits, char nEvent, int nStop);
int main() {
TestUart("/dev/ttyUSB0", 9600, 8, 'N', 1);
return 0;
}
int TestUart(const char * portName, int nSpeed, int nBits, char nEvent, int nStop)
{
int Port = open(portName, O_RDWR | O_NOCTTY);
tcflush(Port, TCIFLUSH);
tcflush(Port, TCIOFLUSH);
usleep(500000);
if (Port == -1)
{
printf("UART.cpp : Unable to open port.");
return -1;
}
else
{
printf("UART.cpp : Port opened. Setting Port options...");
}
int fd = Port;
struct termios newtio, oldtio;
if (tcgetattr(fd, &oldtio) != 0)
{
perror("SetupSerial 1");
return -1;
}
newtio = oldtio;
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch (nBits)
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch (nEvent)
{
case 'O':
case 'o':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
break;
case 'E':
case 'e':
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
case 'n':
newtio.c_cflag &= ~PARENB;
break;
}
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
newtio.c_cflag &= ~CRTSCTS;
newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
if (nStop == 1)
{
newtio.c_cflag &= ~CSTOPB;
}
else if (nStop == 2)
{
newtio.c_cflag |= CSTOPB;
}
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtio.c_oflag &= ~OPOST;
if ((tcsetattr(fd, TCSANOW, &newtio)) != 0)
{
perror("com set error");
return -1;
}
tcflush(fd, TCIFLUSH);
int num = 0;
unsigned char rx_data[256];
while (1)
{
int m_curPacket_length = read(Port, rx_data, 256);
tcflush(Port, TCIFLUSH);
tcflush(Port, TCIOFLUSH);
if (m_curPacket_length <= 0) continue;
printf(" Data %d [port = %s,length = %d]: ", ++num, portName, m_curPacket_length);
for (int i = 0; i < m_curPacket_length; i++) {
printf("%X ", rx_data[i]);
}
printf("\n");
}
return 0;
}
Besides, I tried more tests:
1 C code check. I run my c code on a PC( Ubuntu 18.04.6 LTS os on a windows virtual machine), it doesn’t lost byte. As show in figure 3 above:
2 I changed my c code as below ,set a un-blocking mode, it will not lost any data. Thren I change back to blocking mode as below, it won’t lost any data; and then I restart the Nx, and run my original code(blocking mode), the lost first byte appears.
3 I tried two third party softwares on Nx to receive data, CuteCom won’t lost first data while MiniCom lost first byte, the same problem. Test figure as Figure 4 above:
(CuteCom not lost first byte, Note this tiem sending data are EB901f1112……1c1d)
Figure 5 (Mini Com Lost first byte ‘EB’, note sending data are eb901f1112…28cb146c)
I expect anyone can help me, thanks a lot!