There seems to be a weird problem that I have encountered while dealing with UART on the Raspberry Pi. The problem is simple, I initialize the UART as shown here and set the baud rate at 9600 and even the transmission of first few bytes goes on well. However, after sometime when I continuously keep sending the data, the RPi shows this message on the screen: Unable to open UART. Ensure it is not in use by another application.
I don't know where am I going wrong and make to things more complicated, I encountered this problem a couple of days ago before which, the code ran just fine without errors for long period of time.
Here is the code snippet:
minMaxLoc( diffImg, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
xpos=maxLoc.x;
ypos=maxLoc.y;
xposs = xpos - xposp;
yposs = ypos - yposp;
xposp = xpos;
yposp = ypos;
char x_tx_buffer[20], y_tx_buffer[20];
char x_dummy_buffer[20];
char y_dummy_buffer[20];
char *p_x_tx_buffer, *p_y_tx_buffer;
sprintf(x_dummy_buffer,"%d", (int)(abs(xposs*2.5)));
sprintf(y_dummy_buffer,"%d", (int)(abs(yposs*2.5)));
p_x_tx_buffer = &x_tx_buffer[0];
*p_x_tx_buffer++ = x_dummy_buffer[0];
*p_x_tx_buffer++ = x_dummy_buffer[1];
*p_x_tx_buffer++ = x_dummy_buffer[2];
*p_x_tx_buffer++ = x_dummy_buffer[3];
p_y_tx_buffer = &y_tx_buffer[0];
*p_y_tx_buffer++ = y_dummy_buffer[0];
*p_y_tx_buffer++ = y_dummy_buffer[1];
*p_y_tx_buffer++ = y_dummy_buffer[2];
*p_y_tx_buffer++ = y_dummy_buffer[3];
uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
}
if (uart0_filestream != -1)
{
int countx = write(uart0_filestream, &x_tx_buffer[0], (p_x_tx_buffer - &x_tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
int county = write(uart0_filestream, &y_tx_buffer[0], (p_y_tx_buffer - &y_tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
if (countx < 0 || county < 0)
{
printf("UART TX error\n");
}
}
Where am I going wrong?
You need to do this at the end of the routine.
close(uart0_filestream);