I have setup a remote microcontroller connected to my linux PC that spits out a continuous string of characters. I have configured the port using screen (by issuing screen /dev/ttyS0 57600).
I validated the data with od command and got what I expected. Data follows as 8-bit hex values...
0006040 03 01 09 00 00 00 00 00 00 00 00 00 00 00 00 00
0006060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006100 00 00 00 00 00 00 00 00 00 00 00 06 0b a5 00 03
0006120 01 09 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006160 00 00 00 00 00 00 00 00 00 00 06 0b a5 00 03 01
0006200 09 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006220 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006240 00 00 00 00 00 00 00 00 00 06 0b a5 00 02 01 09
0006260 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006300 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006320 00 00 00 00 00 00 00 00 0f 0c a5 00 02 01 09 00
0006340 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006400 00 00 00 00 00 00 00 0f 0c a5 00 02 01 09 00 00
0006420 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006440 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006460 00 00 00 00 00 00 0f 0c a5 00 01 01 09 00 00 00
0006500 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006520 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006540 00 00 00 00 00 05 0c a5 00 01 01 09 00 00 00 00
0006560 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006600 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006620 00 00 00 00 05 0c a5 00 01 01 09 00 00 00 00 00
0006640 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006660 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0006700 00 00 00 05 0c a5 01 08 01 09 00 00 00 00 00 00
I then create a simple C program to try to read the same data, but all I get from my C program is the following output:
a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1
a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1
a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1
a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1
a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1
a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1
a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1
a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1 a5:1
a5:1
Based on that output, it seems the read function does return that one byte is being read, but every time the value returned is a5 which is the flag byte the micro sends out to the PC to indicate a new stream of fixed-size data.
So my program is right in overwriting the existing "A" value in the read buffer, but it needs to update with the correct data all the time (be it 00, or 01), but not just only a5
What am I doing wrong?
I attached the full code below for someone to use if they want to try to reproduce the problem. and yes, I configured the baud and data bits and stop bits correctly.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#define TIMEOUT 5
int main(){
char inb[3]; //our byte buffer
int nread=0; //number bytes read from port
int n; //counter
int iosz=128; //Lets get 128 bytes
int fd=open("/dev/ttyS0",O_NOCTTY | O_RDONLY | O_SYNC); //Open port
for(n=0;n<iosz;n++){
int s=time(NULL); //Start timer for 5 seconds
while (time(NULL)-s < TIMEOUT && nread < 1){
inb[0]='A'; //Fill buffer with bad data
inb[1]='B';
inb[2]='C';
nread=read(fd,inb,1); //Read ONE byte
if (nread < 0 || time(NULL)-s >= TIMEOUT){
close(fd); //Exit if read error or timeout
return -1;
}
}
printf("%x:%d ",inb[0] & 0xFF,nread); //Print byte as we receive it
}
close(fd); //program ends so close and exit
return 0;
}