private static object State_TransData(TransData_Struct data) { State_TransData_refData recv = new State_TransData_refData(); if (data.Data == null) { return(recv); } if (data.Data.Length <= 9) { return(recv); } recv.Start_Index = ((uint)data.Data[0] << 24); recv.Start_Index |= ((uint)data.Data[1] << 16); recv.Start_Index |= ((uint)data.Data[2] << 8); recv.Start_Index |= ((uint)data.Data[3]); DateTime time = new DateTime(data.Data[4] + 1980, data.Data[5], data.Data[6], data.Data[7], data.Data[8], data.Data[9]); recv.Init_Time = time; recv.wav_data = new byte[data.Data.Length - 12]; Buffer.BlockCopy(data.Data, 12, recv.wav_data, 0, data.Data.Length - 12); return(recv); }
public static byte[] Construct_Data_Packet(TransData_Struct stu) { int sum = 0; byte[] data = new byte[Dic_Protocol[stu.Keyword].State_string.Length + stu.Data.Length + stu.Device_ID.Length + 8]; byte[] strdata = Encoding.Default.GetBytes(Dic_Protocol[stu.Keyword].State_string); for (int i = 0; i < Dic_Protocol[stu.Keyword].State_string.Length; i++) { sum += strdata[i]; data[i] = strdata[i]; } for (int i = 0; i < stu.Device_ID.Length; i++) { sum += stu.Device_ID[i]; data[i + Dic_Protocol[stu.Keyword].State_string.Length + 8] = stu.Device_ID[i]; } for (int i = 0; i < stu.Data.Length; i++) { sum += stu.Data[i]; data[i + 8 + stu.Device_ID.Length + Dic_Protocol[stu.Keyword].State_string.Length] = stu.Data[i]; } data[Dic_Protocol[stu.Keyword].State_string.Length] = (byte)(stu.Data.Length >> 24); data[Dic_Protocol[stu.Keyword].State_string.Length + 1] = (byte)(stu.Data.Length >> 16); data[Dic_Protocol[stu.Keyword].State_string.Length + 2] = (byte)(stu.Data.Length >> 8); data[Dic_Protocol[stu.Keyword].State_string.Length + 3] = (byte)(stu.Data.Length); data[Dic_Protocol[stu.Keyword].State_string.Length + 4] = (byte)(sum >> 24); data[Dic_Protocol[stu.Keyword].State_string.Length + 5] = (byte)(sum >> 12); data[Dic_Protocol[stu.Keyword].State_string.Length + 6] = (byte)(sum >> 8); data[Dic_Protocol[stu.Keyword].State_string.Length + 7] = (byte)(sum); return(data); }
private static object State_Idle(TransData_Struct data) { State_Idle_refData recv = new State_Idle_refData(); recv.State = data.Data[0]; recv.Binding_User = gb2312.GetString(data.Data.Skip(1).ToArray()); return(recv); }
public static object State_Response_Function(TransData_Struct stu) { foreach (var item in Dic_Protocol) { if (stu.Keyword == item.Key) { return(item.Value.Function_Event(stu)); } } return(null); }
private static object State_Bingding_Check(TransData_Struct data) { State_Bingding_Check_refData recv = new State_Bingding_Check_refData(); if (data.Data == null) { recv.Binding_User = ""; } else { recv.Binding_User = gb2312.GetString(data.Data); } return(recv); }
private static object State_FixDataRequest(TransData_Struct data) { State_FixDataRequest_refData recv = new State_FixDataRequest_refData(); return(recv); }
private static object State_EndRecording(TransData_Struct data) { State_ReadyToStartRecoder_refData recv = new State_ReadyToStartRecoder_refData(); return(recv); }
private static object State_DeviceInfo(TransData_Struct data) { State_DeviceInfo_refData recv = new State_DeviceInfo_refData(); return(recv); }
private static object State_Binding_Check_Confirm(TransData_Struct data) { State_Binding_Check_Confirm_refData recv = new State_Binding_Check_Confirm_refData(); return(recv); }
public static void AnalysisRawData(ref byte[] srcdata, ref List <TransData_Struct> RecvArray) { bool isSearch = true; while (isSearch) { //string str = System.Text.Encoding.ASCII.GetString(srcdata); //str = str.Replace('\0', '*'); isSearch = false; byte[][] search_list = new byte[Dic_Protocol.Count][]; for (int i = 0; i < search_list.Length; i++) { search_list[i] = Encoding.ASCII.GetBytes(Dic_Protocol[(Protocol_Keyword)i].State_string); } List <int>[] recvfindlist = Search_ByteArray_String(ref srcdata, ref search_list); for (int i = 0; i < recvfindlist.Length; i++) { for (int j = 0; j < recvfindlist[i].Count; j++) { int commLoc = recvfindlist[i][j]; if ((commLoc + search_list[i].Length + 4 + 4) >= srcdata.Length) { for (int k = 0; k < search_list[i].Length; k++) { srcdata[commLoc + k] = 0; } break; } int recvdatalen = srcdata[commLoc + search_list[i].Length] << 24; recvdatalen |= srcdata[commLoc + search_list[i].Length + 1] << 16; recvdatalen |= srcdata[commLoc + search_list[i].Length + 2] << 8; recvdatalen |= srcdata[commLoc + search_list[i].Length + 3]; int recvdatacheck = srcdata[commLoc + search_list[i].Length + 4] << 24; recvdatacheck |= srcdata[commLoc + search_list[i].Length + 4 + 1] << 16; recvdatacheck |= srcdata[commLoc + search_list[i].Length + 4 + 2] << 8; recvdatacheck |= srcdata[commLoc + search_list[i].Length + 4 + 3]; int recvsum = 0; for (int k = 0; k < search_list[i].Length; k++) { recvsum += search_list[i][k]; } if (recvdatalen > 1024 * 20 || (commLoc + search_list[i].Length + 4 + 4 + recvdatalen + 12) >= srcdata.Length) { for (int k = 0; k < search_list[i].Length; k++) { srcdata[commLoc + k] = 0; } break; } for (int k = 0; k < recvdatalen + 12; k++) { recvsum += srcdata[commLoc + search_list[i].Length + 4 + 4 + k]; } if (recvsum == recvdatacheck) { byte[] deviceid = new byte[12]; byte[] data = null; Array.Copy(srcdata, commLoc + search_list[i].Length + 4 + 4, deviceid, 0, 12); if (recvdatalen > 0) { data = new byte[recvdatalen]; Array.Copy(srcdata, commLoc + search_list[i].Length + 4 + 4 + 12, data, 0, recvdatalen); } TransData_Struct recv = new TransData_Struct { Keyword = (Protocol_Keyword)i, Device_ID = deviceid, Data = data }; RecvArray.Add(recv); for (int k = 0; k < search_list[i].Length; k++) { srcdata[commLoc + k] = 0; } deviceid = null; data = null; isSearch = true; break; } else { if (recvfindlist[i].Count - 1 != j) { for (int k = 0; k < search_list[i].Length; k++) { srcdata[commLoc + k] = 0; } } } } } } }
public static void AnalysisRawData_2(ref byte[] srcdata, ref List <TransData_Struct> RecvArray) { bool isSearch = true; while (isSearch) { //string str = System.Text.Encoding.ASCII.GetString(srcdata); //str = str.Replace('\0', '*'); isSearch = false; foreach (var item in Dic_Protocol) { //int commLoc = str.IndexOf(item.Value.State_string); bool isonly = true; byte[] strdata = Encoding.Default.GetBytes(item.Value.State_string); int commLoc = Search_ByteArray_String(ref srcdata, ref strdata, ref isonly); if (commLoc >= 0) { if ((commLoc + item.Value.State_string.Length + 4 + 4) >= srcdata.Length) { for (int i = 0; i < strdata.Length; i++) { srcdata[commLoc + i] = 0; } break; } int recvdatalen = srcdata[commLoc + item.Value.State_string.Length] << 24; recvdatalen |= srcdata[commLoc + item.Value.State_string.Length + 1] << 16; recvdatalen |= srcdata[commLoc + item.Value.State_string.Length + 2] << 8; recvdatalen |= srcdata[commLoc + item.Value.State_string.Length + 3]; int recvdatacheck = srcdata[commLoc + item.Value.State_string.Length + 4] << 24; recvdatacheck |= srcdata[commLoc + item.Value.State_string.Length + 4 + 1] << 16; recvdatacheck |= srcdata[commLoc + item.Value.State_string.Length + 4 + 2] << 8; recvdatacheck |= srcdata[commLoc + item.Value.State_string.Length + 4 + 3]; int recvsum = 0; for (int i = 0; i < strdata.Length; i++) { recvsum += strdata[i]; } if (recvdatalen > 1024 * 20 || (commLoc + item.Value.State_string.Length + 4 + 4 + recvdatalen + 8) >= srcdata.Length) { for (int i = 0; i < strdata.Length; i++) { srcdata[commLoc + i] = 0; } break; } for (int i = 0; i < recvdatalen + 8; i++) { recvsum += srcdata[commLoc + item.Value.State_string.Length + 4 + 4 + i]; } if (recvsum == recvdatacheck) { byte[] deviceid = new byte[12]; byte[] data = null; Array.Copy(srcdata, commLoc + item.Value.State_string.Length + 4 + 4, deviceid, 0, 12); if (recvdatalen > 0) { data = new byte[recvdatalen]; Array.Copy(srcdata, commLoc + item.Value.State_string.Length + 4 + 4 + 12, data, 0, recvdatalen); } TransData_Struct recv = new TransData_Struct { Keyword = item.Key, Device_ID = deviceid, Data = data }; RecvArray.Add(recv); for (int i = 0; i < strdata.Length; i++) { srcdata[commLoc + i] = 0; } deviceid = null; data = null; isSearch = true; break; } else { if (isonly == false) { for (int i = 0; i < strdata.Length; i++) { srcdata[commLoc + i] = 0; } } } //for (int i = 0; i < strdata.Length; i++) // srcdata[commLoc + i] = 0; strdata = null; //if (recvsum == recvdatacheck) //{ // isSearch = true; // break; //} } } } }