/* save "RSIO98.C" ver 1.0 H2/10/22 by S.F */ /* ver 1.1 H3/4/8 add arg. by S.F */ /* ver 1.2 H3/4/9 add str. by S.F */ /****************************************************************** RS−232C ライブラリー (Turbo−C 2.0用) ******************************************************************/ #include #include #define ON 1 #define OFF 0 #define CR 0x0d #define LF 0x0a #define Eof 0x1a #define ESC 0x1b unsigned com_init( unsigned char *buf, int size, int baud ) { COM_INIT cint; cint.cmmd = 1; /* Xパラメータ付き初期化コマンド */ switch(baud){ case 300: cint.transfer = 0x02; /* baud rate 300 bps */ case 600: cint.transfer = 0x03; /* baud rate 600 bps */ case 1200: cint.transfer = 0x04; /* baud rate 1200 bps */ case 2400: cint.transfer = 0x05; /* baud rate 2400 bps */ case 4800: cint.transfer = 0x06; /* baud rate 4800 bps */ case 9600: cint.transfer = 0x07; /* baud rate 9600 bps */ default: cint.transfer = 0x04; /* baud rate 1200 bps */ } cint.mode_inf = 0x4E; /* mode data-8,no-parity,stop-1,1/16 */ cint.cmmd_inf = 0x37; /* IR=0,RTS=1,ER=1,SBRK=0,RxE=1,DTR=1,TxE=1 */ cint.recv_buf = buf; /* receive buffer */ cint.recv_siz = size; /* receive buffer size */ cint.time_snd = 0x10; /* time_out time for send (500mS) */ cint.time_rcv = 0x0F; /* time_out time for receive (500mS) */ bios98com_init(&cint); /* RS−232Cの初期化 */ return( cint.stus ); } unsigned com_recvsize() { COM_INFO cinf; cinf.cmmd = 2; bios98com( &cinf ); /* 受信データサイズの取得 */ if( cinf.stus != 0 ) printf( "inif_stus = %02X.\n",cinf.stus ); return( cinf.data_siz ); } unsigned com_send( unsigned char ch ) { COM_INFO cinf; cinf.cmmd = 3; cinf.data = ch; bios98com( &cinf ); /* データ送信 */ if( cinf.stus != 0 ) printf( "inif_stus = %02X.\n",cinf.stus ); return( cinf.stus ); } unsigned com_recv( void ) { COM_INFO cinf; cinf.cmmd = 4; bios98com( &cinf ); /* データ受信 */ if( cinf.stus != 0 ) printf( "inif_stus = %02X.\n",cinf.stus ); return( cinf.data ); } unsigned com_sioout(int sio_cmd) /* 通信コマンド出力 */ { COM_INFO cinf; cinf.ctrl_inf = sio_cmd; cinf.cmmd = 5; bios98com( &cinf ); return( cinf.stus ); } unsigned com_sioin( void ) /* 通信ステータスの取得 */ { COM_INFO cinf; cinf.cmmd = 6; bios98com( &cinf ); return( cinf.port_inf*256 + cinf.ctrl_inf ); } com_flash(int mode) /* 通信バッファ内のデータ出力 */ { int rch; while(com_recvsize()!=0){ rch = com_recv(); if(mode) putchar(rch); /* mode=0 なら画面表示なし */ } } com_sends(char *str, int delim) /* 文字列の送信 */ { char kyin; kyin = 0; while(*str != '\0'){ com_send(*str); str++; if(kbhit() && (kyin=getch())==ESC) break; com_flash(1); } if(kyin!=ESC){ switch(delim){ case 0: break; /* 送信デリミタ無し */ case 1: com_send(CR); break; case 2: com_send(LF); break; default: com_send(CR); com_send(LF); break; } } com_flash(1); return(kyin); } com_recvs( char *buf, int delim ) /* 文字列の受信 */ { /* デリミタ文字を受信 */ char rch,tch,kyin; /* すると戻ります */ switch(delim){ case 0: tch = '\0'; break; case 1: tch = LF; break; case 2: tch = CR; break; case 3: tch = Eof; break; default: tch = CR; break; } do{ if(com_recvsize()!=0){ rch = com_recv(); putchar(rch); if(rch==0x09 || rch>=0x20){ *buf = rch; buf++; } kyin = rch; } if(kbhit()){ kyin = getch(); } }while(rch!=delim && rch!=tch && kyin!=ESC); *buf = '\0'; return(kyin); }