0

私は1回線のシリアル通信インターフェースを持っていますが、問題は01010101で送信し、受信するエコーが01010101の10回のうち8回ですが、10回のうち2回は01110101を受信することです。

コード例:

void checkVersion(int fd) {
    tcflush(fd, TCIFLUSH);
    unsigned char checkVersion[] = {0x55, 0x02, 0x00, 0x02};
    int n = write(fd, &checkVersion, 4); //Send data
    if (n < 0) cout << "BM: WRITE FAILED" << endl;

    char read_bytes[10] = {0};
    char c;
    int aantalBytes = 0;
    bool foundU = false;
    int res;
    while (aantalBytes < 7) {
        res = read(fd, &c, 200);
        if (res != 0) {
            cout << "Byte received: " << bitset < 8 > (c) << endl;
            if (c == 'U')foundU = true;
            if (foundU)
                read_bytes[aantalBytes++] = c;
        }
        if (aantalBytes > 2 && !foundU) break;
    }
    if (!foundU) checkVersionSucceeded = false;
    if (read_bytes[aantalBytes - 3] == 0x02 && read_bytes[aantalBytes - 2] == 0x04 && read_bytes[aantalBytes - 1] == 0x06)
       cout << "BM Version 4" << endl;
}

ポートの構成方法:

int configure_port(int fd) // configure the port
{
    struct termios port_settings; // structure to store the port settings in

    cfsetispeed(&port_settings, B9600); // set baud rates
    cfsetospeed(&port_settings, B9600);

    port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
    port_settings.c_cflag &= ~CSTOPB;
    port_settings.c_cflag &= ~CSIZE;
    port_settings.c_cflag |= CS8;

    tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
    return (fd);
} 

何が問題ですか?エコーが10回のうち2回混合される可能性はありますか?

4

1 に答える 1

1

おそらく、接続を構成するときに関数bzero()を試す必要があります。

bzero(&port_settings, sizeof (port_settings));

これにより、新しいポート設定の構造体がクリアされ、シリアルポートを介して受け取る不規則な応答を防ぐのに役立つ場合があります。

于 2012-05-29T09:41:03.107 に答える