[英]Convert and update continuous serial port communication from Arduino to an integer array in Visual Studios (C#)
I am currently reading data coming in continuously from an i2c sensor connected to an Arduino and capturing the data successfully in Visual Studios. 我目前正在读取来自连接到Arduino的i2c传感器的连续数据,并在Visual Studios中成功捕获数据。 The sensor is getting 4 different X, Y values and then sends them via Serial.Print in the format of: 传感器获取4个不同的X,Y值,然后通过Serial.Print以以下格式发送它们:
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. X1,Y1,X2,Y2,X3,Y3,X4,Y4(其中X1-X4和Y1-Y4是介于0-1023之间的值,即1023、1023、1023、1023、1023、1023、1023、1023)然后跟着Serial.println(“”)命令。 This whole process is repeated continuously until turned off. 整个过程不断重复进行,直到关闭为止。 Arduino code: Arduino代码:
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. 我遇到的问题是,在Visual Studios中转换这些值,以便我可以将数据作为单个整数值传递。 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. 我相信我有一个问题,其中几乎所有的婴儿值都会放入string [] str中,因为在开始将新值放入/替换其中的字符串值之前,我对此没有限制。
Any guidance is appreciated. 任何指导表示赞赏。
You can use Linq. 您可以使用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. 首先在,
分割,跳过空白和空白字符,然后修剪第一个字符(X或Y),并将其余部分转换为整数。
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]
. 对于"x1,y1,x2,y2,"
您将获得一个包含[1,1,2,2]
的整数数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.