簡體   English   中英

Visual C ++:與Arduino的串行端口通信失敗

[英]Visual C++: Unsuccessful communication through serial port with Arduino

我從Visual C ++開始,正在嘗試編寫一個使用Arduino發送和接收某些數據的程序。 但是,它似乎無法正常工作。 例如,即使當我從Arduino發送完全相同的單詞時,使用Compare :: String時,它也會返回1而不是0。

可視C ++代碼如下:

private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
    while (true)
    {
        try {
            String^ tempVal = Arduino->ReadLine();
            this->SetText(tempVal); //Calls the secure method for analyse the incoming data
            Arduino->DiscardInBuffer();
        }
        catch (TimeoutException^) {
        }
    }
}
delegate void SetTextDelegate(String^ text);
private: String^ IDemitter;
private: String^ Subred;
private: String^ Data;
private: String^ Options;
private: int Memoria;
private: void SetText(String^ texto)
{

    if (this->textReceive->InvokeRequired)
    {
        SetTextDelegate^ d = gcnew SetTextDelegate(this, &MyForm::SetText);
        this->Invoke(d, gcnew array<Object^> { texto });
    }
    else
    {

        if (String::Compare(texto, "DATA") == 1) { //Theoretically it should be 0, but it returns 1
            this->Arduino->WriteLine("OK");
            this->textReceive->Text = "OK";
            Memoria = 1; 
            texto = "0";
        }
        else if ((Memoria == 1) && (String::Compare(texto, "0") != 0)) { //It never reaches this stage... 
            IDemitter = texto;
            this->Arduino->WriteLine("OKID");
            this->textReceive->Text = "OKID";
            Memoria = 2;
            texto = "0";
        }
        else if ((Memoria == 2) && (String::Compare(texto, "0") != 0)) {
            Subred = texto;
            this->Arduino->WriteLine("OKSUB");
            Memoria = 3;
            texto = "0";
        }
        else if ((Memoria == 3) && (String::Compare(texto, "0") != 0)) {
            Options = texto;
            this->textReceive->Text = "OKOPT";
            this->Arduino->WriteLine("OKOPT");
            Memoria = 4;
            texto = "0";
            if (Options != "9") {
                this->ProcessData(IDemitter, Subred, Options);
            }
        }
        else if ((Memoria == 4) && (String::Compare(texto, "0") != 0) && (Options=="9")) {
            Data = texto;
            this->Arduino->WriteLine("OKDATA");
            this->textReceive->Text = "OKDATA";
            Memoria = 5;
            texto = "0";
            this->ProcessData(IDemitter, Subred, Options, Data);
        }
    }
}

簡化的Arduino代碼如下。

String confirmation;

void setup () {
Serial.begin(9600);
while(!Serial) { ; }
}

void loop () {

while(confirmation!="OK"){
Serial.println("DATA");
delay(5000);
confirmation=Serial.readString();
}
Serial.print("3"); //IDemitter
while(confirmation!="OKID"){
confirmation=Serial.readString();
}
Serial.print("2"); //Subred
while(confirmation!="OKSUB"){
confirmation=Serial.readString();
}
Serial.print("7"); // Options
while(confirmation!="OKOPT"){
confirmation=Serial.readString();
}

我的想法是一旦我收到所有參數,就可以在ProcessData()中進行操作,但是我認為應該更困難。 任何幫助將非常感激。

在從Arduino程序發送的“ DATA”字符串后添加換行符。 然后,來自PC的讀取線應正常工作。

暫無
暫無

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

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