シリアル通信のバッファのフラッシュ

バッファのフラッシュは大事ですね。通信相手の挙動が怪しいときは特に。

// Linuxのシリアル通信プログラムの一部
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
...
    // シリアルポートのハンドラ
    int  handle;
    if((handle = open("/dev/tty0", O_RDWR | O_NOCTTY | O_NONBLOCK))==-1){
        printf("Can not open /dev/tty0\n");
        exit(1);
    }
    ...    
    // 受信バッファのフラッシュ
    tcflush(handle,TCIFLUSH);
    // 送信バッファのフラッシュ
    tcflush(handle,TCOFLUSH);