Queue <byte[]> dataIsBeacon = new Queue <byte[]>(); //完整的信标的数据包 private void dealData() { byte res = 0; while (dataQueue.Any()) { if ((dataLength & 0X2000) != 0) //接收到包头 { if ((dataLength & 0x8000) == 0) //接收未完成 { if ((dataLength & 0x4000) != 0) //接收到包尾 { dataLength |= 0X8000; //接收完成 } else { res = dataQueue.Dequeue(); if (res == 0XFE) { allbag = true; } else if (allbag == true) { if (res == 0X01) { dataLength |= 0X4000; allbag = false; } else { allbag = false; } } lenByte[dataLength & 0X1FFF] = res; dataLength++; if (dataLength > (199 + 0X6000)) { dataLength = 0; //接收错误重新开始 } } } else//接收完成 { int middleLength = (dataLength & 0X1FFF) - 2; //TODO:判断数据的完整性 byte[] vs = new byte[middleLength]; Array.Copy(lenByte, vs, middleLength); DataIsAll dataIsAll = new DataIsAll(vs); this.Invoke(new Action(() => { textBox1.AppendText($"{dataIsAll.carAddress}" + "号车-->"); textBox1.AppendText($"经度:{dataIsAll.longitude}" + " "); textBox1.AppendText($"纬度:{dataIsAll.latitude}" + " "); textBox1.AppendText("\r\n"); })); if (vs[18] == 0X01) { allCarNum[lastNum] = false; sendByte = vs; nowNum = (sendByte[0] << 8) + sendByte[1]; allCarNum[nowNum] = true; lastNum = nowNum; } else if (vs[18] == 0X00) { dataIsBeacon.Enqueue(vs); } dataLength = 0; } } else { res = dataQueue.Dequeue(); if (res == 0X01) { allbag = true; //接收到包头(0X01和0XFE) } else if (allbag == true) { if (res == 0XFE) { dataLength |= 0X2000; allbag = false; } else { allbag = false; } } } } }
MyBeacon myBeacon4; //信标4 private void dealData() { byte res = 0; while (dataQueue.Any()) { if ((dataLength & 0X2000) != 0) //接收到包头 { if ((dataLength & 0x8000) == 0) //接收未完成 { if ((dataLength & 0x4000) != 0) //接收到包尾 { dataLength |= 0X8000; //接收完成 } else { res = dataQueue.Dequeue(); if (res == 0XFE) { allbag = true; } else if (allbag == true) { if (res == 0X01) { dataLength |= 0X4000; allbag = false; } else { allbag = false; } } lenByte[dataLength & 0X1FFF] = res; dataLength++; if (dataLength > (199 + 0X6000)) { dataLength = 0; //接收错误重新开始 } } } else//接收完成 { int middleLength = (dataLength & 0X1FFF) - 2; //TODO:判断数据的完整性 byte[] vsAndDestination = new byte[middleLength + 33];//获取包含距离的数据 Array.Copy(lenByte, vsAndDestination, middleLength); DataIsAll dataIsAll = new DataIsAll(vsAndDestination); if (vsAndDestination[18] == 0X01)//车子的坐标 { this.Invoke(new Action(() => { textBox1.AppendText($"{dataIsAll.carAddress}" + "号车-->"); textBox1.AppendText($"经度:{dataIsAll.longitude}" + " "); textBox1.AppendText($"纬度:{dataIsAll.latitude}" + " "); textBox1.AppendText("\r\n"); })); allCarNum[lastNum] = false; //获得距离信标的位置 byte[] distance1 = GetDistance(dataIsAll.latitude, dataIsAll.longitude, myBeacon1.beaconLatitude, myBeacon1.beaconLongitude); byte[] distance2 = GetDistance(dataIsAll.latitude, dataIsAll.longitude, myBeacon2.beaconLatitude, myBeacon2.beaconLongitude); byte[] distance3 = GetDistance(dataIsAll.latitude, dataIsAll.longitude, myBeacon3.beaconLatitude, myBeacon3.beaconLongitude); byte[] distance4 = GetDistance(dataIsAll.latitude, dataIsAll.longitude, myBeacon3.beaconLatitude, myBeacon3.beaconLongitude); Array.Copy(distance1, 0, vsAndDestination, middleLength, 8); if (radioButton1.Checked) { vsAndDestination[middleLength + 32] = 0x00; } else if (radioButton2.Checked) { //获取各个信标的位置信息 Array.Copy(distance2, 0, vsAndDestination, middleLength + 8, 8); Array.Copy(distance3, 0, vsAndDestination, middleLength + 16, 8); Array.Copy(distance4, 0, vsAndDestination, middleLength + 24, 8); vsAndDestination[middleLength + 32] = 0x01; } sendByte = vsAndDestination; nowNum = (sendByte[0] << 8) + sendByte[1]; allCarNum[nowNum] = true; lastNum = nowNum; } else if (vsAndDestination[18] == 0X00)//信标的坐标 { if (dataIsAll.carAddress == 1) { myBeacon1.beaconLatitude = dataIsAll.latitude; myBeacon1.beaconLongitude = dataIsAll.longitude; myBeacon1.beaconLocation = dataIsAll.carAddress; this.Invoke(new Action(() => { beaconLatitude1.Text = myBeacon1.beaconLatitude.ToString(); beaconLongitude1.Text = myBeacon1.beaconLongitude.ToString(); })); } else if (dataIsAll.carAddress == 2) { myBeacon2.beaconLatitude = dataIsAll.latitude; myBeacon2.beaconLongitude = dataIsAll.longitude; myBeacon2.beaconLocation = dataIsAll.carAddress; this.Invoke(new Action(() => { beaconLatitude2.Text = myBeacon2.beaconLatitude.ToString(); beaconLongitude2.Text = myBeacon2.beaconLongitude.ToString(); })); } else if (dataIsAll.carAddress == 3) { myBeacon3.beaconLatitude = dataIsAll.latitude; myBeacon3.beaconLongitude = dataIsAll.longitude; myBeacon3.beaconLocation = dataIsAll.carAddress; this.Invoke(new Action(() => { beaconLatitude3.Text = myBeacon3.beaconLatitude.ToString(); beaconLongitude3.Text = myBeacon3.beaconLongitude.ToString(); })); } else if (dataIsAll.carAddress == 4) { myBeacon4.beaconLatitude = dataIsAll.latitude; myBeacon4.beaconLongitude = dataIsAll.longitude; myBeacon4.beaconLocation = dataIsAll.carAddress; this.Invoke(new Action(() => { beaconLatitude4.Text = myBeacon4.beaconLatitude.ToString(); beaconLongitude4.Text = myBeacon4.beaconLongitude.ToString(); })); } } dataLength = 0; } } else { res = dataQueue.Dequeue(); if (res == 0X01) { allbag = true; //接收到包头(0X01和0XFE) } else if (allbag == true) { if (res == 0XFE) { dataLength |= 0X2000; allbag = false; } else { allbag = false; } } } } }