public void uart_rms_data_para(ref byte[] insoure, ref byte[] lefttemp, ref int DataConvertLeftLength) { //============================================B uartdata = new UART_RMS_DATA(); int StructLen = 48; double adconvert = 1.0; DataTable rawData = RawDataTable(); DataRow row1 = rawData.NewRow(); int errorcount = 0; //============================================E //每次最多只做一次显示的转换 //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)); UART_RMS_DATA uartdata = (UART_RMS_DATA)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; MainV2.comPort.MAV.cs.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; MainV2.comPort.MAV.cs.current = BitConverter.ToInt32(tempdata, 0) / 16.0f * 25.0f / 10000; MainV2.comPort.MAV.cs.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(); MainV2.comPort.MAV.cs.lat = latitude_integer / 100 + latitude_integer % 100 / 60.0 + latitude_decimals / 100000.0 / 60.0; 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(); MainV2.comPort.MAV.cs.lng = longitude_integer / 100 + longitude_integer % 100 / 60.0 + longitude_decimals / 100000.0 / 60.0; 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; MainV2.comPort.MAV.cs.alt = BitConverter.ToInt32(tempdata, 0) / 10000.0f; //字节反转 高4位中的最低位晶振同步状态 MainV2.comPort.MAV.cs.ocxo_states = (Byte)((uartdata.states[0] >> 4) & 0x1); MainV2.comPort.MAV.cs.work_states = (Byte)((uartdata.states[0] >> 7)); MainV2.comPort.MAV.cs.ocxo_voltage = ((uartdata.states[0] & 0x0f) * 256) * 5 / 4096.0f; MainV2.comPort.MAV.cs.gps_fix_type = (Byte)(uartdata.hour_gpsstate >> 4); MainV2.comPort.MAV.cs.Hour = (Byte)(uartdata.hour_gpsstate & 0xf); MainV2.comPort.MAV.cs.Minute = (Byte)(uartdata.minute); MainV2.comPort.MAV.cs.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; MainV2.comPort.MAV.cs.Count = BitConverter.ToInt32(tempdata, 0); Array.Clear(tempdata, 0, 4); Array.Copy(uartdata.CH3_X, 0, tempdata, 1, 3); // Array.Reverse(tempdata); MainV2.comPort.MAV.cs.mx = BitConverter.ToInt32(tempdata, 0) * adconvert; Array.Copy(uartdata.CH4_Y, 0, tempdata, 1, 3); // Array.Reverse(tempdata); MainV2.comPort.MAV.cs.my = BitConverter.ToInt32(tempdata, 0) * adconvert; Array.Copy(uartdata.CH5_Z, 0, tempdata, 1, 3); // Array.Reverse(tempdata); MainV2.comPort.MAV.cs.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) { MainV2.comPort.MAV.cs.mag01 = 0; } else { //327680000*(Y+1)/(X+1)/3.498577 (nT)327680000 换为196608000 MainV2.comPort.MAV.cs.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) { MainV2.comPort.MAV.cs.mag02 = 0; } else { MainV2.comPort.MAV.cs.mag02 = 320000000f * (Cxdata + 1) / (Fxdata + 1) / 3.498577f; } //文件存储使用 /* * cqfileline1.Enqueue(ch1data); * */ //更新界面 // this.Invoke(UpdateTextHandler, "UartDataParser"); //===========================================================B //Console.WriteLine(MainV2.comPort.MAV.cs.Count.ToString() + " Sec:" + MainV2.comPort.MAV.cs.Sec.ToString()); //未初始化,不执行 if (fs == 0) { return; } if (GotStartTime) { Nany = (Int64)(N0 + (Int64)MainV2.comPort.MAV.cs.Count - 1); // debug begin //DateTime AnyDateTime = new DateTime(); //int us = 0; //sampleID2DateTime(fs, Nany, ref AnyDateTime, ref us); //UInt64 NanyR = 0; //DateTime2sampleID(fs, AnyDateTime, us, ref NanyR); //Console.Write(MainV2.comPort.MAV.cs.Count.ToString() + " Sec:" + MainV2.comPort.MAV.cs.Sec.ToString() + " Nany:" + Nany.ToString() + " " + AnyDateTime.ToString("yyyy-MM-dd,hh:mm:ss fff") + " us:" + us.ToString() + " N:" + NanyR.ToString()); //Console.WriteLine(MainV2.comPort.MAV.cs.Count.ToString() + " Sec:" + MainV2.comPort.MAV.cs.Sec.ToString()+ " Nany:" + Nany.ToString()); // debug end } else { if (iLastSec >= 0) { // 已经获得之前的秒时间 // if (iLastSec != MainV2.comPort.MAV.cs.Sec && GotStartTime == false) // 如果是变秒的样点。并且尚未获得采集开始时间StartDateTime。 { int n = MainV2.comPort.MAV.cs.Count; int K = (int)Math.Round((n - 1) / fs * 1.0); // 秒脉冲时刻,采集经历的总秒数。 //DateTime NewDateTime = new DateTime(YY, MM, DD, MainV2.comPort.MAV.cs.Hour-7, MainV2.comPort.MAV.cs.Minute-12, MainV2.comPort.MAV.cs.Sec-21, 0); DateTime NewDateTime = new DateTime(YY, MM, DD, MainV2.comPort.MAV.cs.Hour, MainV2.comPort.MAV.cs.Minute, MainV2.comPort.MAV.cs.Sec, 0); StartDateTime = NewDateTime.AddSeconds(-K); TimeSpan ts = StartDateTime - DTref; N0 = (Int64)(ts.TotalSeconds * fs + 1); GotStartTime = true; Nany = (Int64)(N0 + (Int64)MainV2.comPort.MAV.cs.Count - 1); //debug begin //DateTime AnyDateTime = new DateTime(); //int us = 0; //sampleID2DateTime(fs, Nany, ref AnyDateTime, ref us); //UInt64 NanyR = 0; //DateTime2sampleID(fs, AnyDateTime, us, ref NanyR); //Console.Write(MainV2.comPort.MAV.cs.Count.ToString() + " Sec:" + MainV2.comPort.MAV.cs.Sec.ToString() + " Nany:" + Nany.ToString() + " " + AnyDateTime.ToString("yyyy-MM-dd,hh:mm:ss fff") + " us:" + us.ToString() + " N:" + NanyR.ToString()); //Console.WriteLine(MainV2.comPort.MAV.cs.Count.ToString() + " Sec:" + MainV2.comPort.MAV.cs.Sec.ToString()+ " Nany:" + Nany.ToString()); // debug end } } else { iLastSec = MainV2.comPort.MAV.cs.Sec; // 获取这段数据的新1个样点时间秒时间,用于之后确定秒时间的变化点。 Nany = 0; } } // int CheckFlg = -1; if (GotStartTime) { ChcekCXData(MainV2.comPort.MAV.cs.mag01, MainV2.comPort.MAV.cs.mag02, Nany, ref CheckFlg); Console.Write(" CheckFlg: " + CheckFlg.ToString() + " RawMean1: " + CX1rawMean.ToString() + " CheckMean1: " + CX1Mean.ToString() + "\n"); } //===========================================================E double X, Y, Zone; X = 0.0; Y = 0.0; Zone = 0.0; bool ok = WGS84toUTM.LatLonToUTM(MainV2.comPort.MAV.cs.lat, MainV2.comPort.MAV.cs.lng, ref X, ref Y, ref Zone); magdata.Nany = Nany; magdata.CheckFlg = CheckFlg; magdata.Count = MainV2.comPort.MAV.cs.Count; magdata.CX1_data = MainV2.comPort.MAV.cs.mag01; magdata.CX2_data = MainV2.comPort.MAV.cs.mag02; magdata.CX1rawMean = CX1rawMean; magdata.CX1Mean = CX1Mean; magdata.CX2rawMean = CX2rawMean; magdata.CX2Mean = CX2Mean; magdata.mx = MainV2.comPort.MAV.cs.mx; magdata.my = MainV2.comPort.MAV.cs.my; magdata.mz = MainV2.comPort.MAV.cs.mz; magdata.lat = MainV2.comPort.MAV.cs.lat; magdata.lng = MainV2.comPort.MAV.cs.lng; magdata.alt = MainV2.comPort.MAV.cs.alt; magdata.x = X; magdata.y = Y; magdata.zone = Zone; magdata.Hour = MainV2.comPort.MAV.cs.Hour; magdata.Minute = MainV2.comPort.MAV.cs.Minute; magdata.Sec = MainV2.comPort.MAV.cs.Sec; magdata.voltage = MainV2.comPort.MAV.cs.battery_voltage; magdata.current = MainV2.comPort.MAV.cs.current; magdata.work_states = MainV2.comPort.MAV.cs.work_states; magdata.ocxo_states = MainV2.comPort.MAV.cs.ocxo_states; magdata.ocxo_voltage = MainV2.comPort.MAV.cs.ocxo_voltage; magdata.satcount = MainV2.comPort.MAV.cs.satcount; magdata.gps_fix_type = MainV2.comPort.MAV.cs.gps_fix_type; } //长度不符合说明数据有丢失或错误 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); } } }
/// <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); } } }