2016年6月11日土曜日

SmartLaser用のアプリを0から開発する。その3

SmartLaserCO2というのを研究開発目的で購入、
ソフトウェアの研究開発を開始です。
さて、codeを書いてみます。
ちょっと書きっぱなしな感じなのですが、テストとしてはこんな感じです。
起動して移動させてそれからリセット(HOMEへの移動)をさせてますが、
たぶん、このままではうまく動かないです。
タイミングをうまくとらないと、ダメですね。
ここらへんは、SleepやらRecieveのバイト数やら、いろいろやることになります。
実際にはデバッガでところどころ止めながらやってみたので、うまく動作したですね。

謎の文字列はどっから引っ張ってきたかというと、実際に元々のソフトウエァを動かして、
SerialPortMonitorなるものでキャップったからですね(o^^o)

  1. int aCom::test01(void){  
  2.  HANDLE cPort = ::CreateFileA("\\\\.\\COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);  
  3.  DCB dcb;     // シリアルポート構成情報STRUCT  
  4.  ::GetCommState(cPort, &dcb);// 現在の設定値をGET  
  5. /* 
  6.  dcb.BaudRate  = 57600;  // 速度 
  7.  dcb.ByteSize  = 8;   // データ長 
  8.  dcb.Parity   = NOPARITY;  // パリティ 
  9.  dcb.StopBits  = ONESTOPBIT; // ストップビット長 
  10.  dcb.fOutxCtsFlow = 0;   // 送信時CTSフロー 
  11.  dcb.fRtsControl  = RTS_CONTROL_ENABLE; // RTSフロー 
  12. */  
  13.  ::SetCommState(cPort, &dcb); // 取得したまま書き込む。こうしないと、データが戻らない?  
  14.   
  15.  unsigned long errors = 0;  
  16.  COMSTAT comStat;  
  17.  ::ClearCommError(cPort, &errors, &comStat);  
  18.  int lenR = comStat.cbInQue; // 受信したメッセージ長を取得する  
  19.   
  20.  for(int w=0;w<20;w++){  
  21.   if(lenR) break;  
  22.   ::Sleep(500);  
  23.   ::ClearCommError(cPort, &errors, &comStat);  
  24.   lenR = comStat.cbInQue;  
  25.  }  
  26.  if(!lenR){  
  27.   // Error  
  28.   printf("error-01\n");  
  29. //  return 0;  
  30.  }  
  31.  if(lenR > 100){  
  32.   // Error  
  33.   printf("error-02\n");  
  34. //  return 0;  
  35.  }  
  36.  char rBuf[10000];  
  37.  memset(rBuf,0,sizeof(rBuf));  
  38.  unsigned long nPut = 0;  
  39.  unsigned long nRead = 0;  
  40.  ::ReadFile(cPort, rBuf, lenR, &nPut, 0); // バッファからREAD  
  41.  // テスト環境では、【# LasaurGrbl 14.01\x0a】  
  42.  if(!strstr(rBuf,"LasaurGrbl")){  
  43.   // Error  
  44.   printf("error-03\n");  
  45. //  return 0;  
  46.  }else{  
  47.   printf("LasaurGrbl OK!\n");  
  48.  }  
  49.   
  50.  ::WriteFile(cPort, "\x14", 1, &nPut, 0); // ポートへPUT  
  51.  memset(rBuf,0,sizeof(rBuf));  
  52.   
  53.  ::ReadFile(cPort, rBuf, 10, &nRead, 0);  // バッファからREAD  
  54.   
  55.  // 謎の文字列送信  
  56.  ::WriteFile(cPort, "\x5e\x9f\x3f\x0a\x2a\x9f\x3f\x0a", 8, &nPut, 0);  
  57.  memset(rBuf,0,sizeof(rBuf));  
  58.   
  59.  ::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD  
  60.   
  61. //::Sleep(2000);  
  62.   
  63.   
  64.   
  65.   
  66.  ::WriteFile(cPort, "G90\x0aM80\x0aG0F8000\x0aG1F1500\x0aS77\x0a", 28, &nPut, 0);  
  67.  memset(rBuf,0,sizeof(rBuf));  
  68.  ::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD  
  69.   
  70.   
  71.   
  72.  // 移動させてみる。  
  73.  ::WriteFile(cPort, "G0X150.0Y150.0\x0a", 15, &nPut, 0);  
  74.  memset(rBuf,0,sizeof(rBuf));  
  75.  ::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD  
  76.   
  77.  // レーザーさせてみる。  
  78.  ::WriteFile(cPort, "G1X130.0Y130.0\x0a", 15, &nPut, 0);  
  79.  memset(rBuf,0,sizeof(rBuf));  
  80.  ::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD  
  81.   
  82.   
  83.  // 戻す  
  84.  ::WriteFile(cPort, "M81\x0aS0\x0aG0X0Y0F8000\x0a", 19, &nPut, 0);  
  85.  memset(rBuf,0,sizeof(rBuf));  
  86.  ::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD  
  87.   
  88.   
  89.   
  90.   
  91.   
  92. //::Sleep(2000);  
  93.   
  94.  // 謎の文字列送信原点の移動?  
  95.  ::WriteFile(cPort, "\x21\x0a", 2, &nPut, 0);  
  96.  ::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);  
  97.   
  98.  memset(rBuf,0,sizeof(rBuf));  
  99.   
  100.  nRead = 0;  
  101.  while(nRead == 0)  
  102.  ::ReadFile(cPort, rBuf, 100, &nRead, 0); // バッファからREAD  
  103.   
  104.  printf("%s\n",rBuf);  
  105.  ::CloseHandle(cPort); // シリアルポートを閉じる  
  106.  return 0;  
  107. }  

0 件のコメント:

コメントを投稿