Exemple #1
0
        private bool sendYmodemClosingPacket(byte STX, int packetNumber, int invertedPacketNumber, byte[] data, int dataSize, byte[] CRC, int crcSize)
        {
            /* calculate CRC */
            Crc16Ccitt crc16Ccitt = new Crc16Ccitt(InitialCrcValue.Zeros);

            CRC = crc16Ccitt.ComputeChecksumBytes(data);

            /* send the packet */
            if (!sendYmodemPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize))
            {
                return(false);
            }

            return(true);
        }
Exemple #2
0
        private bool sendYmodemInitialPacket(byte STX, int packetNumber, int invertedPacketNumber, byte[] data, int dataSize, string path, FileStream fileStream, byte[] CRC, int crcSize)
        {
            string fileName = System.IO.Path.GetFileName(path);
            string fileSize = fileStream.Length.ToString();


            /* add filename to data */
            int i;

            for (i = 0; i < fileName.Length && (fileName.ToCharArray()[i] != 0); i++)
            {
                data[i] = (byte)fileName.ToCharArray()[i];
            }
            data[i] = 0;

            /* add filesize to data */
            int j;

            for (j = 0; j < fileSize.Length && (fileSize.ToCharArray()[j] != 0); j++)
            {
                data[(i + 1) + j] = (byte)fileSize.ToCharArray()[j];
            }
            data[(i + 1) + j] = 0;

            /* fill the remaining data bytes with 0 */
            for (int k = ((i + 1) + j) + 1; k < dataSize; k++)
            {
                data[k] = 0;
            }

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

            CRC = crc16Ccitt.ComputeChecksumBytes(data);

            /* send the packet */
            if (!sendYmodemPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize))
            {
                return(false);
            }
            return(true);
        }
Exemple #3
0
        private bool YmodemUpdateFile(string filePath)
        {
            const byte SOH = 1;     // Start Of Head

            const byte EOT     = 4; // End Of Transmission
            const byte ACK     = 6; // Positive ACknowledgement
            const int  crcSize = 2;

            //byte HEAD = 0;
            // int dataSize = 1024;
            int dataSize = Convert.ToInt32(cbx_PageSize.Text);

            STX = getPageHead(cbx_PageSize.Text);

            /* header: 3 bytes */
            int proprassVal          = 0;
            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(@filePath, FileMode.Open, FileAccess.Read);
            int        packetCnt  = (fileStream.Length % dataSize) == 0 ? (int)(fileStream.Length / dataSize) : (int)(fileStream.Length / dataSize) + 1;

            progressBar1.Maximum = packetCnt;

            try
            {
                if (!sendYmodemInitialPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, filePath, fileStream, CRC, crcSize))
                {
                    return(false);
                }

                if (serialPort1.ReadByte() != ACK)
                {
                    Console.WriteLine("Can't send the initial packet.");
                    return(false);
                }

                if (serialPort1.ReadByte() != C)
                {
                    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 */
                    if (!sendYmodemPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize))
                    {
                        return(false);
                    }
                    progressBar1.Value = ++proprassVal;
                    /* wait for ACK */
                    if (serialPort1.ReadByte() != ACK)
                    {
                        Console.WriteLine("Couldn't send a packet.");
                        return(false);
                    }
                } while (dataSize == fileReadCount);

                /* send EOT (tell the downloader we are finished) */
                serialPort1.Write(new byte[] { EOT }, 0, 1);
                /* send closing packet */
                packetNumber         = 0;
                invertedPacketNumber = 255;
                data = new byte[dataSize];
                CRC  = new byte[crcSize];
                if (!sendYmodemClosingPacket(STX, packetNumber, invertedPacketNumber, data, dataSize, CRC, crcSize))
                {
                    return(false);
                }
                /* get ACK (downloader acknowledge the EOT) */
                if (serialPort1.ReadByte() != ACK)
                {
                    Console.WriteLine("Can't complete the transfer.");
                    return(false);
                }
            }
            catch (TimeoutException)
            {
                progressBar1.Value = 0;
                MessageBox.Show("数据传输超时");
            }
            catch (InvalidOperationException)
            {
                progressBar1.Value = 0;
                MessageBox.Show("端口打开失败");
            }
            catch (IOException)
            {
                progressBar1.Value = 0;
                MessageBox.Show("端口被中断,请检查连接");
            }
            finally
            {
                fileStream.Close();
            }

            Console.WriteLine("File transfer is succesful");
            return(true);
        }