2016-04-12 34 views
0

ile seri bağlantı noktası üzerinden başarısız iletişim Visual C++ ile başlıyorum ve belirli verileri Arduino ile gönderen ve alan bir program yazmaya çalışıyorum. Ancak, düzgün çalışmıyor gibi görünmüyor. Karşılaştırma :: Ben Arduino tam aynı kelimeyi gönderiyorum bile dize kullanırken Örneğin, 1 0 yerineVisual C++: Arduino

görsel C döner ++ kod aşağıdaki gibidir:

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); 
     } 
    } 
} 

Ve basitleştirilmiş Arduino kodu şöyledir.

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(); 
} 

fikri tüm parametreleri aldıktan sonra ProcessData() faaliyet, ama ben olması gerektiği düşünülen bu daha zor ediliyor. Herhangi bir yardım gerçekten takdir edilecektir.

+0

'String :: Compare (texto," DATA ")' çağrısına bir kesme noktası koyun ve o satıra geldiğinizde, 'texto' içeriğine gerçekten iyi bakın. Herhangi bir boşluk var mı? Beklenmeyen uzunluk uyuşmazlığı? –

cevap

0

Arduino'nun programından gönderilen "DATA" dizesine yeni satır ekleyin. Ardından, bilgisayardan gelen readline düzgün çalışması gerekir.

+0

Ok teşekkürler! Bu, Karşılaştırma bölümündeki sorunu çözüyor gibi görünüyor. Ancak, eğer Memoria 1'e eşit ise, ilk olarak sıkışır. Arduino, doğru şekilde "OK" almıyor gibi görünüyor. Düşüncesi olan var mı? \ N "Tamam \ n" göndermeyi denedim ancak yanıt vermemeye çalıştım. – Pekuso

+0

"texto" değişkenini boşaltmanız gerekir - "DATA \ n" kaldığı sürece, ilkiniz her zaman doğrudur. – rdavb