how to use aridity to send byte
Closed this issue · 6 comments
I want to use Unity as my host computer to send instructions to Arduino. I need to send a 16-bit instruction by dividing it into two bytes, the upper 8 bits and the lower 8 bits, and send them separately
string sendMessage = "";
for (int i = 0;i < 14; i++)
{
sendMessage += Send[i];
//Debug.Log(Send[i] + " DEBUG");
//sendMessage += 'U';
}
This is a part of my code, where Send is a char array. After this, I will call the SendSerialMessage function to send the data in Send[] to Arduino. The problem I encountered is that when the data in Send[] is greater than 63, only 63 is sent to Arduino. When the data is less than 63, the data is correct. Is there any solution for this issue
Hi,
- What type is your Send variable?
- When you send any value from 64 to 255, is 63 arriving to the Arduino?
- What type is the variable in the Arduino side, could you also show the relevant code on that side?
arduino side
#define FRAME_HEADER 0x55
uint8_t fromUnity[32];
void RecUnity() { //从unity接收数据
uint8_t f1 = 0, f2 = 0;
while (f1 != FRAME_HEADER && Serial.available()) {
f1 = (uint8_t)Serial.read();
delay(2);
}
f2 = (uint8_t)Serial.read();
if (f2 != FRAME_HEADER) {
return;
}
fromUnity[0] = (uint8_t)Serial.read();
delay(2);
//Serial.println(fromUnity[0]);
for(uint8_t i = 1; i < fromUnity[0]; i++){
fromUnity[i] = (uint8_t)Serial.read();
delay(2);
}
for(int i = 0 ; i < fromUnity[0];i++)
Serial.println((uint8_t)fromUnity[i]);
softSerial.write(buf, buf[2] + 2);
}
unity side
public class communication : MonoBehaviour
{
public SerialController serialController;
public ushort time = 1000;
public ushort[] angel = {0,0,0,0};
private char[] Send = new char[64];
private byte[] Receive = new byte[64];
// Start is called before the first frame update
private void Awake()
{
Application.targetFrameRate = 30;
}
void Start()
{
}
// Update is called once per frame
void Update()
{
string message = serialController.ReadSerialMessage();
if (message == null)
return;
if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED))
Debug.Log("Connection established");
else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED))
Debug.Log("Connection attempt failed or disconnection detected");
else
{
Receive = Encoding.ASCII.GetBytes(message);
Debug.Log("Unity arrived: " + message);
}
}
char GET_LOW_BYTE(ushort num)
{
return (char)(num & 0xff);
}
char GET_HIGH_BYTE(ushort num)
{
return (char)(num >> 8);
}
ushort BYTE_TO_HW(byte A, byte B)
{
return ((ushort)((ushort)A << 8 | B));
}
public void Send_To_Move_Drivers()
{
Send[0] = Send[1] = (char)0x55;
Send[2] = (char)12;//数据长度
Send[3] = (char)3;
Send[4] = GET_LOW_BYTE((ushort)time);//时间
Send[5] = GET_HIGH_BYTE((ushort)time);
Send[6] = GET_LOW_BYTE((ushort)angel[0]);//舵机1
Send[7] = GET_HIGH_BYTE((ushort)angel[0]);
Send[8] = GET_LOW_BYTE((ushort)angel[1]);//舵机2
Send[9] = GET_HIGH_BYTE((ushort)angel[1]);
Send[10] = GET_LOW_BYTE((ushort)angel[2]);//舵机3
Send[11] = GET_HIGH_BYTE((ushort)angel[2]);
//Debug.Log("低八位"+Send[10]);
//Debug.Log("高八位" + Send[11]);
Send[12] = GET_LOW_BYTE((ushort)angel[3]);//舵机4
Send[13] = GET_HIGH_BYTE((ushort)angel[3]);
string sendMessage = "";
for (int i = 0;i < 14; i++)
{
sendMessage += Send[i];
//Debug.Log(Send[i] + " DEBUG");
//sendMessage += 'U';
}
char[] t= sendMessage.ToCharArray();
for (int i = 0; i < 14; i++)
{
Debug.Log((int)t[i]);
}
serialController.SendSerialMessage(sendMessage);
}
}
When I wanted to send the 16-bit integer 500, I needed to split the data into the high 8 bits and low 8 bits to send separately. The high 8 bits were 0000 0001 and the low 8 bits were 1111 0100, but when the data was sent back to the Arduino through the serial port, the high 8 bits became 0000 0001 and the low 8 bits became 0011 1111.
After further testing, I found that sending 127 is normal, but once I send 128, Arduino returns 63 to me.When I send 0000 0000 1000 0000, Arduino receives 0000 0000 0011 1111. When I send 1000 0000 0000 0000, Arduino receives 0011 1111 0000 0000.
If I use a serial debugging tool to send and receive the data mentioned above and the data returned by Arduino is correct, I am confused about this.
Hi
The code is quite long to follow, but my recommendation would be:
- check for places where the code converts between signed and unsigned types
- use
byte
instead ofchar
as much as possible, both in the Arduino and C# parts
thank you so much for help me
多谢哥