/// <summary> /// 接收: 读取待上送扳动过程加速度数据记录(未测试) /// </summary> /// <param name="pkgData"></param> void ReceiveFlipAcceleration(DataPackage pkgData) { object item = null; using (MemoryStream ms = new MemoryStream(pkgData.PureData)) { using (BinaryReader br = new BinaryReader(ms)) { byte cmd2 = br.ReadByte(); byte dataIndex = br.ReadByte(); ushort pakID = br.ReadUInt16(); if (pakID == 0) { //包序号,0x0000 表示信息帧 var frameItem = new ReadRecordInfoItem(); frameItem.CMD2 = cmd2; frameItem.DataIndex = dataIndex; frameItem.PakID = pakID; frameItem.Time.Buffer = br.ReadBytes(7); frameItem.DP_Num = br.ReadUInt32(); frameItem.AcqFrq = br.ReadByte(); frameItem.SW_DIR = br.ReadByte(); //byte[] R = br.ReadBytes(4); item = frameItem; //--------------------------------------------- try { //创建文件,写入文件头数据 curFileNameFA = GetDataPath(string.Format("FA_{0}.dat", DateTime.Now.ToString("yyyyMMdd_HHmmss"))); curStreamFA = new FileStream(curFileNameFA, FileMode.Create, FileAccess.Write); curBinaryWriterFA = new BinaryWriter(curStreamFA); WriteFileHeader(curBinaryWriterFA, frameItem); } catch { } ReadFlipAcceleration(new ReadRecordItem(1)); //开始读数据帧 //--------------------------------------------- } else { //数据帧 var frameItem = new ReadRecordDataItem(); frameItem.CMD2 = cmd2; frameItem.DataIndex = dataIndex; frameItem.PakID = pakID; frameItem.DP_Num = br.ReadUInt16(); frameItem.PakData = br.ReadBytes(frameItem.DP_Num * 3); item = frameItem; //--------------------------------------------- if (pakID == 0xFFFF || frameItem.DP_Num == 0) { //数据帧接收完毕,发送确认帧 try { curBinaryWriterFA.Flush(); curStreamFA.Close(); if (OnDataChanged != null) { //通知文件读取结束 FileHandlerEventArgs eventArgs = new FileHandlerEventArgs(RunMode.FileVA); eventArgs.OperState = OperationState.ReadComplete; eventArgs.fileName = curFileNameFA; OnDataChanged(this, eventArgs); } } catch { } //发送确认信息,结束文件读取 ACK_DataReceive(new DataReceiveAckItem(1, frameItem.DataIndex)); } else { //继续读取下一帧数据 WriteFileData(curBinaryWriterFA, frameItem); //将数据写入文件 ReadFlipAcceleration(new ReadRecordItem(++pakID)); //继续读下一帧数据 } //--------------------------------------------- } } } OnDataChangeAction(pkgData, item); }
/// <summary> /// 写入数据记录的文件头信息(文件头共32字节,剩余位置用0填充) /// </summary> /// <param name="bw"></param> /// <param name="value"></param> void WriteFileHeader(BinaryWriter bw, ReadRecordInfoItem value) { try { bw.Write(value.CMD2); bw.Write(value.DataIndex); bw.Write(value.PakID); bw.Write(value.Time.Buffer); bw.Write(value.DP_Num); bw.Write(value.AcqFrq); bw.Write(value.SW_DIR); bw.Write(value.Oil_Level); bw.Write(value.SW_TIME); bw.Write(new byte[12]); } catch { } }