private byte[] BuildPacket(double x, double y, double z, double vx, double vy, double vz, int time) { PACK_HEAD packHeader = new PACK_HEAD() { Station = 0x21, Type = 0x20 }; S_HEAD sHead = new S_HEAD() { Len = (ushort)(Marshal.SizeOf(typeof(S_HEAD)) + Marshal.SizeOf(typeof(S_OBJECT))), Time = time, }; S_OBJECT sObject = new S_OBJECT { X = x, Y = y, Z = z, VX = vx, VY = vy, VZ = vz }; int len = Marshal.SizeOf(typeof(PACK_HEAD)) + sHead.Len; byte[] buffer = new byte[len]; MemoryStream memoryStream = new MemoryStream(buffer); BinaryWriter binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(StructToBytes(packHeader)); binaryWriter.Write(StructToBytes(sHead)); binaryWriter.Write(StructToBytes(sObject)); return(buffer); }
private void btnSendRadarFile_Click(object sender, EventArgs e) { binaryBufferList.Clear(); if (!isLoopSend) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "二进制文件|*.bin"; if (openFileDialog.ShowDialog() == DialogResult.OK) { String fileName = openFileDialog.FileName; using (FileStream fileStream = File.Open(fileName, FileMode.Open)) { BinaryReader reader = new BinaryReader(fileStream); while (reader.BaseStream.Position < reader.BaseStream.Length) { byte[] packHead = reader.ReadBytes(Marshal.SizeOf(typeof(PACK_HEAD))); byte[] s_head = reader.ReadBytes(Marshal.SizeOf(typeof(S_HEAD))); int size = Marshal.SizeOf(typeof(S_HEAD)); IntPtr buffer = Marshal.AllocHGlobal(size); Marshal.Copy(s_head, 0, buffer, size); S_HEAD sHead = (S_HEAD)Marshal.PtrToStructure(buffer, typeof(S_HEAD)); Marshal.FreeHGlobal(buffer); int bodyLen = sHead.Len; byte[] sObjects = reader.ReadBytes(bodyLen - Marshal.SizeOf(typeof(S_HEAD))); binaryBufferList.Add(packHead.Concat(s_head).Concat(sObjects).ToArray()); } } btnSendRadarFile.Text = "停止"; isLoopSend = true; timerBin.Interval = int.Parse(editInterval.Text); timerBin.Start(); btnSendOne.Enabled = false; btnSendLoop.Enabled = false; } } else { timerBin.Stop(); btnSendRadarFile.Text = "发送二进制雷测文件"; isLoopSend = false; btnSendOne.Enabled = true; btnSendLoop.Enabled = true; } }
private byte[] BuildT0() { PACK_HEAD packHeader = new PACK_HEAD() { Station = 0x21, Type = 0x50 }; S_HEAD sHead = new S_HEAD() { Len = (ushort)(Marshal.SizeOf(typeof(S_HEAD))), Time = (int)DateTime.Now.TimeOfDay.TotalMilliseconds, }; int len = Marshal.SizeOf(typeof(PACK_HEAD)) + sHead.Len; byte[] buffer = new byte[len]; MemoryStream memoryStream = new MemoryStream(buffer); BinaryWriter binaryWriter = new BinaryWriter(memoryStream); binaryWriter.Write(StructToBytes(packHeader)); binaryWriter.Write(StructToBytes(sHead)); return(buffer); }