2017-07-25 3 views
0

Ich hoffe, jemand kann mir helfen, IVE erstellt eine Schnittstelle mit dem Visual Studio IDE, die auch mit einem Arduino verbunden ist. Ich hatte ein Problem mit dem Lesen der Daten, aber es wurde gelöst. die Daten aus dem Arduino sieht wie folgt aus 1,2,3, das Problem, das ich habe ist, dass jedes Mal, wenn ich versuche, die Zeichenfolge in seprate Variablen zu trennen scheint es nicht zu funktionieren. Ich werde den Code für beide Teile unten veröffentlichen. wenn jemand könnte mir dies herausfinden helfen, wäre tollVisual Studio C# Split-String in Variablen

Visual Studio:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO.Ports; 

namespace WindowsFormsApp3 
{ 
    public partial class Form1 : Form 
    { 
     public SerialPort myport; 

     int irisvalue; 

     string myString; 
     String s; 

     //string[] words; 

     String readString; 

     String firstValue; 
     String secondValue; 
     String thirdValue; 

     public Form1() 
     { 
      InitializeComponent(); 
      //Load += new EventHandler(Form1_Load); 
      connectbtn.Text = "Connect"; 
      disconnect.Text = "Disconnect"; 
      this.connectbtn.Click += new EventHandler(connectbtn_Click); 
      this.disconnect.Click += new EventHandler(disconnect_Click); 
      this.iris1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.iris1_MouseDown); 
      this.iris1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.iris1_MouseUp); 
      this.iris2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.iris2_MouseDown); 
      this.iris2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.iris2_MouseUp); 
      this.focus1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.focus1_MouseDown); 
      this.focus1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.focus1_MouseUp); 
      this.focus2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.focus2_MouseDown); 
      this.focus2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.focus2_MouseUp); 

     } 
     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 


     void connect() 
     { 

      myport = new SerialPort(); 
      myport.BaudRate = 9600; 
      myport.PortName = "COM3"; 
      myport.Open(); 

     } 

     void read() 
     { 


      myport.DataReceived += (sender, e) => 
      { 
       if (e.EventType == SerialData.Chars) 
        s = myport.ReadLine(); 

      }; 

     } 

     void discon() 
     { 


      myport.Close(); 

     } 

     private void disconnect_Click(object sender, System.EventArgs e) 
     { 
      discon(); 
      if (myport.IsOpen) 
      { 

      } 
      else 
      { 

       connectbtn.Text = "Connect"; 
       disconnect.BackColor = default(Color); 
       connectbtn.BackColor = default(Color); 
      } 
      } 

      private void connectbtn_Click(object sender, System.EventArgs e) 
     { 
      connect(); 

      if (myport.IsOpen) 
      { 

       connectbtn.Text = "Connected"; 
       connectbtn.BackColor = Color.Green; 
       //Load += new EventHandler(Form1_Load); 
       Form1_Load(); 
       disconnect.BackColor = Color.Red; 
       disconnect.Text = "Disconnect"; 
       read(); 
       //s = myport.ReadLine(); 





      } 
      else 
      { 
       connectbtn.Text = "Error"; 
       connectbtn.BackColor = Color.Red; 
      } 


     } 


     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

     private void iris1_MouseDown(object sender, MouseEventArgs e) 
     { 
      //Console.WriteLine("Hello"); 

      irisvalue = 1; 
      myString = irisvalue.ToString(); 
      Form1_Load(); 

     } 

     private void iris1_MouseUp(object sender, MouseEventArgs e) 
     { 

      irisvalue = 0; 
      myString = irisvalue.ToString(); 
      Form1_Load(); 
     } 



     private void iris2_MouseDown(object sender, MouseEventArgs e) 
     { 
      irisvalue = 2; 
      myString = irisvalue.ToString(); 
      Form1_Load(); 

     } 


     private void iris2_MouseUp(object sender, MouseEventArgs e) 
     { 
      irisvalue = 0; 
      myString = irisvalue.ToString(); 
      Form1_Load(); 

     } 

     private void focus1_MouseDown(object sender, MouseEventArgs e) 
     { 
      irisvalue = 3; 
      myString = irisvalue.ToString(); 
      Form1_Load(); 

     } 


     private void focus1_MouseUp(object sender, MouseEventArgs e) 
     { 
      irisvalue = 0; 
      myString = irisvalue.ToString(); 
      Form1_Load(); 


     } 

     private void focus2_MouseDown(object sender, MouseEventArgs e) 
     { 
      irisvalue = 4; 
      myString = irisvalue.ToString(); 
      Form1_Load(); 

     } 

     private void focus2_MouseUp(object sender, MouseEventArgs e) 
     { 

      irisvalue = 0; 
      myString = irisvalue.ToString(); 
      Form1_Load(); 
     } 



     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

     public void Form1_Load() 
      { 





      readString += s; 


      if (readString.Length > 0) 
      { 

       int commaIndex = readString.IndexOf(','); 
       int secondCommaIndex = readString.IndexOf(',', commaIndex + 1); 

       firstValue = readString.Substring(0, commaIndex); 
       secondValue = readString.Substring(commaIndex + 1, secondCommaIndex); 
       thirdValue = readString.Substring(secondCommaIndex + 1); // To the end of the string 

       //int x = Int32.Parse(firstValue); 
       //int y = Int32.Parse(secondValue); 
       // int z = thirdValue.toInt(); 



       Console.WriteLine(firstValue); 
       Console.WriteLine(secondValue); 
       Console.WriteLine(thirdValue); 
      } 


      myport.WriteLine(myString); 




     } 

     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 


    } 

} 

