Ejemplo n.º 1
0
        public void CanSerializeHeader()
        {
            ComFileHeader header = new ComFileHeader(LoadAddress, ObjectSize);

            byte[] data = header.ToBytes();

            Assert.AreEqual(ComFileHeader.HEADER_SIZE, Marshal.SizeOf(typeof(ComFileHeader)), "COM header size should be 10");
            CollectionAssert.AreEqual(comFileHeaderBuffer, data);
        }
Ejemplo n.º 2
0
        public void CanDeserializeHeader()
        {
            ComFileHeader header = ComFileHeader.FromBytes(comFileHeaderBuffer);

            Assert.AreEqual(0x80, header.MagicBytes[0]);
            Assert.AreEqual(0x08, header.MagicBytes[1]);
            Assert.AreEqual(0x0200, header.LoadAddress);
            Assert.AreEqual(0x12D1, header.ObjectSize);
            Assert.AreEqual("BS93", Encoding.ASCII.GetString(header.Bs93Signature));
        }
Ejemplo n.º 3
0
        public void CanInitializeHeader()
        {
            ushort address = 0x0200;
            ushort size    = 0x12D1;

            ComFileHeader header = new ComFileHeader(address, size);

            Assert.AreEqual(address, header.LoadAddress);
            Assert.AreEqual(size, header.ObjectSize);
        }
Ejemplo n.º 4
0
        public void UploadComFile(string portName, byte[] file, int baudRate = 62500)
        {
            ComFileHeader header = ComFileHeader.FromBytes(file);

            //if (!header.Verify())
            //{
            //	Console.WriteLine("Invalid Lynx Com file");
            //}

            UploadCore(portName, header, file, ComFileHeader.HEADER_SIZE,
                       file.Length - ComFileHeader.HEADER_SIZE, baudRate);
        }
Ejemplo n.º 5
0
        protected void UploadCore(string portName, ComFileHeader header, byte[] program, int offset, int count, int baudRate)
        {
            UploadDebugMessage message = new UploadDebugMessage(header.LoadAddress, header.ObjectSize);

            using (SerialPort port = new SerialPort(portName, baudRate, Parity.Even, 8, StopBits.One))
            {
                port.WriteTimeout = WRITE_TIMEOUT;
                port.Handshake    = Handshake.None;

                if (!port.TryOpen())
                {
                    return;
                }

                // Write command
                byte[] messageBytes = message.ToBytes();
                port.Write(messageBytes, 0, messageBytes.Length);

                int bytesSent = 0;
                while (bytesSent < header.ObjectSize)
                {
                    // Send single chunk
                    int chunkSize = Math.Min(header.ObjectSize - bytesSent, WRITE_CHUNKSIZE);
                    port.Write(program, offset + bytesSent, chunkSize);
                    bytesSent += chunkSize;

                    // Report progress
                    int percentage = (bytesSent * 100) / header.ObjectSize;
                    ProgressChanged?.Invoke(this, new ProgressChangedEventArgs(percentage, chunkSize));
                }

                if (port.IsOpen)
                {
                    port.Close();
                }
            }
        }