繁体   English   中英

将数据从C#程序发送到Arduino

[英]Sending data from C# program to Arduino

我有一个问题,从C#prog发送数字到Arduino(当时一个值)。 我注意到如果我发送低于128的值就可以了,问题就是从更高的值开始。

C#行:

shinput = Convert.ToInt16(line2); // shinput = short.
byte[] bytes = BitConverter.GetBytes(shinput);
arduino.Write(bytes, 0, 2);

Arduino线:

Serial.readBytes(reciver,2);
inByte[counter]= reciver[0]+(reciver[1]*256);

我真的很感激任何帮助。

您可以尝试使用已知值进行测试,以确保以已知顺序进行正确通信;

arduino.Write(new byte[]{ 145}, 0, 1);
arduino.Write(new byte[]{ 240}, 0, 1);

然后

Serial.readBytes(reciver,1); //check reciver == 145
Serial.readBytes(reciver,1); //check reciver == 240

假设这是正确的,现在测试Endianness

arduino.Write(new byte[]{ 145, 240}, 0, 2);

然后

Serial.readBytes(reciver,1); //check reciver == 145
Serial.readBytes(reciver,1); //check reciver == 240

最后,你可能有一个字节reciver [1] * 256,你需要将其转换为可以存储更大值的值:

((int) reciver[1] * 256)

试试这个:

Serial.readBytes(reciver,2); 
inShort[counter] = (short) reciver[0] | ((short) reciver[1]) << 8;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM