visual studio c# split string into variables -
im hopeing somone can me, ive created interface using visual studio ide,which connected arduino. had issue reading data, has been solved. data sent arduino looks 1,2,3, issue having every time try split string, seprate variables doesnt seem work. post code both parts below. if me figure out great
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); // end of 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 setup code here, 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 main code here, run repeatedly: reading = analogread(inpin); reading2 = analogread(inpin2); while(serial.available() > 0) { char c = serial.read(); //gets 1 byte serial buffer readstring += c; //makes string readstring delay(5); //slow looping allow buffer fill next character } if (readstring.length() >0) { //serial.println(readstring); //so can see captured string //int n = readstring.toint(); //convert readstring 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); // end of 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 next input }
try replacing this:
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); // end of string
with this:
string[] values = readstring.split(new string[]{","},stringsplitoptions.removeemptyentries); firstvalue = values[0]; secondvalue = values[1]; thirdvalue = values[2];
hope helps.
ps - assumes have 3 values, did in code.
pps - work in c# code. understanding sending string arduino winforms - , splitting necessary in winforms app.
Comments
Post a Comment