Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 2278|回復: 1

請問怎麼用writefile輸出字串給Arduino

[複製鏈接]
發表於 2016-7-24 17:01:55 | 顯示全部樓層 |閱讀模式
我現在在使用opencv去傳訊息給arduino,想請問怎麼用writefile輸出字串給Arduino,我照下面的程式碼是能夠傳送一個字元像是 opencv輸入'r' arduino接收到'r' 然後執行動作 ,但是沒辦法變成 opencv輸入"rr"  arduino接收到"rr"這樣


opencv 程式碼

http://aleksandarkrstikj.com/downloads/code/tracker/tracker.cpp

arduino程式碼

http://aleksandarkrstikj.com/downloads/code/tracker/tracker.ino


----以上是能夠達成傳送一個字元來執行動作
我修改arduino的部分變成


#define RED_PIN 3



void setup()
{


Serial.begin(9600);
  pinMode(RED_PIN, OUTPUT);


}



String s="";



void loop()
{

if(Serial.available() > 0)
    {

   char c = Serial.read();
   if(c != '\n')
        {
            s += c;
        }
        else
        {
            Serial.println(s);



  if(s=="xx\r")
  {

    digitalWrite(RED_PIN,HIGH);
  }


}

可以透過直接在Arduino上面的方大鏡圖片輸入裏頭輸入xx讓燈泡亮
但是連接opencv後不知道要怎麼修改讓他從原本的輸出字元變成輸出字串
-----------------------------------------------------下面是我擷取opencv傳送部分的程式碼
HANDLE hSerial = CreateFile(L"COM3", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);char outputChars[] = "c";  ---->在想是不是要改這項??  但是不知道要怎麼改       DWORD btsIO;

if (servoOrientation == 1)                       
{                 
               outputChars[0] = 'l';----->想變成輸出 "xx"這樣                        
       WriteFile(hSerial, outputChars, strlen(outputChars), &btsIO, NULL);

}

--------------------------------------------
發表於 2016-7-25 08:29:10 | 顯示全部樓層
本帖最後由 超新手 於 2016-7-25 08:48 編輯

char outputChars[] = "xx\r\n";
當然...以下這行要拿掉
outputChars[0] = 'l';
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-3-29 14:02 , Processed in 0.196376 second(s), 7 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表