在處理Raspberry Pi上的UART時,我遇到了一個奇怪的問題。問題很簡單,我初始化UART,如here所示,並將波特率設置爲9600,甚至前幾個字節的傳輸進行得很順利。但是,在我持續不斷地發送數據之後,RPi會在屏幕上顯示此消息:無法打開UART。確保它未被其他應用程序使用。無法打開UART。確保它沒有被其他應用程序使用
我不知道我在哪裏錯了,使事情變得更加複雜,前兩天我遇到了這個問題,在這之前,代碼長時間沒有錯誤地運行得很好。
這裏是代碼片段:
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");
}
}
我要去哪裏錯了?
你是否一遍又一遍地調用它?你還記得在再次打開它之前關閉它嗎? – Jiminion