簡體   English   中英

Arduino串行通信工作異常

[英]Arduino serial communication working strange

我正在編寫一個必須與Arduino通信的C#程序。 基本上,它向它發送數據,我應該能夠在串行監視器中讀取。

C#代碼:

if (errCheck[i].IsChecked.GetValueOrDefault() == true)
    err = "1"+err;
else 
    err = "0"+err;
_serialPort.Write("<16,"+ Convert.ToUInt32(err,2) + ">"); 

Arduino代碼:

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    //strtokIndx = strtok(tempChars,",");      // get the first part - the string
    //strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

    strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
    integerFromPC = atoi(strtokIndx);     // convert this part to an integer


    switch (integerFromPC) {
        //all cases         
        case 16: //managing errors
            delay(10);
            strtokIndx = strtok(NULL, ",");
            uint32_tFromPC = atoi(strtokIndx);     
            errors=uint32_tFromPC;
            Serial.print("errors Updated" );

選中最后一個復選框時(我的二進制字符串是1和31 0),串行監視器將讀取7F FF FF FF而不是80 00 00 00
我已經嘗試過使用ulong但是似乎也行不通,有什么想法嗎?

為什么要將String轉換為int32,然后再轉換回String? 只需執行以下操作:

if (errCheck[i].IsChecked.GetValueOrDefault() == true)
    err = "1"+err;
else 
    err = "0"+err;
_serialPort.Write("<16,"+ err + ">"); 

即使是Uint32也不能使用32位數字!

在您的arduino代碼中,您也正在使用atoi。 將其作為字符串處理。 為什么您需要它作為整數?

順便說一句有關將枚舉用於按位運算的示例,請看這里: http : //www.alanzucconi.com/2015/07/26/enum-flags-and-bitwise-operators/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM