少々時間が無かったので昨日の解析結果が正しいかどうか確認するだけ。
BAND A/D の ROBO-Q も調達できたので 4バンド分の信号を確認できました。

実際にコマンド送信プログラムを作ってみます。
スーの道具箱/分解してみよう/PC-OP-RS1
のページを参考にさせていただきました。そのままです。

初期化

hPort= CreateFile(
		"COM4",
		GENERIC_WRITE|GENERIC_READ,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL,
		OPEN_EXISTING,
		0,
		NULL
	);
if( hPort == INVALID_HANDLE_VALUE ){
	// ERROR
	return;
}
DCB	dcb;
dcb.DCBlength= sizeof(DCB);
dcb.BaudRate= CBR_115200;
dcb.fBinary= TRUE;
dcb.fParity= FALSE;
dcb.fOutxCtsFlow= FALSE;
dcb.fOutxDsrFlow= FALSE;
dcb.fDtrControl= DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity= FALSE;
dcb.fTXContinueOnXoff= FALSE;
dcb.fOutX= FALSE;
dcb.fInX= FALSE;
dcb.fErrorChar= FALSE;
dcb.fNull= FALSE;
dcb.fRtsControl= RTS_CONTROL_DISABLE;
dcb.fAbortOnError= FALSE;
dcb.fDummy2= 0;
dcb.wReserved= 0;
dcb.XonLim= 0;
dcb.XoffLim= 0;
dcb.ByteSize= 8;
dcb.Parity= NOPARITY;
dcb.StopBits= ONESTOPBIT;
dcb.XonChar= 0;
dcb.XoffChar= 0;
dcb.ErrorChar= 0;
dcb.EofChar= 0;
dcb.EvtChar= 0;
dcb.wReserved1= 0;
SetCommState( hPort, &dcb );

送受信は WriteFile()/ReadFile() です。

PC-OP-RS1 用の送信データを組み立てます。
赤外線の On/Off 情報をビット列で指定。あくまで動作確認用なのでべたで。

int pushbit( char* ptr, int index, int flag, int length )
{
	if( flag ){
		for( int i= 0 ; i< length ; i++ ){
			int	byteoffset= index >> 3;
			int	bitoffset= index & 7;
			ptr[byteoffset] |=  (1<<bitoffset);
			index++;
		}
	}else{
		index+= length;
	}
	return	index;
}

// H 1ms  + data x7
void SendCommand( int band, int speed, int turn )
{
	unsigned int	bitpattern=  ((speed<<5) & 0x60)
				|(( turn<<3) & 0x18)
				|(( band<<1) & 0x06);
	char	byte[240];
	memset( byte, 0, 240 );
	int	index= 0;
	index= pushbit( byte, index, TRUE, 10 ); // H 1ms
	for( int i= 0 ; i< 7 ; i++ ){
		if( bitpattern & (1<<(6-i)) ){
			index= pushbit( byte, index, TRUE, 10 ); // H 1ms
		}else{
			index= pushbit( byte, index, TRUE, 5 ); // H 0.5ms
		}
		index= pushbit( byte, index, FALSE, 5 ); // L 0.5ms
	}
	SendIR( byte, 240 );
}

データ送信

void SendIR( const char* command )
{
    iPort->Send( "t", 1 );
    iPort->Recv( rdata, 1 );	// 'Y'
    iPort->Send( "1", 1 );		// ch1
    iPort->Recv( rdata, 1 );	// 'Y'
    iPort->Send( command, 240 );
    iPort->Recv( rdata, 1 );	// 'E'
}

昨日のデータ通りです。すんなり動作しました。
PC で ROBO-Q を操作できます。

SendCommand( 1, 2, 0 ); // BAND B で中速(M)前進
SendCommand( 1, 1, 1 ); // BAND B で低速(L)右回転

でもデータ送信まで思ったより時間がかかるので、細かい制御をリアルタイムに
行うには少々厳しいです。要検討。

修正 2009/3/5 4:27 勘違いでした。十分な速度で操作できます。

きちんと使うなら 192ms 分複数のコマンドをパックして送る必要あります。


関連エントリ
タカラトミー ROBO-Q (2) 赤外線コマンドの解析
タカラトミー ROBO-Q