public void SendPacket(PacketEventArgs packet) { int packetLen = 0; int checkLen = 0; byte[] data; if (xmodemInfo.CheckMode == XModemCheckMode.CheckSum) { checkLen = 1; } else { checkLen = 2; } if (xmodemInfo.Type == XModemType.XModem_1K) { packetLen = 1024; } else { packetLen = 128; } data = new byte[3 + packetLen + checkLen]; data[0] = SOH; if (xmodemInfo.Type == XModemType.XModem_1K) { data[0] = STX; } data[1] = Convert.ToByte(packet.PacketNo & 0xFF); data[2] = Convert.ToByte((~data[1]) & 0xFF); Array.Copy(packet.Packet, 0, data, 3, packetLen); if (xmodemInfo.CheckMode == XModemCheckMode.CheckSum) { data[3 + packetLen] = Convert.ToByte(DataCheck.GetCheckSum(packet.Packet) & 0xFF); } else { UInt16 crc = Convert.ToUInt16(DataCheck.GetCRC(CRCType.CRC16_XMODEM, packet.Packet) & 0xFFFF); data[3 + packetLen] = Convert.ToByte(crc >> 8); data[3 + packetLen + 1] = Convert.ToByte(crc & 0xFF); } SendFrameToUart(data); }
private void CalCheckData() { try { UInt32 checkResult = 0; if (_DataBytes == null) { return; } gbCRCParam.Enabled = false; int index = cbDataCheckType.SelectedIndex; if (index == -1) { return; } if (index == 0) { checkResult = Convert.ToUInt16(DataCheck.GetCheckSum(_DataBytes) & 0xFFFF); dataWidth = 16; } else if (index == 1) { checkResult = Convert.ToByte(DataCheck.GetCheckSum(_DataBytes) & 0xFF); dataWidth = 8; } else if (index == 2) { checkResult = DataCheck.GetXor(_DataBytes); dataWidth = 8; } else { gbCRCParam.Enabled = true; CRCType crcType = (CRCType)(index - 3); CRCInfo crcInfo = DataCheck.GetCRCInfo(crcType); crcInfo.Poly = Convert.ToUInt32(txtPoly.Text, 16); crcInfo.Init = Convert.ToUInt32(txtInit.Text, 16); crcInfo.RefIn = chkRefIn.Checked; crcInfo.RefOut = chkRefOut.Checked; crcInfo.XorOut = chkXorOut.Checked ? 0xFFFFFFFF : 0x00000000; if (crcType >= CRCType.CRC16_IBM && crcType <= CRCType.CRC16_DNP) { checkResult = DataCheck.GetCRC16(crcInfo, _DataBytes); dataWidth = 16; } else if (crcType >= CRCType.CRC32 && crcType <= CRCType.CRC32_MPEG2) { checkResult = DataCheck.GetCRC32(crcInfo, _DataBytes); dataWidth = 32; } } CheckResultValue = checkResult; txtResult.Text = getStringByUint32(CheckResultValue, chkIsLittleEndian.Checked, dataWidth); } catch (System.Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// 解析数据 /// </summary> /// <param name="data"></param> private void ParseReceivedMessage(byte[] data) { XmodemMessage ReceivedMessage = null; if (data == null) { ReceivedMessage = null; } else { if (data[0] == STX || data[0] == SOH) { ReceivedMessage = new XmodemMessage(XmodemMessageType.PACKET_ERROR); int packetLen = 0; if (data[0] == STX) { packetLen = 1024; } else if (data[0] == SOH) { packetLen = 128; } int checkDataLen = xmodemInfo.CheckMode == XModemCheckMode.CheckSum ? 1 : 2; if (packetLen + 3 + checkDataLen == data.Length) { int packetNo = 0; if (data[1] == Convert.ToByte((~data[2]) & 0xFF)) { packetNo = data[1]; } int frameCheckCode = 0; int calCheckCode = -1; byte[] packet = new byte[packetLen]; Array.Copy(data, 3, packet, 0, packetLen); if (xmodemInfo.CheckMode == XModemCheckMode.CheckSum) { frameCheckCode = data[3 + packetLen]; calCheckCode = Convert.ToByte(DataCheck.GetCheckSum(packet) & 0xFF); } else { frameCheckCode = (data[3 + packetLen] << 8) + data[3 + packetLen + 1]; calCheckCode = Convert.ToUInt16(DataCheck.GetCRC(CRCType.CRC16_XMODEM, packet) & 0xFFFF); } if (frameCheckCode == calCheckCode) { ReceivedMessage = new XmodemMessage(XmodemMessageType.PACKET, new PacketEventArgs(packetNo, packet)); } } msgQueue.Enqueue(ReceivedMessage); } else { foreach (byte b in data) { ReceivedMessage = null; if (b == EOT) { ReceivedMessage = new XmodemMessage(XmodemMessageType.EOT); } else if (b == CAN) { ReceivedMessage = new XmodemMessage(XmodemMessageType.CAN); } else if (b == NAK) { ReceivedMessage = new XmodemMessage(XmodemMessageType.NAK); } else if (b == ACK) { ReceivedMessage = new XmodemMessage(XmodemMessageType.ACK); } else if (b == KEY_C) { ReceivedMessage = new XmodemMessage(XmodemMessageType.KEY_C); } if (ReceivedMessage != null) { msgQueue.Enqueue(ReceivedMessage); } } } } waitReceiveEvent.Set(); }