public void TestSerializeMultiStruct() { var byteStruct = new BytesStruct { a = 7, b = 3 }; Expect.AreArraysEqual(new byte[] { 7, 0, 3 }, SerializableUtil.Serialize(byteStruct)); }
public bool Equals(BytesStruct other) => (Value == null && other.Value == null) || (other.Value != null && Value?.SequenceEqual(other.Value) == true);
/// <summary> /// AA55为数据头 aaaa 20170208换为4个AA /// </summary> /// <param name="insoure">解析数据源</param> /// <param name="lefttemp"> 剩余部分</param> public void uart_rms_data_para(ref byte[] insoure, ref byte[] lefttemp) { //每次最多只做一次显示的转换 //int StructLen = Marshal.SizeOf(typeof(UART_RMS_DATA)); int TempDateSize = insoure.Length; int index1 = -1; int index2 = -1; //匹配分析的起始位置点 int searchindex = 0; bool findflag = false; while (searchindex + StructLen <= TempDateSize) { //有一个完整的结构体 if (TempDateSize >= StructLen) { //SIZE -1 后面有+1 不能越界 //第一个aaaa findflag = false; for (int i = searchindex; i < TempDateSize - 3; i++) { if (insoure[i] == 0xAA && insoure[i + 1] == 0xAA && insoure[i + 2] == 0x55 && insoure[i + 3] == 0x55) { index1 = i; findflag = true; break; } } //没有找到头直接结束 if (findflag == false) { searchindex = 0; break; } //够解析一个吗? if (TempDateSize - index1 < StructLen) { searchindex = 0; break; } //第二个aaaa //SIZE -1 后面有+1 不能越界 for (int i = index1 + 4; i < TempDateSize - 3; i++) { if (insoure[i] == 0xAA && insoure[i + 1] == 0xAA && insoure[i + 2] == 0x55 && insoure[i + 3] == 0x55) { index2 = i; break; } } if (index2 == -1) { searchindex = index1; //没有找到第二个 break; } if (index2 == index1) { searchindex = index1; break; } if (index2 > index1) { searchindex = index2; } if (index2 - index1 == StructLen) { UART_RMS_DATA uartdata = (UART_RMS_DATA)BytesStruct.BytesToStuct(insoure, index1, typeof(UART_RMS_DATA)); int Fxdata, Cxdata = 0; //24BIT 加8位变成32位,符号位就自动升到高位了 byte[] tempdata = new byte[4]; //tempdata[0] = 0; // tempdata[1] = 0; Array.Clear(tempdata, 0, 4); Array.Copy(uartdata.voltage, 0, tempdata, 2, 2); Array.Reverse(tempdata); //voltageshow = BitConverter.ToInt32(tempdata, 0) * 25.0f /1000/ 16.0f; boxstate.battery_voltage = BitConverter.ToInt32(tempdata, 0) * 25.0f / 1000 / 16.0f; //tempdata[0] = 0; //tempdata[1] = 0; Array.Clear(tempdata, 0, 2); Array.Copy(uartdata.current, 0, tempdata, 2, 2); Array.Reverse(tempdata); //currentshow = BitConverter.ToInt32(tempdata, 0)/16.0f * 25.0f /10000; boxstate.current = BitConverter.ToInt32(tempdata, 0) / 16.0f * 25.0f / 10000; boxstate.satcount = (Byte)(BitConverter.ToInt32(tempdata, 0) & 0x0f); Array.Clear(tempdata, 0, 4); Array.Copy(uartdata.latitude_integer, 0, tempdata, 2, 2); Array.Reverse(tempdata); //voltageshow = BitConverter.ToInt32(tempdata, 0) * 25.0f /1000/ 16.0f; int latitude_integer = BitConverter.ToInt32(tempdata, 0); Array.Clear(tempdata, 0, 4); Array.Copy(uartdata.latitude_decimals, 0, tempdata, 1, 3); Array.Reverse(tempdata); //voltageshow = BitConverter.ToInt32(tempdata, 0) * 25.0f /1000/ 16.0f; int latitude_decimals = BitConverter.ToInt32(tempdata, 0); string latstring = latitude_integer.ToString() + "." + latitude_decimals.ToString(); boxstate.lat = Convert.ToDouble(latstring); Array.Clear(tempdata, 0, 4); Array.Copy(uartdata.longitude_integer, 0, tempdata, 2, 2); Array.Reverse(tempdata); //voltageshow = BitConverter.ToInt32(tempdata, 0) * 25.0f /1000/ 16.0f; int longitude_integer = BitConverter.ToInt32(tempdata, 0); Array.Clear(tempdata, 0, 4); Array.Copy(uartdata.longitude_decimals, 0, tempdata, 1, 3); Array.Reverse(tempdata); //voltageshow = BitConverter.ToInt32(tempdata, 0) * 25.0f /1000/ 16.0f; int longitude_decimals = BitConverter.ToInt32(tempdata, 0); string lonstring = longitude_integer.ToString() + "." + longitude_decimals.ToString(); boxstate.lng = Convert.ToDouble(lonstring); Array.Clear(tempdata, 0, 4); Array.Copy(uartdata.alt, 0, tempdata, 1, 3); Array.Reverse(tempdata); //currentshow = BitConverter.ToInt32(tempdata, 0)/16.0f * 25.0f /10000; boxstate.alt = BitConverter.ToInt32(tempdata, 0) / 10000.0f; //字节反转 高4位中的最低位晶振同步状态 boxstate.ocxo_states = (Byte)((uartdata.states[0] >> 4) & 0x1); boxstate.work_states = (Byte)((uartdata.states[0] >> 7)); boxstate.ocxo_voltage = ((uartdata.states[0] & 0x0f) * 256) * 5 / 4096.0f; boxstate.gpsstatus = (Byte)(uartdata.hour_gpsstate >> 4); boxstate.Hour = (Byte)(uartdata.hour_gpsstate & 0xf); boxstate.Minute = (Byte)(uartdata.minute); boxstate.Sec = (Byte)uartdata.sec; Array.Clear(tempdata, 0, 4); Array.Copy(uartdata.count, 0, tempdata, 1, 3); Array.Reverse(tempdata); //voltageshow = BitConverter.ToInt32(tempdata, 0) * 25.0f /1000/ 16.0f; boxstate.Count = BitConverter.ToInt32(tempdata, 0); Array.Clear(tempdata, 0, 4); Array.Copy(uartdata.CH3_X, 0, tempdata, 1, 3); // Array.Reverse(tempdata); boxstate.mx = BitConverter.ToInt32(tempdata, 0) * adconvert; Array.Copy(uartdata.CH4_Y, 0, tempdata, 1, 3); // Array.Reverse(tempdata); boxstate.my = BitConverter.ToInt32(tempdata, 0) * adconvert; Array.Copy(uartdata.CH5_Z, 0, tempdata, 1, 3); // Array.Reverse(tempdata); boxstate.mz = BitConverter.ToInt32(tempdata, 0) * adconvert; //xyzdata = 20.0 * Math.Sqrt(ch3_x_data * ch3_x_data + ch4_y_data * ch4_y_data + ch5_z_data * ch5_z_data); Cxdata = uartdata.CH1_X[1] + uartdata.CH1_X[0] * 256; Fxdata = uartdata.CH1_Y[2] + uartdata.CH1_Y[1] * 256 + uartdata.CH1_Y[0] * 65536; if (Fxdata == 0 || Cxdata == 0) { boxstate.mag01 = 0; } else { //327680000*(Y+1)/(X+1)/3.498577 (nT)327680000 换为196608000 boxstate.mag01 = 320000000f * (Cxdata + 1) / (Fxdata + 1) / 3.498577f; } Fxdata = uartdata.CH2_Y[2] + uartdata.CH2_Y[1] * 256 + uartdata.CH2_Y[0] * 65536; Cxdata = uartdata.CH2_X[1] + uartdata.CH2_X[0] * 256; if (Fxdata == 0 || Cxdata == 0) { boxstate.mag02 = 0; } else { boxstate.mag02 = 320000000f * (Cxdata + 1) / (Fxdata + 1) / 3.498577f; } //327680000*(Y+1)/(X+1)/3.498577 (nT)327680000 换为196608000 //ch3_x_Showline.Enqueue(ch3_x_data); //ch4_y_Showline.Enqueue(ch4_y_data); //ch5_z_Showline.Enqueue(ch5_z_data); //cqShowline24.Enqueue(ch1_optical_pump_data); //cqShowline25.Enqueue(ch2_optical_pump_data); //文件存储使用 /* * cqfileline1.Enqueue(ch1data); * cqfileline2.Enqueue(ch2data); * cqfileline3.Enqueue(ch3data); * cqfileline24.Enqueue(optical_pump_data1); * cqfileline25.Enqueue(optical_pump_data2); * cqfilelinetime.Enqueue(uart_gps_state); * * cqfileline4.Enqueue(ch4data); * cqfileline5.Enqueue(ch5data); * cqfileline6.Enqueue(ch6data); * * cqfileline7.Enqueue(ch7data); * cqfileline8.Enqueue(ch8data); * cqfileline9.Enqueue(ch9data); * */ //更新界面 // this.Invoke(UpdateTextHandler, "UartDataParser"); } //长度不符合说明数据有丢失或错误 else { searchindex = index2; errorcount++; } } else { errorcount++; break; } } //拷贝剩余 if ((TempDateSize - searchindex > 0)) { DataConvertLeftLength = TempDateSize - searchindex; //大小不对 数据有错误 if (DataConvertLeftLength > lefttemp.Length) { DataConvertLeftLength = 0; errorcount++; //MessageBox.Show(DataConvertLeftLength.ToString()); } else { //残留部分拷贝 Array.Copy(insoure, searchindex, lefttemp, 0, DataConvertLeftLength); } } }