Example #1
0
        private void m_asyncButton_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                //Thread.Sleep(1000);
                ////MessageCenter.Instance.MessageAdd.Invoke(null, null);
                //MessageCenter.Instance.AddMessage(DataType.TextValue);//发布(子线程发布)

                //int progress = (int)(((float)dataSize * packetNumber) / fileStream.Length * 100);
                NowDownloadProgressEvent.Invoke(null, new EventArgs());
            });
        }
Example #2
0
        public void YmodemUploadFile()
        {
            /* control signals */
            const byte STX = 2;  // Start of TeXt
            const byte EOT = 4;  // End Of Transmission
            const byte ACK = 6;  // Positive ACknowledgement
            const byte C   = 67; // capital letter C

            /* sizes */
            const int dataSize = 1024;
            const int crcSize  = 2;

            /* THE PACKET: 1029 bytes */
            /* header: 3 bytes */
            // STX
            int packetNumber         = 0;
            int invertedPacketNumber = 255;

            /* data: 1024 bytes */
            byte[] data = new byte[dataSize];
            /* footer: 2 bytes */
            byte[] CRC = new byte[crcSize];

            /* get the file */
            FileStream fileStream = new FileStream(@path, FileMode.Open, FileAccess.Read);

            serialPort.PortName = portName; serialPort.BaudRate = baudRate;
            serialPort.Open();
            try
            {
                //serialPort.Write(new byte[] { 0x31 }, 0, 1);
                /* send the initial packet with filename and filesize */
                if (serialPort.ReadByte() != C)
                {
                    Debug.WriteLine("Can't begin the transfer.");
                    serialPort.Close();
                    DownloadResultEvent.Invoke(false, new EventArgs());
                    return;// false;
                }
                else
                {
                    Debug.WriteLine(" begin the transfer.");
                }

                sendYmodemInitialPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, path, fileStream, CRC, crcSize);
                // Thread.Sleep(5000);
                byte temp = (byte)serialPort.ReadByte();
                Debug.WriteLine(temp);
                if (temp != ACK)//(serialPort.ReadByte() != ACK)
                {
                    Debug.WriteLine("Can't send the initial packet.");
                    DownloadResultEvent.Invoke(false, new EventArgs());
                    return;// false;
                }

                if (serialPort.ReadByte() != C)
                {
                    DownloadResultEvent.Invoke(false, new EventArgs());
                    return;// false;
                }
                /* send packets with a cycle until we send the last byte */
                int fileReadCount;
                do
                {
                    /* if this is the last packet fill the remaining bytes with 0 */
                    fileReadCount = fileStream.Read(data, 0, dataSize);
                    if (fileReadCount == 0)
                    {
                        break;
                    }
                    if (fileReadCount != dataSize)
                    {
                        for (int i = fileReadCount; i < dataSize; i++)
                        {
                            data[i] = 0;
                        }
                    }

                    /* calculate packetNumber */
                    packetNumber++;
                    if (packetNumber > 255)
                    {
                        packetNumber -= 256;
                    }
                    Console.WriteLine(packetNumber);

                    /* calculate invertedPacketNumber */
                    invertedPacketNumber = 255 - packetNumber;

                    /* calculate CRC */
                    Crc16Ccitt crc16Ccitt = new Crc16Ccitt(InitialCrcValue.Zeros);
                    CRC = crc16Ccitt.ComputeChecksumBytes(data);

                    /* send the packet */
                    sendYmodemPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize);
                    int progress = (int)(((float)dataSize * packetNumber) / fileStream.Length * 100);
                    Debug.WriteLine("progress:  " + progress);
                    if (progress > 100)
                    {
                        progress = 100;
                    }
                    NowDownloadProgressEvent.Invoke(progress, new EventArgs());
                    /* wait for ACK */
                    temp = (byte)serialPort.ReadByte();
                    if (temp != ACK)
                    {
                        Debug.WriteLine("temp" + temp);
                        Debug.WriteLine("Couldn't send a packet.");
                        DownloadResultEvent.Invoke(false, new EventArgs());
                        return;// false;
                    }
                } while (dataSize == fileReadCount);

                /* send EOT (tell the downloader we are finished) */
                serialPort.Write(new byte[] { EOT }, 0, 1);
                /* send closing packet */
                packetNumber         = 0;
                invertedPacketNumber = 255;
                data = new byte[dataSize];
                CRC  = new byte[crcSize];
                sendYmodemClosingPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize);
                /* get ACK (downloader acknowledge the EOT) */
                if (serialPort.ReadByte() != ACK)
                {
                    Debug.WriteLine("Can't complete the transfer.");
                    DownloadResultEvent.Invoke(false, new EventArgs());
                    return;// false;
                }
            }
            catch (TimeoutException)
            {
                throw new Exception("Eductor does not answering");
            }
            finally
            {
                fileStream.Close();
            }
            serialPort.Close();
            Debug.WriteLine("File transfer is succesful");
            DownloadResultEvent.Invoke(true, new EventArgs());
            return;// true;
        }