1

I am reading NMEA data from the usb input of an Intel Edison. Everything is working fine except for one problem. If I disconnect the RS232 to USB cable from the other two cables that lead to the bulkhead connector, I get a flood of characters sent to the software. They seem to be a large repetition of the last character sent before the cable was disconnected. I assume this is from some kind of interference introduced on the 10 inches or so of open cable. I would like to write some code so that the software can recover but as it is, I need to power cycle the box. Killing and restarting the software does not work. I have tried to just read and dump all the characters coming in until I get a valid pair but this does not work. Here is what I have tried:

    while(1){
    while (new_nmea_read_sentence == 0){
        while(read(tty_fd, &aa, 1) == -1);   // read 1 character from stream (blocking call)
        if(aa != '\n')  {
            local_buffer[idx++] = aa;
            if(idx > 400){
                idx = 0;// prevent segmentation faults
                printf("\nlocal_buffer overflow in read nmea.\n");
                int flag = 1;
                char old = 'x';
                while (flag == 1){
                    read(tty_fd, &aa, 1);
                    if (aa == '\n'  && old == '\r') flag = 0;
                    //printf("<%c>",aa);
                    old = aa;
                }
            }
        }
        else{// deal with valid nmea sentence

This thread is a forever loop reading the data. Other threads deal with it and they pass a volatile flag back and fourth. When this problem happens, the first thing I see is an overflow and I trap that in the code if(idx > 400). Without that I get a segmentation fault. The code below while (flag == 1) is looking for a which I would get if I got any valid data. If the input is an endless string of characters, I continue to get characters even after reconnecting the cable. Restarting the software produces no input.

My only solution is to tell the user to power cycle the box. Not very nice.

4

2 回答 2

0

此行中的错误处理为零:

while(read(tty_fd, &aa, 1) == -1);   // read 1 character from stream (blocking call)

此代码在所有错误情况下都读取最后一个字符,因为 的返回值read()将与 不同,-1否则永远不会检查。上面的代码会将其视为成功读取,但aa只会保留其旧值。

拉动 USB 转串口电缆会导致read()返回一些错误代码,例如 EIO 或 EBADF。

于 2014-12-23T20:36:39.307 回答
0

你建议我做什么?我应该说我尝试关闭连接并重新打开它,但它仍然无法清除并在数据返回时允许读取。稍后在代码中进行错误检查,其中读取的任何句子都会验证其校验和。

您必须在错误发生的地方处理错误,而不是在稍后您已经处理错误(即不存在)数据的地方。至少改变

                    read(tty_fd, &aa, 1);

        while (read(tty_fd, &aa, 1) < 1) /* perhaps sleep(awhile) */;
于 2017-02-03T09:33:52.077 回答