Closed luoxihao closed 1 year ago
Hi,
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:
byte
instead of char
as much as possible, both in the Arduino and C# partsthank you so much for help me
有遇到过
关于这个问题,我建议你去检查以下几点
检查你arduino的波特率和uinity组件设置的波特率是否相等,端口号是否选对(用设备管理器或者arduinoide可以看)
关闭其他串口调试工具,unity的串口通信是等同于截获arduinoide的那个串口调试的信息,如果同时开两个或者多个,打开串口的可能不是unity而是别的串口调试软件
发件人: @. @.> 代表 franklikn 发送时间: 2023年3月30日 6:17 收件人: dwilches/Ardity @.> 抄送: luoxihao @.>; Author @.***> 主题: Re: [dwilches/Ardity] how to use aridity to send byte (Issue #68)
哥,您有没遇到arduino和unity连接的时候一直都是显示connection attempt failed or disconnection detected吗
— Reply to this email directly, view it on GitHub https://github.com/dwilches/Ardity/issues/68#issuecomment-1489402146 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ANWT5EXH6WLAY3AZKH56MBTW6SYFTANCNFSM6AAAAAAVT4UBY4 . You are receiving this because you authored the thread. https://github.com/notifications/beacon/ANWT5ERJL7OHZ7E5F2ET5FTW6SYFTA5CNFSM6AAAAAAVT4UBY6WGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTSYYZ4SE.gif Message ID: @. @.> >
多谢哥
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
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