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