1

我在 Raspberry Pi 上处理 UART 时似乎遇到了一个奇怪的问题。问题很简单,我初始化了 UART,如图所示并将波特率设置为 9600,甚至前几个字节的传输也顺利进行。但是,当我不断发送数据后,RPi 在屏幕上显示此消息:无法打开 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");
        }
    }

我哪里错了?

4

1 回答 1

2

您需要在例程结束时执行此操作。

close(uart0_filestream);
于 2013-08-06T19:10:25.457 回答