private void ExplainReadVerPacket(byte[] RecBuf) { string strVer = ""; if (UpdateType == enUpdateType.地锁系统) { Protocol6009Struct rxStruct = Extract6009RxBuffer(RecBuf); if (false == rxStruct.isSuccess || rxStruct.CommandIDByte != 0x40) { return; } strVer = "软件版本" + ": " + rxStruct.DataByte[0].ToString("X2") + "." + rxStruct.DataByte[1].ToString("X2") + "\n"; strVer += "硬件版本" + ": " + rxStruct.DataByte[2].ToString("X2") + "." + rxStruct.DataByte[3].ToString("X2") + "\n"; strVer += "协议版本" + ": " + rxStruct.DataByte[4].ToString("X2") + "." + rxStruct.DataByte[5].ToString("X2"); } else if (UpdateType == enUpdateType.无线模块系统) { Protocol6009Struct rxStruct = Extract6009RxBuffer(RecBuf); if (false == rxStruct.isSuccess || rxStruct.CommandIDByte != 0x40) { return; } strVer = "软件版本" + ": " + rxStruct.DataByte[0].ToString("X2") + "." + rxStruct.DataByte[1].ToString("X2") + "\n"; strVer += "硬件版本" + ": " + rxStruct.DataByte[2].ToString("X2") + "." + rxStruct.DataByte[3].ToString("X2") + "\n"; strVer += "协议版本" + ": " + rxStruct.DataByte[4].ToString("X2") + "." + rxStruct.DataByte[5].ToString("X2"); } timerUpdate.Stop(); UpdateStep = enUpdateProc.Update_End; MessageBox.Show(strVer, "程序版本", MessageBoxButtons.OK, MessageBoxIcon.Information); btRdVersionInfo.Enabled = true; btStartUpdate.Enabled = true; gpSelectType.Enabled = true; return; }
private void TransmitReadVerPacket() { int iLoop; if (UpdateStep != enUpdateProc.Read_Version) { return; } if (UpdateRetryTimes == 0) { timerUpdate.Stop(); UpdateStep = enUpdateProc.Update_End; MessageBox.Show("读版本错误,主板没有应答!", "读版本错误", MessageBoxButtons.OK, MessageBoxIcon.Stop); btRdVersionInfo.Enabled = true; btStartUpdate.Enabled = true; gpSelectType.Enabled = true; return; } if (UpdateType == enUpdateType.地锁系统) { Protocol6009Struct txStruct = new Protocol6009Struct(); txStruct.CommandIDByte = 0x40; // 发送数据 dataTransmit(_6009CreatTxBuffer(txStruct)); } else if (UpdateType == enUpdateType.无线模块系统) { Protocol6009Struct txStruct = new Protocol6009Struct(); txStruct.CommandIDByte = 0x40; // 发送数据 dataTransmit(_6009CreatTxBuffer(txStruct)); } else { timerUpdate.Stop(); UpdateStep = enUpdateProc.Update_End; btRdVersionInfo.Enabled = true; btStartUpdate.Enabled = true; gpSelectType.Enabled = true; MessageBox.Show("请选择一个集中器类型"); return; } UpdateWaitTime = 80; UpdateRetryTimes--; }
private void ExplainAppPacket(byte[] RecBuf) { UInt16 u16Crc; byte[] dataBuf = new byte[100]; if (UpdateType == enUpdateType.地锁系统) { Protocol6009Struct rxStruct = Extract6009RxBuffer(RecBuf); if (false == rxStruct.isSuccess || rxStruct.CommandIDByte != 0xF1) { return; } Array.Copy(rxStruct.DataByte, dataBuf, rxStruct.DataByte.Length); } else if (UpdateType == enUpdateType.无线模块系统) { Protocol6009Struct rxStruct = Extract6009RxBuffer(RecBuf); if (false == rxStruct.isSuccess || rxStruct.CommandIDByte != 0xF4) { return; } Array.Copy(rxStruct.DataByte, dataBuf, rxStruct.DataByte.Length); } else { return; } u16Crc = (UInt16)(dataBuf[0] + dataBuf[1] * 256); AppUpdateWriteAddress = dataBuf[2] + (dataBuf[3] << 8) + (dataBuf[4] << 16) + (dataBuf[5] << 24); if (u16Crc != updateCrc) { UpdateWaitTime = 3; AppUpdateWriteAddress = 0; return; } if (dataBuf[6] == 0xAA) { AppUpdateWriteAddress += AppUpdateCountOnePacket; prgBarUpdate.Maximum = updateLength; prgBarUpdate.Value = (AppUpdateWriteAddress > updateLength) ? updateLength : AppUpdateWriteAddress; prgBarUpdate.Visible = true; if (AppUpdateWriteAddress >= updateLength) { timerUpdate.Stop(); UpdateStep = enUpdateProc.Update_End; if (UpdateType == enUpdateType.地锁系统) { MessageBox.Show("程序升级成功,主板将在10秒钟内重新启动。", "升级成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (UpdateType == enUpdateType.无线模块系统) { MessageBox.Show("无线模块升级程序下发成功。", "升级成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } btOpenUpdateFile.Enabled = true; btRdVersionInfo.Enabled = true; rbtBootUpdate.Enabled = true; rbtAppUpdate.Enabled = true; btStartUpdate.Enabled = true; gpSelectType.Enabled = true; btStartUpdate.Text = "开始升级"; return; } UpdateWaitTime = 1; UpdateRetryTimes = 5; } }
private void TransmitAppPacket() { int iLoop, iPos; if (UpdateRetryTimes == 0) { timerUpdate.Stop(); UpdateStep = enUpdateProc.Update_End; MessageBox.Show("数据传输出错,升级失败!", "升级失败", MessageBoxButtons.OK, MessageBoxIcon.Stop); prgBarUpdate.Visible = false; btOpenUpdateFile.Enabled = true; btRdVersionInfo.Enabled = true; btStartUpdate.Enabled = true; rbtBootUpdate.Enabled = true; rbtAppUpdate.Enabled = true; gpSelectType.Enabled = true; btStartUpdate.Text = "开始升级"; return; } if (UpdateType == enUpdateType.地锁系统) { Protocol6009Struct txStruct = new Protocol6009Struct(); txStruct.CommandIDByte = 0xF1; // 数据域 if (updateLength - AppUpdateWriteAddress > AppUpdateCountOnePacket) { AppUpdateCurPkgBytes = AppUpdateCountOnePacket; } else { AppUpdateCurPkgBytes = updateLength - AppUpdateWriteAddress; } txStruct.DataByte = new byte[AppUpdateCurPkgBytes + 12]; iLoop = 0; // 增加CRC txStruct.DataByte[iLoop++] = (byte)updateCrc; txStruct.DataByte[iLoop++] = (byte)(updateCrc >> 8); // 增加写入地址 txStruct.DataByte[iLoop++] = (byte)AppUpdateWriteAddress; txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 8); txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 16); txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 24); // 增加写入代码总长度 txStruct.DataByte[iLoop++] = (byte)updateLength; txStruct.DataByte[iLoop++] = (byte)(updateLength >> 8); txStruct.DataByte[iLoop++] = (byte)(updateLength >> 16); txStruct.DataByte[iLoop++] = (byte)(updateLength >> 24); // 本包升级代码的长度 txStruct.DataByte[iLoop++] = (byte)AppUpdateCurPkgBytes; txStruct.DataByte[iLoop++] = (byte)(AppUpdateCurPkgBytes >> 8); // 升级数据 iPos = AppUpdateWriteAddress; for (; iLoop < txStruct.DataByte.Length; iLoop++) { txStruct.DataByte[iLoop] = updateBuf[iPos++]; } // 发送数据 dataTransmit(_6009CreatTxBuffer(txStruct)); UpdateWaitTime = 200; UpdateRetryTimes--; } else if (UpdateType == enUpdateType.无线模块系统) { Protocol6009Struct txStruct = new Protocol6009Struct(); txStruct.CommandIDByte = 0xF4; // 数据域 if (updateLength - AppUpdateWriteAddress > AppUpdateCountOnePacket) { AppUpdateCurPkgBytes = AppUpdateCountOnePacket; } else { AppUpdateCurPkgBytes = updateLength - AppUpdateWriteAddress; } txStruct.DataByte = new byte[AppUpdateCurPkgBytes + 12]; iLoop = 0; // 增加CRC txStruct.DataByte[iLoop++] = (byte)updateCrc; txStruct.DataByte[iLoop++] = (byte)(updateCrc >> 8); // 增加写入地址 txStruct.DataByte[iLoop++] = (byte)AppUpdateWriteAddress; txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 8); txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 16); txStruct.DataByte[iLoop++] = (byte)(AppUpdateWriteAddress >> 24); // 增加写入代码总长度 txStruct.DataByte[iLoop++] = (byte)updateLength; txStruct.DataByte[iLoop++] = (byte)(updateLength >> 8); txStruct.DataByte[iLoop++] = (byte)(updateLength >> 16); txStruct.DataByte[iLoop++] = (byte)(updateLength >> 24); // 本包升级代码的长度 txStruct.DataByte[iLoop++] = (byte)AppUpdateCurPkgBytes; txStruct.DataByte[iLoop++] = (byte)(AppUpdateCurPkgBytes >> 8); // 升级数据 iPos = AppUpdateWriteAddress; for (; iLoop < txStruct.DataByte.Length; iLoop++) { txStruct.DataByte[iLoop] = updateBuf[iPos++]; } // 发送数据 dataTransmit(_6009CreatTxBuffer(txStruct)); UpdateWaitTime = 200; UpdateRetryTimes--; } UpdateWaitTime = 80; UpdateRetryTimes--; }
public static byte[] _6009CreatTxBuffer(Protocol6009Struct txStruct) { int crc16; //byte bCrc; int iLen = 0; try { byte[] buf = new byte[1500]; // 同步字 buf[iLen++] = SyncWord1_6009; buf[iLen++] = SyncWord2_6009; // 包长度,后面填充 iLen += 2; // 报文标识:下行 命令帧 不加密 需要回执 buf[iLen++] = 0x10; // 帧序号 buf[iLen++] = FrameSn++; // 命令字 buf[iLen++] = txStruct.CommandIDByte; // 设备类型 buf[iLen++] = 0xFA; // 上位机 // 生命周期 buf[iLen++] = 0x0F; // 传输路径 buf[iLen++] = 0x02; // 设置地址 for (int i = 0; i < NodeAddrLength; i++) { buf[iLen++] = 0xFA; } for (int i = 0; i < NodeAddrLength; i++) { buf[iLen++] = 0xD5; } // 数据域 if (txStruct.DataByte != null) { Array.Copy(txStruct.DataByte, 0, buf, iLen, txStruct.DataByte.Length); iLen += txStruct.DataByte.Length; } // 上行信号强度 buf[iLen++] = 0; // 下行信号强度 buf[iLen++] = 0; // 包长度 //buf[2] = (byte)(iLen); //buf[3] = (byte)(iLen >> 8); buf[2] = (byte)(iLen + 1); buf[3] = (byte)((iLen + 1) >> 8); // 校验字 //bCrc = comCalCRC8(buf, 2, iLen - 2, true); //buf[iLen++] = bCrc; crc16 = __CalCRC16(buf, 2, iLen - 2); buf[iLen++] = (byte)((crc16 >> 8) & 0xFF); buf[iLen++] = (byte)((crc16) & 0xFF); // 结束符 buf[iLen++] = 0x16; // 其他参数 //buf[iLen++] = 0x00; //buf[iLen++] = 9 + 16; //if (Util.Protocol == enProtocol._6009) //{ // buf[iLen++] = 3; //} // 赋给另外一个数组 byte[] txBuf = new byte[iLen]; Array.Copy(buf, txBuf, iLen); return(txBuf); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(null); }
public static Protocol6009Struct Extract6009RxBuffer(byte[] Buffer) { Protocol6009Struct proStruct = new Protocol6009Struct(); int iLoop, packPos, startPos; //byte calCrc; int calCrc; try { if (Buffer[0] == SyncWord1_6009 && Buffer[1] == SyncWord2_6009) { startPos = 2; } else { startPos = 0; } packPos = startPos; // 包长度 proStruct.PacketLength = Buffer[packPos++] + (Buffer[packPos++] & 0x03) * 256; //报文标识 proStruct.PacketProperty = Buffer[packPos++]; // 任务号 proStruct.TaskIDByte = Buffer[packPos++]; // 命令字 proStruct.CommandIDByte = Buffer[packPos++]; // 设备类型 proStruct.DeviceTypeByte = Buffer[packPos++]; // 生命周期 proStruct.LifeCycleByte = Buffer[packPos++]; // 路径条数 proStruct.RouteLevelByte = (Byte)(Buffer[packPos] & 0x0F); // 当前位置 proStruct.RouteCurPos = (byte)(Buffer[packPos] >> 4 & 0x0F); // 传输路径 packPos++; proStruct.RoutePathList = new byte[proStruct.RouteLevelByte * NodeAddrLength]; for (iLoop = 0; iLoop < proStruct.RoutePathList.Length; iLoop++) { proStruct.RoutePathList[iLoop] = Buffer[packPos++]; } // 数据域 proStruct.DataByte = new byte[proStruct.PacketLength - 5 - packPos + startPos]; for (iLoop = 0; iLoop < proStruct.DataByte.Length; iLoop++) { proStruct.DataByte[iLoop] = Buffer[packPos++]; } // 下行信号强度 proStruct.DownSignalStrengthByte = Buffer[packPos++]; // 上行信号强度 proStruct.UpSignalStrengthByte = Buffer[packPos++]; // 校验字 //proStruct.Crc8Byte = Buffer[packPos++]; proStruct.Crc16Byte = ((Buffer[packPos++] << 8) & 0xff00) | (Buffer[packPos++] & 0xFF); // 结束字 proStruct.EndByte = Buffer[packPos++]; // 是否成功 //calCrc = comCalCRC8(Buffer, startPos, proStruct.PacketLength - 2, true); //proStruct.isSuccess = (calCrc == proStruct.Crc8Byte) ? true : false; calCrc = __CalCRC16(Buffer, startPos, proStruct.PacketLength - 3); proStruct.isSuccess = (calCrc == proStruct.Crc16Byte) ? true : false; return(proStruct); } catch { proStruct.isSuccess = false; return(proStruct); } }