unity 串口读取 g4 激光雷达数据

主要参考了 https://blog.csdn.net/weixin_44345862/article/details/86500349 此篇文章,在此表示感谢。

先是把数据读出来,后面还有很多工作要做

    private void DataReceiveFunction()
    {
        print("数据监测中···");
        byte[] dataBytes = new byte[255];//存储数据
        int bytesToRead = 0;//记录获取的数据长度
        while (isAlive)
        {

            if (sp != null && sp.IsOpen)
            {
                try
                {

                    bytesToRead = sp.Read(dataBytes, 0, dataBytes.Length);
                    for (int i = 0; i < bytesToRead; i++)
                    {
                        list.Add(dataBytes[i]);

                    }

                }
                catch (Exception ex)
                {
					print("DataReceiveFunction " + ex.Message);
                }
            }
			Thread.Sleep(1);//50     
        }
    }

 

void Update()
    {
        interval += Time.deltaTime;
        if (interval >= 1f)// iniconfig.instance.getfloat("sys", "aipatrol"))
        {
             interval = 0;
             ClearAllItem("Panel");
            list.Clear();
        }
        if (list.Count > 20)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (i < list.Count - 4)
                {
                    // 发现数据开头
                    if (list[i] == Convert.ToByte("AA", 16) &&
                        list[i + 1] == Convert.ToByte("55", 16) &&
                        list[i + 2] == Convert.ToByte("00", 16))
                    {
                        // 采集数据数量
                        int count = list[i + 3];
                        if (count > 1)
                        {
                            //判断是否接收完了全部数据
                            if (list.Count - i > count * 2 + 10)
                            {
                                recvStr = "";
                                List<byte> getdata = list.GetRange(i, count * 2 + 10);
                                for (int j = 0; j < getdata.Count; j++)
                                    recvStr += getdata[j].ToString("X2") + " ";
                                float fsa = (ByteToInt(getdata[4], getdata[5]) >> 1) / 64;
                                float lsa = (ByteToInt(getdata[6], getdata[7]) >> 1) / 64;
                                for (int j = 0; j < count * 2; j += 2)
                                {
                                    // 距离毫米
                                    int dis = ByteToInt(getdata[j + 10], getdata[j + 11]) / 4;
                                    if (dis > 1)
                                    {
                                        // 计算角度 顺时针计算 所以结束减开始
                                        float diff = lsa - fsa;
                                        if (diff < 0)
                                        {
                                            diff = lsa + (360 - fsa);
                                            //print(lsa + " ; " + fsa);
                                        }
                                        float angle = diff / (count - 1) * (j / 2) + fsa;
                                        //print(fsa + " ; " + lsa + " ; " + angle + " ; " + dis);
                                        RawImage img = (RawImage)Instantiate(rimg, new Vector3(dis * 4.0f, 0, 0), Quaternion.identity);
                                        img.transform.SetParent(rpanel.transform, true);
                                        img.transform.RotateAround(rpanel.transform.position, new Vector3(0,0,1), -angle);

                                    }
                                }
                                list.RemoveRange(i, count * 2 + 10);
                                
                                break;
                            }
                        }
                    }
                }
            }
        }
        reText.text = recvStr;
    }

源码公开了 ,手头没有G4雷达了,不知道还能不能运行了。 

链接:https://pan.baidu.com/s/189MI81WIpqZMLZKnieQKFw 
提取码:43p6 
复制这段内容后打开百度网盘手机App,操作更方便哦 

 

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Unity 本身不支持直接读取串口数据,需要使用第三方插件或自己编写插件来实现。 以下是使用自己编写的插件来读取串口 16 进制数据的示例: 1. 创建一个 C# 脚本,命名为 SerialPortManager.cs。 2. 在脚本中引入 System.IO.Ports 命名空间和 Unity 的命名空间。 ```csharp using System.IO.Ports; using UnityEngine; ``` 3. 在脚本中创建一个 SerialPort 类型的变量,用于连接串口。 ```csharp private SerialPort serialPort; ``` 4. 在 Awake() 方法中初始化串口连接,设置串口参数。 ```csharp void Awake() { serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One); serialPort.Open(); serialPort.ReadTimeout = 1000; } ``` 5. 在 Update() 方法中读取串口数据。 ```csharp void Update() { if (serialPort.IsOpen) { try { string hexString = serialPort.ReadExisting(); byte[] bytes = StringToByteArray(hexString); // 处理接收到的数据 } catch (System.Exception e) { Debug.LogWarning(e.Message); } } } ``` 6. 创建一个 StringToByteArray() 方法,用于将 16 进制字符串转换为字节数组。 ```csharp private byte[] StringToByteArray(string hexString) { hexString = hexString.Replace(" ", ""); byte[] bytes = new byte[hexString.Length / 2]; for (int i = 0; i < hexString.Length; i += 2) { bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16); } return bytes; } ``` 使用以上方法,就可以实现在 Unity读取串口 16 进制数据了。注意需要在项目设置中开启串口权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值