ソフトウェアの研究開発を開始です。
さて、codeを書いてみます。
ちょっと書きっぱなしな感じなのですが、テストとしてはこんな感じです。
起動して移動させてそれからリセット(HOMEへの移動)をさせてますが、
たぶん、このままではうまく動かないです。
タイミングをうまくとらないと、ダメですね。
ここらへんは、SleepやらRecieveのバイト数やら、いろいろやることになります。
実際にはデバッガでところどころ止めながらやってみたので、うまく動作したですね。
謎の文字列はどっから引っ張ってきたかというと、実際に元々のソフトウエァを動かして、
SerialPortMonitorなるものでキャップったからですね(o^^o)
int aCom::test01(void){
HANDLE cPort = ::CreateFileA("\\\\.\\COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
DCB dcb; // シリアルポート構成情報STRUCT
::GetCommState(cPort, &dcb);// 現在の設定値をGET
/*
dcb.BaudRate = 57600; // 速度
dcb.ByteSize = 8; // データ長
dcb.Parity = NOPARITY; // パリティ
dcb.StopBits = ONESTOPBIT; // ストップビット長
dcb.fOutxCtsFlow = 0; // 送信時CTSフロー
dcb.fRtsControl = RTS_CONTROL_ENABLE; // RTSフロー
*/
::SetCommState(cPort, &dcb); // 取得したまま書き込む。こうしないと、データが戻らない?
unsigned long errors = 0;
COMSTAT comStat;
::ClearCommError(cPort, &errors, &comStat);
int lenR = comStat.cbInQue; // 受信したメッセージ長を取得する
for(int w=0;w<20;w++){
if(lenR) break;
::Sleep(500);
::ClearCommError(cPort, &errors, &comStat);
lenR = comStat.cbInQue;
}
if(!lenR){
// Error
printf("error-01\n");
// return 0;
}
if(lenR > 100){
// Error
printf("error-02\n");
// return 0;
}
char rBuf[10000];
memset(rBuf,0,sizeof(rBuf));
unsigned long nPut = 0;
unsigned long nRead = 0;
::ReadFile(cPort, rBuf, lenR, &nPut, 0); // バッファからREAD
// テスト環境では、【# LasaurGrbl 14.01\x0a】
if(!strstr(rBuf,"LasaurGrbl")){
// Error
printf("error-03\n");
// return 0;
}else{
printf("LasaurGrbl OK!\n");
}
::WriteFile(cPort, "\x14", 1, &nPut, 0); // ポートへPUT
memset(rBuf,0,sizeof(rBuf));
::ReadFile(cPort, rBuf, 10, &nRead, 0); // バッファからREAD
// 謎の文字列送信
::WriteFile(cPort, "\x5e\x9f\x3f\x0a\x2a\x9f\x3f\x0a", 8, &nPut, 0);
memset(rBuf,0,sizeof(rBuf));
::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD
//::Sleep(2000);
::WriteFile(cPort, "G90\x0aM80\x0aG0F8000\x0aG1F1500\x0aS77\x0a", 28, &nPut, 0);
memset(rBuf,0,sizeof(rBuf));
::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD
// 移動させてみる。
::WriteFile(cPort, "G0X150.0Y150.0\x0a", 15, &nPut, 0);
memset(rBuf,0,sizeof(rBuf));
::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD
// レーザーさせてみる。
::WriteFile(cPort, "G1X130.0Y130.0\x0a", 15, &nPut, 0);
memset(rBuf,0,sizeof(rBuf));
::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD
// 戻す
::WriteFile(cPort, "M81\x0aS0\x0aG0X0Y0F8000\x0a", 19, &nPut, 0);
memset(rBuf,0,sizeof(rBuf));
::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD
//::Sleep(2000);
// 謎の文字列送信原点の移動?
::WriteFile(cPort, "\x21\x0a", 2, &nPut, 0);
::WriteFile(cPort, "\x5e\x80\x7e\x0a\x2a\x80\x7e\x0a\x5e\x95\x47\x33\x30\x0a\x2a\x95\x47\x33\x30\x0a", 20, &nPut, 0);
memset(rBuf,0,sizeof(rBuf));
nRead = 0;
while(nRead == 0)
::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD
printf("%s\n",rBuf);
::CloseHandle(cPort); // シリアルポートを閉じる
return 0;
}
0 件のコメント:
コメントを投稿