| |
検索
辞書
交通
地図
天気
|
|
RS232C の入出力を行うプログラム
linux gcc で主にVISCAプロトコルで動作するソニー製のカメラを制御するために作成したサンプルプログラムですが, 他の RS232C の入出力を行って制御するプログラムにも転用できると思います. Windows VC++ 版を探している方はこちらへどうぞ.
//RS232C制御用アプリケーションのサンプル
//EVI-D100 を制御するためのサンプルですが他の場合にも役立つでしょう.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
//ここからメイン
int main( void )
{
//変数宣言
int fd;
struct termios tio;
int err;
int i,j;
// 送信用
char send[32];
int send_length;
//受信用
unsigned char resv[32];
int resv_length;
unsigned char buff;
// Com1 を開いて初期設定
if ((fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK))<0) {
exit(-1);
}
bzero(&tio, sizeof(tio));
tio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
tio.c_iflag = IGNPAR;
tio.c_oflag = 0;
tio.c_lflag = 0;
tio.c_cc[VTIME] = 0;
tio.c_cc[VMIN] = 1;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &tio);
fcntl(fd, F_SETFL, FNDELAY);
//初期設定ここまで
for(j=0;j<5;j++){
switch(j){
case 0: sprintf(send,"\x88\x30\x01\xff"); send_length=4; break; //アドレス設定
case 1: sprintf(send,"\x88\x01\x00\x01\xff"); send_length=5; break; //インタフェイスクリア
case 2: sprintf(send,"\x81\x01\x06\x04\xff"); send_length=5; break; //ホーム
case 3: sprintf(send,"\x81\x01\x06\x01\x10\x10\x03\x01\xff"); send_length=9; break; //上向く
case 4: sprintf(send,"\x81\x01\x06\x04\xff"); send_length=5; break; //ホーム
}
sleep(1);
err = write(fd, send, send_length); //書き込み
printf("%d 回目:",j+1);
for(i=0;i<32;i++){
err=-1;
while(err<0){
err = read(fd,&buff,1); //1つずつ受信
}
resv[i]=buff;
printf("%02x ",buff);
if(buff==0xff){//ACK の次は Completion
if(resv[0]==0x90 && resv[1]==0x41) i=0;
else i=32;
}
}
printf("\n");
}
//後処理
close( fd );
}
|
|
|
|