从C#程序向Arduino发送数据

从C#程序向Arduino发送数据,c#,arduino,C#,Arduino,我无法将数字从C#prog发送到Arduino(一次一个值)。 我注意到,如果我发送的值低于128,那么问题就从较高的值开始 C#行: shinput = Convert.ToInt16(line2); // shinput = short. byte[] bytes = BitConverter.GetBytes(shinput); arduino.Write(bytes, 0, 2); Serial.readBytes(reciver,2); inByte[counter]= recive

我无法将数字从C#prog发送到Arduino(一次一个值)。 我注意到,如果我发送的值低于128,那么问题就从较高的值开始

C#行:

shinput = Convert.ToInt16(line2); // shinput = short.
byte[] bytes = BitConverter.GetBytes(shinput);
arduino.Write(bytes, 0, 2);
Serial.readBytes(reciver,2);
inByte[counter]= reciver[0]+(reciver[1]*256);
Arduino系列:

shinput = Convert.ToInt16(line2); // shinput = short.
byte[] bytes = BitConverter.GetBytes(shinput);
arduino.Write(bytes, 0, 2);
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);
然后

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

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

最后,很可能您有一个字节接收器[1]*256,您需要将其转换为一个可以存储较大值的值:

((int) reciver[1] * 256)
所以试试这个:

Serial.readBytes(reciver,2); 
inShort[counter] = (short) reciver[0] | ((short) reciver[1]) << 8;
Serial.readBytes(接收器,2);
inShort[counter]=(short)reciver[0]|((short)reciver[1])您的C#程序和arduino代码是否以相同的波特率传输?