public static byte[] SendCommand_68_serialtest(byte[] data, byte dataLength, string nodeMac, int frameNum, bool numflag) { SerialPortFrame frame = new SerialPortFrame(data, dataLength); frame.NodeMac = nodeMac; frame.FrameCount = (ushort)frameNum; //固定帧序号 if (numflag) { frame.FrameCount = (ushort)6; } byte[] serialPortFrame = frame.Send();//形成串口通信帧 /* 构建网络通信帧 */ NetCommunicationFrame netFrame = new NetCommunicationFrame(serialPortFrame, Convert.ToByte(serialPortFrame.Length)); netFrame.TransferDirection = NetCommunicationFrame.Direction.Computer2Gateway; netFrame.FrameCount = (ushort)frameNum; //固定帧序号 if (numflag) { netFrame.FrameCount = (ushort)6; } return(netFrame.Send()); }
/// <summary> /// RSSI读取,68帧内加入了帧序号,用于区分重复帧 /// </summary> /// <param name="frameNum">帧序号</param> /// <param name="destinationMac">最终目的设备的MAC</param> /// <param name="length">命令总长度</param> /// <param name="commandID">命令标识</param> /// <param name="commandPayload">命令负载</param> /// <returns></returns> public static byte[] SendCommand_68_ToNode(int frameNum, string destinationMac, byte length, byte[] commandID, byte[] commandPayload) { #region 构建68帧 SerialPortFrame frame = new SerialPortFrame(commandID, commandPayload, length); frame.ConcentratorMac = ConcentratorMac; //AnalysisProcess.Node_frame = frameNum; //frame.FrameCount = (ushort)AnalysisProcess.Node_frame; frame.FrameCount = (ushort)frameNum; if (destinationMac == ConcentratorMac)//目标是集中器 { frame.NodeMac = "0000"; frame.CommunicationWay = SerialPortFrame.Communication.InnerNet; } else//目标是节点 { frame.NodeMac = destinationMac; frame.CommunicationWay = SerialPortFrame.Communication.Nontransparent; } //frame.NeedAnswer = true; byte[] serialPortFrame = frame.Send();//形成串口通信帧 #endregion #region 构建网络通信帧 /* 构建网络通信帧 */ NetCommunicationFrame netFrame = new NetCommunicationFrame(serialPortFrame, Convert.ToByte(serialPortFrame.Length)); netFrame.Encryption = false; netFrame.NeedAnswer = false; netFrame.FrameFormat = NetCommunicationFrame.FrameType.DataFrame; receiveByte(); netFrame.MobilePhoneMAC = PhoneMAC; netFrame.GatewayClassification = GatewayType; netFrame.GatewayMAC = GatewayMAC; netFrame.FrameCount = 0; if (packetFrameType == FrameType.OutterNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.MobilePhone2Server2Gateway; netFrame.ServerIP = SeverIp; } if (packetFrameType == FrameType.InnerNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Mobile2Gateway; } if (packetFrameType == FrameType.Serial) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Computer2Gateway; } return(netFrame.Send()); #endregion }
//网关更新,用于发送中间的数据帧 public static byte[] SendCommand_68_SendGatewayData(bool needACK, int frame_position, byte[] dataPayload, byte length, ushort Framecount) { #region 构建串口帧 SerialPortFrame frame = new SerialPortFrame(dataPayload, Convert.ToByte(dataPayload.Length)); if (frame_position == 0) { frame.TransferState = SerialPortFrame.FrameState.FirstFrame; } else if (frame_position == 1) { frame.TransferState = SerialPortFrame.FrameState.MidFrame; } else if (frame_position == 2) { frame.TransferState = SerialPortFrame.FrameState.LastFrame; } frame.NeedAnswer = needACK; frameNum = Framecount; frame.FrameCount = Framecount; frame.CommunicationWay = SerialPortFrame.Communication.InnerNet; byte[] intranetFrame = frame.Send();//形成串口通信帧 #endregion #region 构建网络通信帧 /* 构建网络通信帧 */ NetCommunicationFrame netFrame = new NetCommunicationFrame(intranetFrame, Convert.ToByte(intranetFrame.Length)); netFrame.Encryption = false; netFrame.NeedAnswer = false; netFrame.FrameFormat = NetCommunicationFrame.FrameType.DataFrame; receiveByte(); netFrame.MobilePhoneMAC = PhoneMAC; netFrame.GatewayClassification = GatewayType; netFrame.GatewayMAC = GatewayMAC; netFrame.FrameCount = 0; if (packetFrameType == FrameType.OutterNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.MobilePhone2Server2Gateway; netFrame.ServerIP = SeverIp; } if (packetFrameType == FrameType.InnerNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Mobile2Gateway; } if (packetFrameType == FrameType.Serial) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Computer2Gateway; } return(netFrame.Send()); #endregion }
/// <summary> /// 速率测试中,用于发送数据帧 /// </summary> /// <param name="destinationMac"></param> /// <param name="needACK"></param> /// <param name="mode"></param> /// <param name="state"></param> /// <param name="count"></param> /// <param name="dataPayload"></param> /// <returns></returns> public static byte[] SendCommand_68_SeriData(string destinationMac, bool needACK, SerialPortFrame.Communication mode, SerialPortFrame.FrameState state, ushort count, byte[] dataPayload) { #region 构建串口帧 SerialPortFrame frame = new SerialPortFrame(dataPayload, Convert.ToByte(dataPayload.Length)); frame.ConcentratorMac = ConcentratorMac; if (destinationMac == ConcentratorMac)//目标是集中器 { frame.NodeMac = "0000"; frame.CommunicationWay = SerialPortFrame.Communication.InnerNet; } else//目标是节点 { frame.NodeMac = destinationMac; frame.CommunicationWay = mode; } frame.TransferState = state; frame.NeedAnswer = needACK; frame.FrameCount = count; byte[] serialPortFrame = frame.Send();//形成串口通信帧 #endregion #region 构建网络通信帧 /* 构建网络通信帧 */ NetCommunicationFrame netFrame = new NetCommunicationFrame(serialPortFrame, Convert.ToByte(serialPortFrame.Length)); netFrame.Encryption = false; netFrame.NeedAnswer = false; netFrame.FrameFormat = NetCommunicationFrame.FrameType.DataFrame; receiveByte(); netFrame.MobilePhoneMAC = PhoneMAC; netFrame.GatewayClassification = GatewayType; netFrame.GatewayMAC = GatewayMAC; netFrame.FrameCount = 0; if (packetFrameType == FrameType.OutterNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.MobilePhone2Server2Gateway; netFrame.ServerIP = SeverIp; } if (packetFrameType == FrameType.InnerNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Mobile2Gateway; } if (packetFrameType == FrameType.Serial) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Computer2Gateway; } return(netFrame.Send()); #endregion }
/// <summary> /// 发送最终目的地是节点的命令帧. /// </summary> /// <param name="destinationMac">最终目的设备的MAC.</param> /// <param name="length">命令总长度.</param> /// <param name="commandID">命令标识.</param> /// <param name="commandPayload">命令负载.</param> /// <returns></returns> public static byte[] SendCommand_68_ToNode(string destinationMac, byte length, byte[] commandID, byte[] commandPayload) { #region 构建68帧 SerialPortFrame frame = new SerialPortFrame(commandID, commandPayload, length); frame.ConcentratorMac = ConcentratorMac; if (destinationMac.Length > 4) { frame.NodeMac = destinationMac.Substring(destinationMac.Length - 4, 4); } else { frame.NodeMac = destinationMac; } frame.CommunicationWay = SerialPortFrame.Communication.Nontransparent; byte[] serialPortFrame = frame.Send();//形成串口通信帧 #endregion #region 构建网络通信帧 /* 构建网络通信帧 */ NetCommunicationFrame netFrame = new NetCommunicationFrame(serialPortFrame, Convert.ToByte(serialPortFrame.Length)); netFrame.Encryption = false; netFrame.NeedAnswer = false; netFrame.FrameFormat = NetCommunicationFrame.FrameType.DataFrame; receiveByte(); netFrame.MobilePhoneMAC = PhoneMAC; netFrame.GatewayClassification = GatewayType; netFrame.GatewayMAC = GatewayMAC; netFrame.FrameCount = 0; if (packetFrameType == FrameType.OutterNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.MobilePhone2Server2Gateway; netFrame.ServerIP = SeverIp; } if (packetFrameType == FrameType.InnerNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Mobile2Gateway; } if (packetFrameType == FrameType.Serial) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Computer2Gateway; } return(netFrame.Send()); #endregion }
//重载 public static byte[] SendCommand_68(string destinationMac, byte length, byte[] commandID, string commandPayload, byte commandcount) { #region 构建68帧 byte[] commandLoad = Character.StringToBytes(commandPayload); byte[] command = new byte[4]; command[0] = commandLoad[1]; command[1] = commandLoad[0]; command[2] = 0x0A; command[3] = 0xFD; SerialPortFrame frame = new SerialPortFrame(commandID, command, length); if (ConcentratorMac.Length < 4) { ConcentratorMac = "00" + ConcentratorMac; } if (destinationMac.Length < 4) { destinationMac = "00" + destinationMac; } if (ConcentratorMac.Length > 4) { ConcentratorMac = ConcentratorMac.Substring(ConcentratorMac.Length - 4, 4); } if (destinationMac.Length > 4) { destinationMac = destinationMac.Substring(destinationMac.Length - 4, 4); } frame.ConcentratorMac = ConcentratorMac; if (destinationMac == ConcentratorMac)//目标是集中器 { frame.NodeMac = "0000"; frame.CommunicationWay = SerialPortFrame.Communication.InnerNet; } else//目标是节点 { if (destinationMac.Length > 4) { frame.NodeMac = destinationMac.Substring(4, 4); } else { frame.NodeMac = destinationMac; } frame.CommunicationWay = SerialPortFrame.Communication.Nontransparent; } frame.FrameCount = commandcount; byte[] serialPortFrame = frame.Send();//形成串口通信帧 #endregion #region 构建网络通信帧 /* 构建网络通信帧 */ NetCommunicationFrame netFrame = new NetCommunicationFrame(serialPortFrame, Convert.ToByte(serialPortFrame.Length)); netFrame.Encryption = false; netFrame.NeedAnswer = false; netFrame.FrameFormat = NetCommunicationFrame.FrameType.DataFrame; receiveByte(); netFrame.MobilePhoneMAC = PhoneMAC; netFrame.GatewayClassification = GatewayType; netFrame.GatewayMAC = GatewayMAC; netFrame.FrameCount = 0; if (packetFrameType == FrameType.OutterNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.MobilePhone2Server2Gateway; netFrame.ServerIP = SeverIp; } if (packetFrameType == FrameType.InnerNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Mobile2Gateway; } if (packetFrameType == FrameType.Serial) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Computer2Gateway; } return(netFrame.Send()); #endregion }
/// <summary> /// 发送最终目的地是集中器的67组播数据帧.(全网通信) /// </summary> /// <param name="ipNumber">节点数量</param> /// <param name="ipLoad">包含节点数组</param> /// <param name="destinationMac">最终目的设备的MAC</param> /// <param name="length">数据总长度</param> /// <param name="dataPayload">数据负载</param> /// <returns></returns> public static byte[] SendCommand_67_data(byte ipNumber, string[] ipLoad, string destinationMac, bool needACK, byte length, byte[] dataPayload) { #region 构建67帧 NodeMulticastFrame frame67 = new NodeMulticastFrame(ipLoad, ipNumber); byte[] NodeMulticastFrame = frame67.Send();//形成节点组播帧 #endregion #region 构建68帧 SerialPortFrame frame = new SerialPortFrame(dataPayload, Convert.ToByte(dataPayload.Length)); if (ConcentratorMac.Length < 4) { ConcentratorMac = "00" + ConcentratorMac; } if (destinationMac.Length < 4) { destinationMac = "00" + destinationMac; } if (ConcentratorMac.Length > 4) { ConcentratorMac = ConcentratorMac.Substring(ConcentratorMac.Length - 4, 4); } if (destinationMac.Length > 4) { destinationMac = destinationMac.Substring(destinationMac.Length - 4, 4); } frame.NeedAnswer = needACK; frame.ConcentratorMac = ConcentratorMac; frame.NodeMac = "eeee"; frame.CommunicationWay = SerialPortFrame.Communication.phone; byte[] serialPortFrame = frame.Send();//形成串口通信帧 #endregion #region 构建组合帧 byte[] MulFrame = new byte[NodeMulticastFrame.Length + serialPortFrame.Length]; NodeMulticastFrame.CopyTo(MulFrame, 0); serialPortFrame.CopyTo(MulFrame, NodeMulticastFrame.Length); #endregion #region 构建网络通信帧 /* 构建网络通信帧 */ NetCommunicationFrame netFrame = new NetCommunicationFrame(MulFrame, Convert.ToByte(MulFrame.Length)); netFrame.Encryption = false; netFrame.NeedAnswer = false; netFrame.FrameFormat = NetCommunicationFrame.FrameType.DataFrame; receiveByte(); netFrame.MobilePhoneMAC = PhoneMAC; netFrame.GatewayClassification = GatewayType; netFrame.GatewayMAC = GatewayMAC; netFrame.FrameCount = 0; netFrame.TransferDirection = NetCommunicationFrame.Direction.Multicast; if (packetFrameType == FrameType.OutterNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.MobilePhone2Server2Gateway; netFrame.ServerIP = SeverIp; } if (packetFrameType == FrameType.InnerNet) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Mobile2Gateway; } if (packetFrameType == FrameType.Serial) { netFrame.TransferDirection = NetCommunicationFrame.Direction.Computer2Gateway; } return(netFrame.Send()); #endregion }
/// <summary> /// 解析收到的帧. /// </summary> /// <param name="receiveByte">接收的字节流.</param> public static void FrameAnalysis(byte[] receiveByte) { #region 第一次负载提取 byte[] receive;//帧负载 //处理网络通信帧 NetCommunicationFrame frame_first = new NetCommunicationFrame(); frame_first.Receive(receiveByte); if (frame_first.CheckSumCorrect) //检查校验和 { receive = frame_first.DataByte; //提取负载,此时负载为串口通信帧 } else { //Program.mainForm.CheckFailedFrameCount++; //更新主窗体校验失败帧统计 return; } if (receive == null)//收到无负载的帧 { //暂不进行任何处理 return; } #endregion #region 第二次负载提取 #region 解析串口68通信帧 if (receive[0] == (byte)SerialPortFrame.FrameHeadAndTail.Head) //串口通信帧头 { SerialPortFrame frame = new SerialPortFrame(); frame.Receive(receive); if (frame.CheckSumCorrect) //校验和正确 { if (frame.FrameType == SerialPortFrame.Type.OrderFrame) //接收到命令帧 { #region 组网状态信息的应答 /* 读取集中器信息命令应答帧 */ if (Check(PacketProcess.readConcentratorMac, frame.CommandID)) { } /* 读取网络状态命令应答帧 */ else if (Check(PacketProcess.readNetState, frame.CommandID)) { } else if (Check(PacketProcess.readNodeMACIP, frame.CommandID)) { } /* 读取节点信息命令应答帧 */ else if (Check(PacketProcess.readNodeInformation, frame.CommandID)) { } /* 读取所有节点的状态信息命令应答帧 */ else if (Check(PacketProcess.readAllNodesStatus, frame.CommandID)) { //建立CommandPayloadEventArgs 对象 CommandFrameEventArgs e = new CommandFrameEventArgs(frame.CommandPayload); if (ReadAllNodesStatusEvent != null) { // 如果有对象注册 ReadAllNodesStatusEvent(e); // 调用所有注册对象的方法 } } else if (Check(PacketProcess.writeEdBeginUpdate, frame.CommandID)) { CommandFrameEventArgs e = new CommandFrameEventArgs(frame.CommandPayload); if (WriteEdBeginUpdateEvent != null) { WriteEdBeginUpdateEvent(e); } } #endregion else if (Check(PacketProcess.sendfile_start, frame.CommandID)) { } else if (Check(PacketProcess.sendfile_over, frame.CommandID)) { } } else if (frame.FrameType == SerialPortFrame.Type.AlarmFrame)//接收到告警帧 { AlarmFrameEventArgs e = new AlarmFrameEventArgs((frame as InternalFrame).DataByte); if (ReceiveReplyFrameEvent != null) // 如果有对象注册 { ReceiveAlarmFrameEventEvent(e); // 调用所有注册对象的方法 } //MessageBox.Show("68帧告警"+Character.BytesToString(e.AlarmInfo, 0, e.AlarmInfo.Length - 1, true)); //Program.mainForm.AlarmFrameCount++; //更新主窗体告警帧统计 return; } else if (frame.FrameType == SerialPortFrame.Type.ReplyFrame)//接收到应答帧 { } else if (frame.FrameType == SerialPortFrame.Type.DataFrame)//接收到数据帧 { DataFrameEventArgs e = new DataFrameEventArgs(frame.NeedAnswer, frame.TransferState, frame.NodeMac, frame.FrameCount, frame.DataByte, frame.FrameType); if (ReceiveDataFrameEvent != null) // 如果有对象注册 { ReceiveDataFrameEvent(e); // 调用所有注册对象的方法 } return; } else//不是已知帧类型 { //Program.mainForm.UnknownFrameCount++; //更新主窗体未识别帧统计 return; } } else//校验和错误 { //Program.mainForm.CheckFailedFrameCount++; //更新主窗体校验失败帧统计 return; } } #endregion #region 未知帧头处理 else//未知帧头 { return; } #endregion #endregion }