Arduino:

String readString; 

int INA = 11; 
int INB = 9; 
int INC = 7; 
int IND = 5; 

int ledpin = 3; 

int inPin = 14; 
int inPin2 = 15; 

int reading; 
int reading2; 
int nu; 

const String com = ","; 

void setup() { 
    // put your setup code here, to run once: 
    Serial.begin(9600); 
pinMode(INA, OUTPUT); 
pinMode(INB, OUTPUT); 
pinMode(INC, OUTPUT); 
pinMode(IND, OUTPUT); 

pinMode(ledpin, OUTPUT); 

pinMode(inPin, INPUT); 
pinMode(inPin2, INPUT); 

} 

void loop() { 
    // put your main code here, to run repeatedly: 

reading = analogRead(inPin); 
reading2 = analogRead(inPin2); 

while(Serial.available() > 0) { 
    char c = Serial.read(); //gets one byte from serial buffer 
    readString += c; //makes the string readString 

    delay(5); //slow looping to allow buffer to fill with next character 
} 


if (readString.length() >0) { 
    //Serial.println(readString); //so you can see the captured string 
    //int n = readString.toInt(); //convert readString into a number 

    int commaIndex = readString.indexOf(','); 
    int secondCommaIndex = readString.indexOf(',', commaIndex+1); 

String firstValue = readString.substring(0, commaIndex); 
String secondValue = readString.substring(commaIndex+1, secondCommaIndex); 
String thirdValue = readString.substring(secondCommaIndex+1); // To the end of the string 

int x = firstValue.toInt(); 
int y = secondValue.toInt(); 
int z = thirdValue.toInt(); 

////////////////////////////////////////////////////////////////////////////////////////////////////////// 

if (x == 1){ 

digitalWrite(INA, HIGH); 
digitalWrite(INB, LOW); 
digitalWrite(INC, LOW); 
digitalWrite(IND, LOW); 

} 

else if (x == 2){ 


digitalWrite(INA, LOW); 
digitalWrite(INB, HIGH); 
digitalWrite(INC, LOW); 
digitalWrite(IND, LOW); 

} 

else if (x == 3){ 


digitalWrite(INC, HIGH); 
digitalWrite(IND, LOW); 
digitalWrite(INA, LOW); 
digitalWrite(INB, LOW); 

} 

else if (x == 4){ 


digitalWrite(INC, LOW); 
digitalWrite(IND, HIGH); 
digitalWrite(INA, LOW); 
digitalWrite(INB, LOW); 

} 


else{ 

digitalWrite(INA, LOW); 
digitalWrite(INB, LOW); 
digitalWrite(INC, LOW); 
digitalWrite(IND, LOW); 


} 

Serial.println(1 + com + 2 + com + 3); 
//Serial.print(", "); 
//Serial.print(reading); 
//Serial.print(", "); 
//Serial.print(reading2); 
//Serial.println(", "); 


////////////////////////////////////////////////////////////////////////////////////////////////////////// 
} 



readString=""; //empty for next input 


} 
+2

