Esempio n. 1
0
        void ReceiveHandler()
        {
            if (ReceiveStage == YmodemReceiveStage.WaitForFileInfo || ReceiveStage == YmodemReceiveStage.WaitForFirstPacket)
            {
                SendKEYC();
            }

            if (msgQueue.Count > 0)
            {
                YmodemMessage msg = msgQueue.Dequeue();
                if (msg != null)
                {
                    reTryCount = 0;

                    switch (msg.MessageType)
                    {
                    case YmodemMessageType.PACKET:

                        PacketEventArgs e = msg.Value as PacketEventArgs;
                        if (ReceiveStage == YmodemReceiveStage.WaitForFileInfo)
                        {
                            if (e.PacketNo == 0)
                            {
                                ReceiveStage = YmodemReceiveStage.WaitForFirstPacket;
                                SendACK();

                                if (ReceivedPacket != null)
                                {
                                    ReceivedPacket(ymodemInfo, new PacketEventArgs(e.PacketNo, e.Packet));
                                }
                            }
                            //else
                            //{
                            //    SendNAK();
                            //}
                        }

                        else if (ReceiveStage == YmodemReceiveStage.WaitForFirstPacket ||
                                 ReceiveStage == YmodemReceiveStage.PacketReceiving)
                        {
                            if (ReceiveStage == YmodemReceiveStage.WaitForFirstPacket)
                            {
                                ReceiveStage = YmodemReceiveStage.PacketReceiving;
                            }

                            SendACK();

                            if (ReceivedPacket != null)
                            {
                                ReceivedPacket(ymodemInfo, new PacketEventArgs(e.PacketNo, e.Packet));
                            }

                            // 通知发下一包
                            if (SendNextPacket != null)
                            {
                                SendNextPacket(ymodemInfo, null);
                            }
                        }


                        break;

                    case YmodemMessageType.PACKET_ERROR:
                        SendNAK();
                        // 通知重发
                        if (ReSendPacket != null)
                        {
                            ReSendPacket(ymodemInfo, null);
                        }
                        break;

                    case YmodemMessageType.EOT:
                        SendACK();
                        // 通知完成
                        if (EndOfTransmit != null)
                        {
                            EndOfTransmit(ymodemInfo, null);
                        }
                        break;

                    case YmodemMessageType.CAN:
                        SendACK();
                        // 通知中止
                        if (AbortTransmit != null)
                        {
                            AbortTransmit(ymodemInfo, null);
                        }
                        break;

                    default:
                        break;
                    }
                }
            }
            else
            {
                if (waitReceiveEvent.WaitOne(3000))
                {
                    waitReceiveEvent.Reset();
                }
                else
                {
                    reTryCount++;
                    if (reTryCount > RetryMax)
                    {
                        IsStart = false;
                        //通知接收超时
                        if (TransmitTimeOut != null)
                        {
                            TransmitTimeOut(ymodemInfo, null);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 解析数据
        /// </summary>
        /// <param name="data"></param>
        private void ParseReceivedMessage(byte[] data)
        {
            YmodemMessage ReceivedMessage = null;

            if (data == null)
            {
                ReceivedMessage = null;
            }
            else
            {
                if (data[0] == STX || data[0] == SOH)
                {
                    ReceivedMessage = new YmodemMessage(YmodemMessageType.PACKET_ERROR);
                    int packetLen = 0;
                    if (data[0] == STX)
                    {
                        packetLen = 1024;
                    }
                    else if (data[0] == SOH)
                    {
                        packetLen = 128;
                    }

                    int checkDataLen = 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);

                        frameCheckCode = (data[3 + packetLen] << 8) + data[3 + packetLen + 1];
                        calCheckCode   = Convert.ToUInt16(DataCheck.GetCRC(CRCType.CRC16_XMODEM, packet) & 0xFFFF);


                        if (frameCheckCode == calCheckCode)
                        {
                            ReceivedMessage = new YmodemMessage(YmodemMessageType.PACKET, new PacketEventArgs(packetNo, packet));
                        }
                    }
                }
                else
                {
                    foreach (byte b in data)
                    {
                        ReceivedMessage = null;

                        if (b == EOT)
                        {
                            ReceivedMessage = new YmodemMessage(YmodemMessageType.EOT);
                        }
                        else if (b == CAN)
                        {
                            ReceivedMessage = new YmodemMessage(YmodemMessageType.CAN);
                        }
                        else if (b == NAK)
                        {
                            ReceivedMessage = new YmodemMessage(YmodemMessageType.NAK);
                        }
                        else if (b == ACK)
                        {
                            ReceivedMessage = new YmodemMessage(YmodemMessageType.ACK);
                        }
                        else if (b == KEY_C)
                        {
                            ReceivedMessage = new YmodemMessage(YmodemMessageType.KEY_C);
                        }
                        else
                        {
                        }

                        if (ReceivedMessage != null)
                        {
                            msgQueue.Enqueue(ReceivedMessage);
                        }
                    }
                }
            }

            waitReceiveEvent.Set();
        }