Daten an der seriellen Schnittstelle in Linux in C löschen?

Ich teste die Sende- und Empfangsprogramme mit dem Code as

Die main () Funktion ist unten:

#include "lib.h"

int fd;

int initport(int fd) {
    struct termios options;
    // Get the current options for the port...
    tcgetattr(fd, &options);
    // Set the baud rates to 19200...
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    // Enable the receiver and set local mode...
    options.c_cflag |= (CLOCAL | CREAD);

    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;

    // Set the new options for the port...
    tcsetattr(fd, TCSANOW, &options);
    return 1;
}

int main(int argc, char **argv) {

    fd = open("/dev/pts/2", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
        perror("open_port: Unable to open /dev/pts/1 - ");
        return 1;
    } else {
        fcntl(fd, F_SETFL, 0);
    }

    printf("baud=%d\n", getbaud(fd));
    initport(fd);
    printf("baud=%d\n", getbaud(fd));

    char sCmd[254];
    sCmd[0] = 0x41;
    sCmd[1] = 0x42;
    sCmd[2] = 0x43;
    sCmd[3] = 0x00;

    if (!writeport(fd, sCmd)) {
        printf("write failed\n");
        close(fd);
        return 1;
    }

    printf("written:%s\n", sCmd);

    usleep(500000);
    char sResult[254];
    fcntl(fd, F_SETFL, FNDELAY); 

    if (!readport(fd,sResult)) {
        printf("read failed\n");
        close(fd);
        return 1;
    }
    printf("readport=%s\n", sResult);
    close(fd);
    return 0;
}


Die lib.h enthält Lese- und Schreibcode wie folgt:

Datenrahmen in C analysieren und lesen?

und bekam das Problem:

Um mit der seriellen Schnittstelle zu testen, habe ich das socat (https://help.ubuntu.com/community/VirtualSerialPort ), um ein Paar serieller Ports unter Linux zu erstellen und mein Programm mit diesen Ports zu testen.

Das erste Mal, wenn das Programm die Daten sendet und das Programm Daten empfängt, ist in Ordnung. Wenn ich jedoch die neuen Daten erneut lese oder sogar neu in die serielle Schnittstelle schreibe, sind die zurückgegebenen Daten immer null, bis ich die virtuelle serielle Schnittstelle stoppe und erneut starte, dann sind die Schreib- und Lesedaten in Ordnung, aber immer noch nur einmal.

(Im realen Fall wird der Sendeteil von einem anderen Gerät ausgeführt. Ich kümmere mich nur um das Lesen der Daten von der seriellen Schnittstelle. Ich habe beide Teile geschrieben, um meinen Lesecode zu testen.)

Hat jemand irgendwelche Ideen?

Antworten auf die Frage(1)

Ihre Antwort auf die Frage