主要参考了 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,操作更方便哦