#include #include #include #include #include #include #define SERIAL_PORT "/dev/ttyUSB0" /* シリアルインターフェースに対応するデバイスファイル */ int main(int argc, char *argv[]) { char buf[255]; /* バッファ */ int fd; /* ファイルディスクリプタ */ struct termios oldtio, newtio; /* シリアル通信設定 */ fd = open(SERIAL_PORT, O_RDWR); /* デバイスをオープンする */ ioctl(fd, TCGETS, &oldtio); /* 現在のシリアルポートの設定を待避させる */ newtio = oldtio; /* ポートの設定をコピー */ newtio.c_cflag = B9600 | CREAD | CS8; /* ポートの設定をおこなう */ ioctl(fd, TCSETS, &newtio); /* ポートの設定を有効にする */ read(fd, buf, sizeof(buf)); /* デバイスから255バイト読み込み */ write(fd, buf, sizeof(buf)); /* デバイスへ255バイト書き込み */ ioctl(fd, TCSETS, &oldtio); /* ポートの設定を元に戻す */ close(fd); /* デバイスのクローズ */ return 0; }