即時通訊希望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
}
對於你所問的問題,有太多的代碼。做一個你想要做的最簡單,完整的例子。 – crashmstr