UART1 hangs on read command after two successful calls

Hello!

I’m having some trouble getting serial communication running reliable on my BBB. I’ve used code mostly based on this page following the non-canonical input section.

It’s up and running, and I can echo anything I send back with read, write, and shorting the TX and RX pins. The problem I’m having is if I do this multiple times in one instance of the program, on the third time my board hangs on the read command. Does anyone have any idea where a good place to start solving this issue would be?

I’ve tried using “tcflush(SERIAL_fd,TCIFLUSH);” to clear everything out after each read under the assumption that this is some kind of buffer overflow, but that doesn’t seem to do anything at all. Here are the relevant sections of my code, thanks!

void SERIAL_Init(void)
{
/*/
printf("\tSerial Port Initialization …");
SERIAL_fd = open(MODEMDEVICE,O_RDWR | O_NOCTTY /
| O_NDELAY
/);
if (SERIAL_fd < 0)
{ perror(MODEMDEVICE);
exit(-1);
}
fcntl(SERIAL_fd,F_SETFL,0);
tcgetattr(SERIAL_fd,&newtio);
newtio.c_cflag |= CS8 | CLOCAL | CREAD ;
cfsetispeed(&newtio,BAUDRATE);
cfsetospeed(&newtio,BAUDRATE);
newtio.c_iflag = IGNBRK | IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME]=0; /
inter-character timer unused /
newtio.c_cc[VMIN]=31; /
blocking read until 18 chars received /
tcflush(SERIAL_fd,TCIFLUSH);
tcsetattr(SERIAL_fd,TCSANOW,&newtio);
/
/
printf(“Done\n”);
return;
}

…and then in main without all the other code I’m adding this too…

//Read from maestro command, 1a2b is a test command strictly for echoing
unsigned char read_command[2][2] = {
{0x1a,0x2b},
{0x90,0x05},
};

unsigned char test_data[2] = {0x00,0x00};

…here’s where the actual read and write calls happen…

while(readTest == 0)
{
printf(“Enter 0 to read from maestro pin, 1 to continue with testing\n”);
scanf("%d",&readTest);

write(SERIAL_fd,read_command[0],2);
printf(“Wrote values…\n”);
num_bytes = read(SERIAL_fd,&test_data,2);
printf(“Value read: %x %x\n”,test_data[0],test_data[1]);

}

Any help would be greatly appreciated! If there’s anything else you need from me in order to help just let me know! Thanks!

Does the board actually physically hang, or is it just the one ssh session that hangs ? If its just the one ssh session, then perhaps do a dmsg | tail to see if any errors were logged. If that does not work, perhaps running strace on your executable and piping the output to a file will help ?

You're using a blocking read waiting for 31 bytes of input, but you
don't have 31 bytes of input.