RS232入力を受け取り、それをファイルに保存する単純なプログラムをUNIXで実装しています。
これらのリファレンスを使用しました: http://en.wikibooks.org/wiki/Serial_Programming/Serial_Linuxおよび http://www.easysw.com/~mike/serial/serial.html
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
int main(int argc,char** argv)
{
struct termios tio;
struct termios stdio;
int tty_fd;
fd_set rdset;
FILE *file;
unsigned char c;
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open("/dev/ttyS1", O_RDWR | O_NONBLOCK);
speed_t baudrate = 1843200; //termios.h: typedef unsigned long speed_t;
cfsetospeed(&tio,baudrate);
cfsetispeed(&tio,baudrate);
tcsetattr(tty_fd,TCSANOW,&tio);
file = fopen("out.raw", "wb");
while (1)
{
if (read(tty_fd,&c,1)>0) {
fwrite(&c, 1, 1, file);
fflush(file);
}
}
//close(tty_fd);
}
921'600 bps と 1'843'200 bps で試しましたが、正しく動作します。ただし、非標準のボー レート (たとえば 1'382'400 bps) を設定すると機能しません。
つまり、これは機能します:
cfsetospeed(&tio,1843200); cfsetispeed(&tio,1843200);
しかし、これはそうではありません(ランダムデータを取得します):
cfsetospeed(&tio,1382400); cfsetispeed(&tio,1382400);
何が問題になる可能性がありますか?
WinXP で (WIN32 関数の CreateFile、SetCommState、および ReadFile を使用して) 試してみましたが、正しく動作します (1'843'200 bps および非標準の 1'382'400 bps で)。
追伸: この非標準のボーレートを設定する必要がある理由を尋ねられた場合、それは、この速度でのみ動作する特別なマシンのためです。
よろしく、デビッド