2013-06-18 81 views
0

我使用C++ Builder 6與安裝TComPort組件和Arduino。我想要做的事情如下:等待,直到例程結束Arduino

for (int a = 0; a < n; a++){ 
    Edit1->Text = "first"; 
    ComPort->WriteString("a"); 
    //wait till process on Arduino is finished 
    //receive char from Arduino and continue 
    Edit1->Text = "scnd"; 
    ComPort->WriteString("b"); 
    //wait till process on Arduino is finished 
} 

Arduino的代碼(情況):

case 'b': 
    digitalWrite(ledPin2, HIGH); 
    delay(1000); 
    Serial.write('2'); 
    digitalWrite(ledPin2, LOW); 
    break; 

我試圖用OnRxChar但與Arduino的接收字符串的問題。有時他們是「空白」,有時他們是正確的(2)。 有人能指導我什麼是我可以使用的最佳功能?

編輯: COMPORT有一個功能Read(void *,int,bool),但我不知道什麼void*int代表(我是新手)。

編輯2:解決方案! 這就是我所做的:

1st function; Timer1-> Enabled = false; {instructions}; ComPort-> Write('a');

第二個函數OnRxChar; {instructions}; Timer1-> Enabled = true;

第三功能定時器; 回到第一個功能

當我使用睡眠,而不是定時器整個應用程序凍結。我希望這會對某人有用:)我花了大概一週的時間弄清楚:P

回答

1

當你這樣做ComPort->WriteString("b");你發送字符數組b\0。 在arduino方面,它似乎(因爲你不顯示你如何閱讀輸入和切換條件),你正在閱讀一個字符。

所以基本上你要做的就是:

Ard    Host 
| <---['a','\0']--- | 
|     | 
| ----['2']-------> | 
| <---['b','\0']--- | 

在那裏,你的主機發送第一a,匹配開關的情況下條件,並在下次讀它會讀取\0匹配的開關情況的條件沒有。

我不知道ComPort爭論,但你應該看看一些方法會像ComPort->WriteChar(char)代替ComPort->WriteString(string),所以你只交換角色:

Ard    Host 
| <---['a']-------- | 
|     | 
| ----['2']-------> | 
| <---['b']-------- | 

UPDATE(參見第一條評論) :

當我發現ComPort沒有公開的文件,我不能準確地幫助你,卻又讓你的C++代碼等待來自Arduino的輸入,你必須做一些事情,會是什麼樣子如下:

// blocks while there is no input on the serial line 
while (!ComPort->available()); 

,如果你不具備Arduino的類似方法available()在水果盤,你總是可以做這樣的事情

char input = '\0'; 
while ((c = ComPort->ReadChar()) == ERROR); 

其中ERROR是價值上的超時返回,或者如果它不,您可以檢查反對!= '2'

HTH

+0

當我向Arduino發送字符串時,它打開led。我的問題是如何等待,直到Arduino進程結束並移動到C++中的下一行。 – Mike

+0

對我的更新有幫助嗎? – zmo

+0

我試圖用while循環做,但程序凍結時,它進入循環內部:/我找到了另一種解決方案:我使用ComPort1-> Read(buff,1,true);和程序等待字符(這太棒了!),但我需要爲程序添加ShowMessage以在Read函數下執行行,否則它只是跳過它們:/ – Mike