I am currently reading data coming in continuously from an i2c sensor connected to an Arduino and capturing the data successfully in Visual Studios. The sensor is getting 4 different X, Y values and then sends them via Serial.Print in the format of:
X1,Y1,X2,Y2,X3,Y3,X4,Y4 (Where X1 - X4 and Y1 - Y4 are values that range betwen 0-1023 ie, 1023,1023,1023,1023,1023,1023,1023,1023) which is then followed up with a Serial.println("") command. This whole process is repeated continuously until turned off. Arduino code:
for (i=0;i<16;i++) { data_buf[i]=0; }
i=0;
while(Wire.available() && i < 16) {
data_buf[i] = Wire.read();
i++;
}
Ix[0] = data_buf[1];
Iy[0] = data_buf[2];
s = data_buf[3];
//Ix[0] += (s & 0x30) <<4;
Ix[0] = data_buf[1] | ((data_buf[3] >> 4) & 0x03) << 8;
Iy[0] = data_buf[2] | ((data_buf[3] >> 6) & 0x03) << 8;
//Ix[0] = Ix[0] / test;
Ix[1] = data_buf[4];
Iy[1] = data_buf[5];
s = data_buf[6];
Ix[1] += (s & 0x30) <<4;
Iy[1] += (s & 0xC0) <<2;
Ix[2] = data_buf[7];
Iy[2] = data_buf[8];
s = data_buf[9];
Ix[2] += (s & 0x30) <<4;
Iy[2] += (s & 0xC0) <<2;
Ix[3] = data_buf[10];
Iy[3] = data_buf[11];
s = data_buf[12];
Ix[3] += (s & 0x30) <<4;
Iy[3] += (s & 0xC0) <<2;
for(i=0; i<4; i++)
{
if (Ix[i] < 1000)
Serial.print("");
if (Ix[i] < 100)
Serial.print("");
if (Ix[i] < 10)
Serial.print("");
Serial.print( int(Ix[i]) );
Serial.print(",");
if (Iy[i] < 1000)
Serial.print("");
if (Iy[i] < 100)
Serial.print("");
if (Iy[i] < 10)
Serial.print("");
Serial.print( int(Iy[i]) );
if (i<3)
Serial.print(",");
}
Serial.println("");
delay(15);
}
What I'm have problems with, is converting those values from within Visual Studios so that I can pass on the data as individual integer values. What I have now:
void _spManager_NewSerialDataRecieved(object sender, SerialDataEventArgs e)
{
if (this.InvokeRequired)
{
// Using this.Invoke causes deadlock when closing serial port, and BeginInvoke is good practice anyway.
this.BeginInvoke(new EventHandler<SerialDataEventArgs>(_spManager_NewSerialDataRecieved), new object[] { sender, e });
return;
}
int maxTextLength = 1000; // maximum text length in text box
if (tbData.TextLength > maxTextLength)
tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength);
// This application is connected to a Arduino sending ASCCI characters, so data is converted to text
string Rawstr = Encoding.ASCII.GetString(e.Data);
//Get values that are between the ',' from e.Data and store them in our new array
string[] str = Rawstr.Split(',');
//Convert str array into new int[] array (or similar) I've tried multiple things with no success
??????
//This results in a "Input string was not in a correct format" error.
int xx = Convert.ToInt32(str[0]);
tbData.AppendText(Rawstr);
tbData.ScrollToCaret();
}
This is where I am currently stuck. I believe I have an issue where pretty much an infant number of values are going into the string[] str because I don't have a limit on it before it starts to put new values into/and replacing the string values already in it.
Any guidance is appreciated.
You can use Linq.
First split on ,
, skip the empty and whitespace characters, then trim of the first character (X or Y) and convert the remaining part to an integer.
String Rawstr = "x1,y1,x2,y2," + Environment.NewLine;
Int32[] integers = Rawstr.Split(new [] { ',' }, StringSplitOptions.None).Where(s => !String.IsNullOrWhiteSpace(s)).Select(s => Convert.ToInt32(s.Substring(1))).ToArray();
For "x1,y1,x2,y2,"
you get an integer array containing [1,1,2,2]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.