void SendHandler() { XmodemMessage msg = null; lock (msgQueue) { if (msgQueue.Count > 0) { msg = msgQueue.Dequeue(); } } if (msg != null) { reTryCount = 0; switch (msg.MessageType) { case XmodemMessageType.NAK: if (SendStage == XmodemSendStage.WaitReceiveRequest) { SendStage = XmodemSendStage.PacketSending; xmodemInfo.CheckMode = XModemCheckMode.CheckSum; if (StartSend != null) { StartSend(xmodemInfo, null); } } else if (SendStage == XmodemSendStage.WaitReceiveAnswerEndTransmit) { SendEOT(); } else { // 通知重发或发头一包 if (ReSendPacket != null) { ReSendPacket(xmodemInfo, null); } } break; case XmodemMessageType.KEY_C: if (SendStage == XmodemSendStage.WaitReceiveRequest) { SendStage = XmodemSendStage.PacketSending; // 通知发头一包CRC xmodemInfo.CheckMode = XModemCheckMode.CRC16; if (StartSend != null) { StartSend(xmodemInfo, null); } } break; case XmodemMessageType.ACK: if (SendStage == XmodemSendStage.PacketSending) { // 通知发下一包 if (SendNextPacket != null) { SendNextPacket(xmodemInfo, null); } } else if (SendStage == XmodemSendStage.WaitReceiveAnswerEndTransmit) { // 通知中止 //if (AbortTransmit != null) //{ // AbortTransmit(xmodemInfo, null); //} if (EndOfTransmit != null) { EndOfTransmit(xmodemInfo, null); } IsStart = false; } break; case XmodemMessageType.CAN: // 通知中止 if (AbortTransmit != null) { AbortTransmit(xmodemInfo, null); } break; default: break; } } else { if (waitReceiveEvent.WaitOne(3000)) { waitReceiveEvent.Reset(); } else { reTryCount++; if (reTryCount > RetryMax) { IsStart = false; //通知接收超时 if (TransmitTimeOut != null) { TransmitTimeOut(xmodemInfo, null); } } } } }
void ReceiveHandler() { if (ReceiveStage == XmodemReceiveStage.WaitForFirstPacket) { if (reTryCount % 2 == 0) { xmodemInfo.CheckMode = XModemCheckMode.CheckSum; SendKEYC(); } else { xmodemInfo.CheckMode = XModemCheckMode.CRC16; SendNAK(); } } XmodemMessage msg = null; lock (msgQueue) { if (msgQueue.Count > 0) { msg = msgQueue.Dequeue(); } } if (msg != null) { reTryCount = 0; switch (msg.MessageType) { case XmodemMessageType.PACKET: ReceiveStage = XmodemReceiveStage.PacketReceiving; SendACK(); if (ReceivedPacket != null) { PacketEventArgs e = msg.Value as PacketEventArgs; ReceivedPacket(xmodemInfo, new PacketEventArgs(e.PacketNo, e.Packet)); } // 通知发下一包 if (SendNextPacket != null) { SendNextPacket(xmodemInfo, null); } break; case XmodemMessageType.PACKET_ERROR: SendNAK(); // 通知重发 if (ReSendPacket != null) { ReSendPacket(xmodemInfo, null); } break; case XmodemMessageType.EOT: SendACK(); // 通知完成 if (EndOfTransmit != null) { EndOfTransmit(xmodemInfo, null); } break; case XmodemMessageType.CAN: SendACK(); // 通知中止 if (AbortTransmit != null) { AbortTransmit(xmodemInfo, null); } break; default: break; } } else { if (waitReceiveEvent.WaitOne(3000)) { waitReceiveEvent.Reset(); } else { reTryCount++; if (reTryCount > RetryMax) { IsStart = false; //通知接收超时 if (TransmitTimeOut != null) { TransmitTimeOut(xmodemInfo, null); } } } } }
/// <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(); }