Ferdinand Peters
Ferdinand Peters

Reputation: 11

Serial port reading: Raspberry PI misses data

I have connected a device which sends data over a serial port, through UART and a usb connector.

On my laptop running ubuntu, I have written a C++ program to read this data and store it. It sends 5 lines of 15 bites, 100 times per seconds, meaning: 500 lines per second.

I use the standard read command:

while((res += read(IMU, header, 15-res)) < 15);

    string head(header, 15);
    cout << indexLog <<  " " << head << endl;

when I read the serial port and read 15 bites it shows:

0 snp�` �����
0 snp�b#A��H
0 snp�dP�O�^���
0 snp�\����f
0 snp�^���e��M
0 snp�` �����
0 snp�b"����

where the snp is the beginning of each package. It can be seen that my laptop nicely reads every bite coming in, since every line starts with snp.

I would like to run the same application on my Raspberry PI, for weight and size considerations.

now when I run the same application on my raspberry PI, I get:

0 �Nsnp�Nsnp
0 Nsnp�\��
0 vsnp�^���
0 np�^�O�
0 vsnp�^�O�
0 np�^�D�
0 ssnp�
0 X�snp�dU

It looks like the raspberry doesn't read all the bites, and it becomes a big mess. Trying to salvage usefull date results in big data loss.

I all ready installed a skimmed down version of Rasbian, weezy. deinstalled X as GUI. But it doesn't seem to make difference.

I believe the raspberry should be fast enough.

What would be the limiting factor?

Is reading 500 lines (15 bites) per second over a serial just to much to cope with for the RasPI?

Are there any settings I can change or OS for the raspberry which can do the job better? Or is there a way in C++ to read the data more efficient?

kind regards

Upvotes: 1

Views: 5804

Answers (1)

Hans Passant
Hans Passant

Reputation: 941744

Is reading 500 lines (15 bites) per second

That is 500 x 15 x 10 = 75000 bits per second. Clearly that is not possible when your baudrate is only 38400. So transmitter overrun is your first candidate for data loss.

You also have a fire-hose problem on the receiving end. Your terminal needs to be able to keep up with the rate, 500 lines per second is pretty challenging. Scrolling the window fast enough is the usual problem, not to mention the poor eyes of the user that needs to stare at this Matrix effect. This is the more likely source of the data loss, when you don't empty the serial port receive buffer quickly enough then you'll suffer from buffer overflow. It can also occur when the driver doesn't empty the UART fifo buffer quickly enough, less likely to be a problem at 38400 baud.

The obvious way ahead here is to improve your code so that you can actually detect these kind of errors so you know when it goes wrong.

Upvotes: 2

Related Questions