Beispiel #1
0
        public void Start()
        {
            IsStart    = true;
            reTryCount = 0;


            ReceiveStage = XmodemReceiveStage.WaitForFirstPacket;
            SendStage    = XmodemSendStage.WaitReceiveRequest;
            msgQueue.Clear();

            TransThread = new Thread(new ThreadStart(TransThreadHandler));
            TransThread.IsBackground = true;
            TransThread.Name         = "XmodemTransThread";
            TransThread.Start();
            if (xmodemInfo.TransMode == TransmitMode.Receive)
            {
                if (StartReceive != null)
                {
                    StartReceive(xmodemInfo, null);
                }
            }
        }
Beispiel #2
0
        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);
                        }
                    }
                }
            }
        }
Beispiel #3
0
 private void SendEOT()
 {
     SendFrameToUart(EOT);
     SendStage = XmodemSendStage.WaitReceiveAnswerEndTransmit;
 }