/* インクルードファイル */ #include /* KCXH-IOB30RTA(V1.00[2002/3/13]) プログラムで使用する各種値の定義 */ #define IOB30RTAs (1) /* 接続するIOB30RTAの数 */ #define IOBPORT (20000) /* IOB30RTAの接続ポート */ #define MYHOSTPORT1 (30000) /* ホスト側の接続ポート(分ける必要あり) */ #define MYHOSTPORT2 (30001) /* ホスト側の接続ポート(分ける必要あり)(予備) */ #define RCVTOUTSEC (5) /* IOB30RTA接続タイムアウト秒 */ #define COMMANDRETRYs (5) /* IOB30RTA通信リトライ回数 */ /* オブジェクト操作作業メモリ構造体の定義 */ typedef union { /* 共有体の定義の開始 */ long l; /* 整数データ時の格納名称 */ float f; /* 実数データ時の格納名称 */ } ObjDataSX; /* オブジェクトデータの型定義 */ /* グローバルメモリの宣言 */ KcxUdpInfoSX kcxudp [IOB30RTAs]; /* ~/karacrix?/sys/sinc/kcxux_program.h */ /* * プログラムの開始 * ※注意:本プログラムは完全な通信エラー処理に対応していませんので、実用的な * プログラムとしてお使い頂く場合には、使用環境に応じたエラー処理等が * 必須となりますので、ご注意ください。 */ main( argc, argv ) int argc; char *argv[]; { /* ローカルメモリの宣言 */ int i, len, objid; long aildata; /* アナログ値ワークメモリ */ float aival; /* アナログ値ワークメモリ */ int objid_bi01, objid_bi02; /* オブジェクトID(BI) */ int objid_bi03, objid_bi04; /* オブジェクトID(BI) */ int objid_bi05, objid_bi06; /* オブジェクトID(BI) */ int objid_bi07, objid_bi08; /* オブジェクトID(BI) */ int objid_bo01, objid_bo02; /* オブジェクトID(BO) */ int objid_bo03, objid_bo04; /* オブジェクトID(BO) */ int objid_aif01, objid_aif02; /* オブジェクトID(AI) */ int objid_aif03, objid_aif04; /* オブジェクトID(AI) */ int objid_aif05, objid_aif06; /* オブジェクトID(AI) */ int objid_aif07, objid_aif08; /* オブジェクトID(AI) */ int st_bi01, st_bi02; /* BIオブジェクト状態の記憶 */ int st_bi03, st_bi04; /* BIオブジェクト状態の記憶 */ int st_bi05, st_bi06; /* BIオブジェクト状態の記憶 */ int st_bi07, st_bi08; /* BIオブジェクト状態の記憶 */ int st_bo01, st_bo02; /* BOオブジェクト状態の記憶 */ int st_bo03, st_bo04; /* BOオブジェクト状態の記憶 */ float st_aif01, st_aif02; /* AIオブジェクト状態の記憶 */ float st_aif03, st_aif04; /* AIオブジェクト状態の記憶 */ float st_aif05, st_aif06; /* AIオブジェクト状態の記憶 */ float st_aif07, st_aif08; /* AIオブジェクト状態の記憶 */ char packetid [64]; /* フレームIDバッファ */ char cmdtext [64]; /* コマンドバッファ */ char bichanels[64]; /* Bin データの取得バッファ(1〜4ch) */ char bochanels[64]; /* Boutデータの取得バッファ(1〜4ch) */ char ai1chanel[64], ai2chanel[64]; /* Ain データの取得バッファ(1,2ch) */ char ai3chanel[64], ai4chanel[64]; /* Ain データの取得バッファ(3,4ch) */ char ai5chanel[64], ai6chanel[64]; /* Ain データの取得バッファ(5,6ch) */ char ai7chanel[64], ai8chanel[64]; /* Ain データの取得バッファ(7,8ch) */ char sndbuff[BUFSIZ]; char rcvbuff[BUFSIZ]; KcxUdpInfoSX kcxudp[IOB30RTAs]; /* ~/karacrix?/sys/sinc/kcxux_program.h */ ObjDataSX udata; /* KARACRIXからの操作データの格納 */ /* 1 ) KARACRIXの初期設定 */ kcxinit( argc, argv ); /* 2 ) 1台目のIOB30RTA / !!! IPアドレス !!! の設定例 */ kcxudp_smpl_client_init( &kcxudp[0], "192.168.0.200", IOBPORT, MYHOSTPORT1 ); /* 2台目は予備として記述 kcxudp_smpl_client_init( &kcxudp[1], "192.168.0.201", IOBPORT, MYHOSTPORT2 ); */ /* 3 ) A/Dコンバータのレンジの設定と校正(アナログ計測しない場合不要) */ for( i = 0; i < IOB30RTAs; i++ ){ switch( i ){ case 0: /* 3.1.1 ) 1台目のIOB30RTAのレンジの設定 */ /* 例: 1〜8ch-> 0〜2.5V */ strcpy( sndbuff, "1111 range 26 26 26 26 26 26 26 26" ); len = iob30rta_com0( &kcxudp[i], sndbuff, strlen(sndbuff), rcvbuff, sizeof(rcvbuff), RCVTOUTSEC ); break; case 1: /* 3.1.2 ) 2台目のIOB30RTAのレンジの設定(予備) */ /* 例: 1〜8ch-> -2.5V〜2.5V */ strcpy( sndbuff, "1111 range 26 26 26 26 26 26 26 26" ); len = iob30rta_com0( &kcxudp[i], sndbuff, strlen(sndbuff), rcvbuff, sizeof(rcvbuff), RCVTOUTSEC ); break; } rcvbuff[len] = (char)0; kcxstr_printm( 0, rcvbuff ); /*「プログラム実行」画面へのデバッグデータ表示例 */ /* 3.2 ) A/Dコンバータの校正 */ strcpy( sndbuff, "2222 cal" ); len = iob30rta_com0( &kcxudp[i], sndbuff, strlen(sndbuff), rcvbuff, sizeof(rcvbuff), RCVTOUTSEC ); rcvbuff[len] = (char)0; kcxstr_printm( 1, rcvbuff ); /*「プログラム実行」画面へのデバッグデータ表示例 */ } /* 4 ) KARACRIXで監視操作するオブジェクトIDを取得する */ objid_bi01 = kcxobj_open( "bi01" ); objid_bi02 = kcxobj_open( "bi02" ); objid_bi03 = kcxobj_open( "bi03" ); objid_bi04 = kcxobj_open( "bi04" ); objid_bi05 = kcxobj_open( "bi05" ); objid_bi06 = kcxobj_open( "bi06" ); objid_bi07 = kcxobj_open( "bi07" ); objid_bi08 = kcxobj_open( "bi08" ); objid_bo01 = kcxobj_open( "bo01" ); objid_bo02 = kcxobj_open( "bo02" ); objid_bo03 = kcxobj_open( "bo03" ); objid_bo04 = kcxobj_open( "bo04" ); objid_aif01 = kcxobj_open( "aif01" ); objid_aif02 = kcxobj_open( "aif02" ); objid_aif03 = kcxobj_open( "aif03" ); objid_aif04 = kcxobj_open( "aif04" ); objid_aif05 = kcxobj_open( "aif05" ); objid_aif06 = kcxobj_open( "aif06" ); objid_aif07 = kcxobj_open( "aif07" ); objid_aif08 = kcxobj_open( "aif08" ); /* 5 ) オブジェクトの初期状態を取得格納する */ kcxobj_stat_ird( objid_bi01, &st_bi01 ); kcxobj_stat_ird( objid_bi02, &st_bi02 ); kcxobj_stat_ird( objid_bi03, &st_bi03 ); kcxobj_stat_ird( objid_bi04, &st_bi04 ); kcxobj_stat_ird( objid_bi05, &st_bi05 ); kcxobj_stat_ird( objid_bi06, &st_bi06 ); kcxobj_stat_ird( objid_bi07, &st_bi07 ); kcxobj_stat_ird( objid_bi08, &st_bi08 ); kcxobj_stat_ird( objid_bo01, &st_bo01 ); kcxobj_stat_ird( objid_bo02, &st_bo02 ); kcxobj_stat_ird( objid_bo03, &st_bo03 ); kcxobj_stat_ird( objid_bo04, &st_bo04 ); kcxobj_stat_frd( objid_aif01, &st_aif01 ); kcxobj_stat_frd( objid_aif02, &st_aif02 ); kcxobj_stat_frd( objid_aif03, &st_aif03 ); kcxobj_stat_frd( objid_aif04, &st_aif04 ); kcxobj_stat_frd( objid_aif05, &st_aif05 ); kcxobj_stat_frd( objid_aif06, &st_aif06 ); kcxobj_stat_frd( objid_aif07, &st_aif07 ); kcxobj_stat_frd( objid_aif08, &st_aif08 ); /* 6 ) 接点とリレーの状態、及びアナログ入力値の読み取り(永久ループ) */ while( 1 ){ kcxtim_tsleep( 100000 ); for( i = 0; i < IOB30RTAs; i++ ){ /* 6.1 ) 接点とリレーの状態の読み取り */ strcpy( sndbuff, "3333 bin" ); len = iob30rta_com0( &kcxudp[i], sndbuff, strlen(sndbuff), rcvbuff, sizeof(rcvbuff), RCVTOUTSEC ); rcvbuff[len] = (char)0; sscanf( rcvbuff, "%s%s%s%s", packetid, cmdtext, bichanels, bochanels ); switch( i ){ case 0: /* 6.1.1.1 ) 1台目のIOB30RTAの接点状態を取得し変化していたら状態取込 */ if( bichanels[0] == (char)'0' ){ if( st_bi01 != (0) ){ kcxobj_stat_iwt( objid_bi01, (0) ); st_bi01 = (0); } }else{ if( st_bi01 == (0) ){ kcxobj_stat_iwt( objid_bi01, (1) ); st_bi01 = (1); } } if( bichanels[1] == (char)'0' ){ if( st_bi02 != (0) ){ kcxobj_stat_iwt( objid_bi02, (0) ); st_bi02 = (0); } }else{ if( st_bi02 == (0) ){ kcxobj_stat_iwt( objid_bi02, (1) ); st_bi02 = (1); } } if( bichanels[2] == (char)'0' ){ if( st_bi03 != (0) ){ kcxobj_stat_iwt( objid_bi03, (0) ); st_bi03 = (0); } }else{ if( st_bi03 == (0) ){ kcxobj_stat_iwt( objid_bi03, (1) ); st_bi03 = (1); } } if( bichanels[3] == (char)'0' ){ if( st_bi04 != (0) ){ kcxobj_stat_iwt( objid_bi04, (0) ); st_bi04 = (0); } }else{ if( st_bi04 == (0) ){ kcxobj_stat_iwt( objid_bi04, (1) ); st_bi04 = (1); } } /* 6.1.1.2 ) 1台目のIOB30RTAのリレー状態を取得し変化していたら状態取込 */ if( bochanels[0] == (char)'0' ){ if( st_bo01 != (0) ){ kcxobj_stat_iwt( objid_bo01, (0) ); st_bo01 = (0); } }else{ if( st_bo01 == (0) ){ kcxobj_stat_iwt( objid_bo01, (1) ); st_bo01 = (1); } } if( bochanels[1] == (char)'0' ){ if( st_bo02 != (0) ){ kcxobj_stat_iwt( objid_bo02, (0) ); st_bo02 = (0); } }else{ if( st_bo02 == (0) ){ kcxobj_stat_iwt( objid_bo02, (1) ); st_bo02 = (1); } } if( bochanels[2] == (char)'0' ){ if( st_bo03 != (0) ){ kcxobj_stat_iwt( objid_bo03, (0) ); st_bo03 = (0); } }else{ if( st_bo03 == (0) ){ kcxobj_stat_iwt( objid_bo03, (1) ); st_bo03 = (1); } } if( bochanels[3] == (char)'0' ){ if( st_bo04 != (0) ){ kcxobj_stat_iwt( objid_bo04, (0) ); st_bo04 = (0); } }else{ if( st_bo04 == (0) ){ kcxobj_stat_iwt( objid_bo04, (1) ); st_bo04 = (1); } } break; case 1: /* 6.1.2.1 ) 2台目のIOB30RTAの接点状態を取得し変化していたら状態取込(予備) */ if( bichanels[0] == (char)'0' ){ if( st_bi02 != (0) ){ kcxobj_stat_iwt( objid_bi02, (0) ); st_bi02 = (0); } }else{ if( st_bi02 == (0) ){ kcxobj_stat_iwt( objid_bi02, (1) ); st_bi02 = (1); } } /* 6.1.2.2 ) 2台目のIOB30RTAのリレー状態を取得し変化していたら状態取込(予備) */ if( bochanels[0] == (char)'0' ){ if( st_bo02 != (0) ){ kcxobj_stat_iwt( objid_bo02, (0) ); st_bo02 = (0); } }else{ if( st_bo02 == (0) ){ kcxobj_stat_iwt( objid_bo02, (1) ); st_bo02 = (1); } } break; } /* 6.2 ) アナログ入力値の読み取り */ strcpy( sndbuff, "4444 ain" ); len = iob30rta_com0( &kcxudp[i], sndbuff, strlen(sndbuff), rcvbuff, sizeof(rcvbuff), RCVTOUTSEC ); rcvbuff[len] = (char)0; sscanf( rcvbuff, "%s%s%s%s%s%s%s%s%s%s", packetid, cmdtext, ai1chanel, ai2chanel, ai3chanel, ai4chanel, ai5chanel, ai6chanel, ai7chanel, ai8chanel ); switch( i ){ case 0: /* 6.2.1 ) 1台目のIOB30RTAのアナログ値の取込(0〜2.5Vレンジ) */ aildata = strtol( ai1chanel, (char **)NULL, 10 ); aival = (float)aildata / (float)65536 * 2.5 * 100.; kcxobj_stat_fwt( objid_aif01, aival ); aildata = strtol( ai2chanel, (char **)NULL, 10 ); aival = (float)aildata / (float)65536 * 2.5 * 100.; kcxobj_stat_fwt( objid_aif02, aival ); aildata = strtol( ai3chanel, (char **)NULL, 10 ); aival = (float)aildata / (float)65536 * 2.5 * 100.; kcxobj_stat_fwt( objid_aif03, aival ); aildata = strtol( ai4chanel, (char **)NULL, 10 ); aival = (float)aildata / (float)65536 * 2.5 * 100.; kcxobj_stat_fwt( objid_aif04, aival ); aildata = strtol( ai5chanel, (char **)NULL, 10 ); aival = (float)aildata / (float)65536 * 2.5 * 100.; kcxobj_stat_fwt( objid_aif05, aival ); aildata = strtol( ai6chanel, (char **)NULL, 10 ); aival = (float)aildata / (float)65536 * 2.5 * 100.; kcxobj_stat_fwt( objid_aif06, aival ); aildata = strtol( ai7chanel, (char **)NULL, 10 ); aival = (float)aildata / (float)65536 * 2.5 * 100.; kcxobj_stat_fwt( objid_aif07, aival ); aildata = strtol( ai8chanel, (char **)NULL, 10 ); aival = (float)aildata / (float)65536 * 2.5 * 100.; kcxobj_stat_fwt( objid_aif08, aival ); break; case 1: /* 6.2.2 ) 2台目のIOB30RTAのアナログ値の取込(-2.5〜0〜2.5Vレンジ)(予備) */ aildata = strtol( ai1chanel, (char **)NULL, 10 ); aival = (float)aildata / (float)65536 * /*2.5*/ 2.0 * 100.; kcxobj_stat_fwt( objid_aif03, aival ); aildata = strtol( ai2chanel, (char **)NULL, 10 ); aival = (float)aildata / (float)65536 * 2.5 * 100.; kcxobj_stat_fwt( objid_aif04, aival ); break; } } /* 7 ) KARACRIXシステムからの出力オブジェクトデータを取得する */ switch( kcxobj_sndstat_fromkcx( &objid, &udata ) ){ case KcxINTEGER: /* 7.1 ) オブジェクトデータがあった場合、IOB30RTAのリレーを操作する */ if( objid == objid_bo01 ){ if( udata.l > 0 ) strcpy( sndbuff, "5555 bout 1---" ); else strcpy( sndbuff, "5555 bout 0---" ); iob30rta_com0( &kcxudp[(0)], sndbuff, strlen(sndbuff), rcvbuff, sizeof(rcvbuff), RCVTOUTSEC ); } if( objid == objid_bo02 ){ if( udata.l > 0 ) strcpy( sndbuff, "5556 bout -1--" ); else strcpy( sndbuff, "5556 bout -0--" ); iob30rta_com0( &kcxudp[(0)], sndbuff, strlen(sndbuff), rcvbuff, sizeof(rcvbuff), RCVTOUTSEC ); } if( objid == objid_bo03 ){ if( udata.l > 0 ) strcpy( sndbuff, "5557 bout --1-" ); else strcpy( sndbuff, "5557 bout --0-" ); iob30rta_com0( &kcxudp[(0)], sndbuff, strlen(sndbuff), rcvbuff, sizeof(rcvbuff), RCVTOUTSEC ); } if( objid == objid_bo04 ){ if( udata.l > 0 ) strcpy( sndbuff, "5558 bout ---1" ); else strcpy( sndbuff, "5558 bout ---0" ); iob30rta_com0( &kcxudp[(0)], sndbuff, strlen(sndbuff), rcvbuff, sizeof(rcvbuff), RCVTOUTSEC ); } break; default: break; } } } iob30rta_com0( kcxudp, sndbuff, sndlen, rcvbuff, rcvlen, timeoutsec ) KcxUdpInfoSX *kcxudp; char sndbuff[]; /* 送信データバッファ */ int sndlen; /* 送信データ長 */ char rcvbuff[]; /* 受信データバッファ */ int rcvlen; /* 受信データバッファ長 */ int timeoutsec; /* 受信タイムアウト秒 */ { int i,len,ichar,retry; char snd_frameidname[BUFSIZ]; char rcv_frameidname[BUFSIZ]; /* フレームIDを小文字に変換 */ sscanf( sndbuff, "%s", snd_frameidname ); for( i = 0; i < strlen( snd_frameidname ); i++ ){ ichar = tolower( (int)snd_frameidname[i] ); snd_frameidname[i] = (char)ichar; } for( retry = 0; retry < COMMANDRETRYs; retry ++ ){ /* 送信準備チェック&データ送信 */ len = kcxudp_smpl_sendto( kcxudp,(void *)sndbuff,sndlen ); if( len <= 0 ){ sleep (1); continue; } /* 受信準備チェック&データ受信 */ rcv_frameidname[0] = (char)0; len = kcxudp_smpl_tmout_recvfrom( kcxudp,(void *)rcvbuff,BUFSIZ,timeoutsec ); if( len <= 0 ){ sleep (1); continue; } sscanf( rcvbuff, "%s", rcv_frameidname ); for( i = 0; i < strlen( rcv_frameidname ); i++ ){ ichar = tolower( (int)rcv_frameidname[i] ); rcv_frameidname[i] = (char)ichar; } /* 送受信フレームIDの確認 */ if( strcmp( snd_frameidname, rcv_frameidname ) == 0 ){ /* 正常受信データ長 */ return len; }else{ sleep (1); continue; } } /* エラー発生 */ return (0); } /* FILE_END */