/// <summary> /// 串口数据接收回调 /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private static void SerialPortInput_MessageReceived(object sender, MessageReceivedEventArgs args) { Console.WriteLine("收到: " + BitConverter.ToString(args.Data)); //获取msgid用来分辨是环境应答还是控制应答 uint msgid = BitConverter.ToUInt16(args.Data, 4); //环境数据应答 if (msgid == 2) { Console.WriteLine("收到环境应答"); DataTemp.EnvData = PortEnvResponse.UnPack(args.Data); DataTemp.DateTime = DateTime.Now; } //控制应答 else if (msgid == 4) { Console.WriteLine("收到控制应答"); DataTemp.portControlResponse = PortControlResponse.Unpack(args.Data); } else { Console.WriteLine($"串口应答msgid为{msgid}, 无法解析"); } }
internal static PortControlResponse Unpack(byte[] receiveData) { PortControlResponse portControlResponse = new PortControlResponse(); portControlResponse.Cmn = BitConverter.ToUInt32(receiveData, 0); portControlResponse.MsgId = BitConverter.ToUInt16(receiveData, 4); portControlResponse.MsgLen = BitConverter.ToUInt16(receiveData, 6); portControlResponse.Success = BitConverter.ToUInt16(receiveData, 8); Console.WriteLine("调试输出"); return(portControlResponse); }