我写了循环2次,再加最后一次的复原,一共是三次,正确来说接收到的数据只有三条,但是现在接收到的数据是
正确的应该是这样的,在别人写完的测试软件上是这样显示的(x,y值只是测试,所以打的不一样,主要是格式都不同,输出还很多)
这是我的接收代码
void Comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
int len = serialPort.BytesToRead;
Byte[] InputBuf = new Byte[len];
serialPort.Read(InputBuf, 0, len);
ASCIIEncoding encoding = new ASCIIEncoding();
data = encoding.GetString(InputBuf);
new Thread(SetReceiveData).Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
public void SetReceiveData()
{
lock (this)
Invoke(new MethodInvoker(delegate ()
{
this.txtReceive.AppendText(data);
}));
}
这是我的发送代码
for(int i=1;i<=2;i++)
{
string valuex = @""""+ini.IniReadValue("坐标值" + i, "x")+ @"""";
string valuey = @""""+ini.IniReadValue("坐标值" + i, "y")+ @"""";
string valuez = @""""+ini.IniReadValue("坐标值" + i, "z")+ @"""";
string valuer = @""""+ini.IniReadValue("坐标值" + i, "r")+ @"""";
sd = Encoding.UTF8.GetBytes(@"{""c"":""run""," + @"""x"":" + valuex + "," + @"""y"":" + valuey + "," + @"""z"":" + valuez + "," + @"""r"":" + valuer + "," + @"""v"":""322""}");
serialPort.Write(sd, 0, sd.Length);
textBox12.AppendText(valuex);
Thread.Sleep(10000);
}
sd = Encoding.UTF8.GetBytes(@"{""c"":""run"",""x"":""0"",""y"":""0"",""z"":""0"",""r"":""0"",""v"":""322""}");
serialPort.Write(sd, 0, sd.Length);