/* Copyright(c) 2002,2003 Device Drivers Limited, All rights reserved. */ /* info@devdrv.com http://www.devdrv.com/ */ #include #include #include static char r[2] = {0,0}; /* 入力側のバッファ */ static char w[2] = "A"; /* 最初の出力文字 */ main() { int fd; int i; struct termios tio; /* ttyS0ファイルディスクリプタのオープン処理 */ if ((fd = open("/dev/ttyS0", O_RDWR)) < 0) { fprintf(stderr, "open error\n"); exit(1); } bzero(&tio, sizeof(tio)); /* アトリビュート設定構造体の初期化 */ /* 115200bps, フロー制御有り, 8ビット,DTR/DSR無効,受信可能 */ tio.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD; tio.c_cc[VMIN] = 1; /* 入力データをバッファしない */ tcsetattr(fd, TCSANOW, &tio); /* アトリビュートのセット */ for(i = 0; i < 20; i++) { /* ループは20回 */ write(fd, w, 1); /* 1文字出力 */ read(fd, r, 1); /* 1文字入力 */ printf("read = %s\n", r); /* 入力結果表示 */ w[0]++; /* 出力側の文字を変更 */ } close(fd); exit(0); } /* コンパイルコマンド: cc -g -o onebyone onebyone.c */