Exemple #1
0
        public void TestConnect()
        {
            var serialPort = new SerialPort("COM5", 250000);

            serialPort.ReadTimeout  = SerialPort.InfiniteTimeout;
            serialPort.WriteTimeout = SerialPort.InfiniteTimeout;
            serialPort.Encoding     = Encoding.ASCII;
            serialPort.Open();

            var ser = new SerialQueue(serialPort);

            ser.Start();

            MemoryStream buffer = new MemoryStream();
            int          res    = 0;

            var          cq  = new command_queue();
            MemoryStream msg = new MemoryStream(16);

            while (true)
            {
                QueueMessage qm;
                ser.pull(out qm);

                Console.WriteLine($"MSG {qm.sent_time}/{qm.receive_time} {(int)((qm.receive_time - qm.sent_time) * 1000)}");

                msg.Position = 0;
                msg.SetLength(0);
                for (var i = SerialQueue.MESSAGE_HEADER_SIZE;
                     i < qm.len - SerialQueue.MESSAGE_TRAILER_SIZE; i++)
                {
                    msg.WriteByte(qm.msg[i]);
                }
                msg.Position = 0;
                var code = msg.ReadByte();

                if (code == 0)
                {
                    Console.WriteLine("identify_response");
                    var offset = msg.parse(false);
                    var len    = msg.ReadByte();
                    if (len > 0)
                    {
                        var buf  = new byte[len];
                        var read = msg.Read(buf, 0, len);
                        if (read != len)
                        {
                            Console.WriteLine("Error");
                        }

                        buffer.Write(buf, 0, read);
                        res += read;

                        Console.WriteLine($"Read bytes {buffer.Length}; {read}/{len}");
                    }
                    if (len < 50)
                    {
                        Console.WriteLine("download done");
                        res = -1;

                        //var final = ZlibDecompress(buffer.ToArray());
                        //var jsonText = Encoding.ASCII.GetString(final);
                        //var xml = XDocument.Load(JsonReaderWriterFactory.CreateJsonReader(Encoding.ASCII.GetBytes(jsonText), new XmlDictionaryReaderQuotas()));
                        //xml.Save("identify.xml");

                        Assert.AreEqual(PROGMEM, buffer.ToArray());

                        return;
                    }
                }
                else
                {
                    Console.WriteLine($"{qm.receive_time * 1000} code:{code}");
                }

                if (res != -1)
                {
                    Console.WriteLine($"identify get data {res}");
                    //{ 1, "identify offset=%u count=%c" }, uint32, byte
                    msg.Position = 0;
                    msg.SetLength(0);
                    msg.WriteByte(1);
                    msg.encode(res);
                    msg.encode((byte)50);
                    ser.send(cq, msg.ToArray());
                }
            }
        }