[英]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.