2017-07-25 35 views
0

即時通訊希望somone能夠幫助我,我創建了一個使用Visual Studio IDE的界面,它也連接到一個arduino。我在閱讀數據時遇到了一個問題,但它已被解決。從arduino發送的數據看起來像這樣1,2,3,我遇到的問題是每當我嘗試將字符串拆分成單獨的變量時,它似乎都不起作用。我將在下面發佈這兩個部分的代碼。如果有人可以幫助我弄清楚這一點,將是巨大的visual studio c#將字符串拆分爲變量

視覺工作室:

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

對於你所問的問題,有太多的代碼。做一個你想要做的最簡單,完整的例子。 – crashmstr

回答

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 

與此:

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

希望它有幫助。

PS - 這裏假設你總是有三個值,就像你在代碼中所做的那樣。

PPS - 這隻適用於您的C#代碼。我的理解是,你從Arduino發送字符串到winforms - 只有在winforms應用程序中需要分割。

+1

他使用的是Arduino,所以沒有Split功能。我一開始也是這麼想的。 –

+0

第一個代碼似乎是一個合法的Windows窗體應用程序 - 沒有看到如何string.split不會工作。 –

+0

哦,現在我注意到他在Arduino方面也有相同的代碼。謝謝 - 將嘗試清除它。 –

0

下一次,請不要把所有的代碼,而只是你的問題所屬的部分。

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]) : ""; 
} 

然後,您可以調用該函數在readString變量傳遞與可變你想要的指數一起:

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

嘗試在字符串中可用的拆分方法(C#) 。如在:

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

在調試模式下啓動並檢查readString的內容。