Viel zu viel Code für das Problem, nach dem Sie fragen. Machen Sie ein minimales, vollständiges Beispiel von dem, was Sie versuchen zu tun. – crashmstr

Antwort

0

Versuchen Sie ersetzen diese:

  int commaIndex = readString.IndexOf(','); 
      int secondCommaIndex = readString.IndexOf(',', commaIndex + 1); 

      firstValue = readString.Substring(0, commaIndex); 
      secondValue = readString.Substring(commaIndex + 1, secondCommaIndex); 
      thirdValue = readString.Substring(secondCommaIndex + 1); // To the end of the string 

mit diesem:

  string[] values = readString.Split(new string[]{","},StringSplitOptions.RemoveEmptyEntries); 
      firstValue = values[0]; 
      secondValue = values[1]; 
      thirdValue = values[2]; 

Ich hoffe, es hilft.

PS - vorausgesetzt, Sie haben immer drei Werte, wie Sie es in Ihrem Code getan haben.

PPS - dies funktioniert nur in Ihrem C# -Code. Mein Verständnis ist, dass Sie die Zeichenfolge von Arduino an Winforms senden - und das Teilen ist nur in der Winforms-App erforderlich.

+1

Er verwendet Arduino, also gibt es keine Split-Funktion. Das gleiche habe ich zuerst auch gedacht. –

+0

Das erste Bit Code scheint eine legitime Windows Forms App zu sein - nicht sehen, wie string.split nicht funktionieren würde. –

+0

Oh, jetzt habe ich bemerkt, dass er den gleichen Code auch auf der Arduino-Seite hat. Danke - werde versuchen, es aufzuräumen. –

0

Beim nächsten Mal bitte nicht den gesamten Code eingeben, sondern nur den Teil, zu dem Ihre Frage gehört.

See this original post for separating the string based on the separator and passing an index to get the value.

String getValue(String data, char separator, int index) 
{ 
int found = 0; 
int strIndex[] = { 0, -1 }; 
int maxIndex = data.length() - 1; 

for (int i = 0; i <= maxIndex && found <= index; i++) { 
    if (data.charAt(i) == separator || i == maxIndex) { 
     found++; 
     strIndex[0] = strIndex[1] + 1; 
     strIndex[1] = (i == maxIndex) ? i+1 : i; 
    } 
} 
return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; 
} 

Sie können dann die Funktion aufrufen in Ihrem readString Variable vorbei zusammen mit dem Index variabel Sie wollen:

String firstValue = getValue(readString,',',0); 
String secondValue = getValue(readString,',',1); 
String thirdValue = getValue(readString,',',2); 
0

die Split-Methode in String verfügbar Versuchen (C#) . Wie in:

 string[] myVars = readString.Split(","); 
    if (myVars.Length > 2) 
    { 
     firstValue = myVars[0]; 
     secondValue = myVars[1]; 
     thirdValue = myVars[2]; 
    } 

Im Debug-Modus starten und den Inhalt von readString überprüfen.