public Device[] AcquireDevicesOnSerialPort(SerialPort sp, byte max_addr)
        {
            Console.WriteLine("\nSending PING to serial port {0}... ", sp.PortName);
            Console.CursorVisible = false;

            bool intro = true;
            int  cx = 0, cy = 0;

            int timeout = 100;
            int counter = 0;

            sp.ReadTimeout = 20;
            byte[]           buffer    = new byte[1024];
            MessageExtractor me        = new MessageExtractor();
            List <Device>    endpoints = new List <Device>();

            // scan through 0x00 - 0xEF. Range 0xF0 - 0xFF is reserved
            max_addr = Math.Min(max_addr, (byte)0xF0);

            for (int i = 0x00; i < max_addr; i++)
            {
                //i = 0x12;
                if (intro)
                {
                    Console.Write(" Looking for device ");
                    cx    = Console.CursorLeft;
                    cy    = Console.CursorTop;
                    intro = false;
                }

                Console.SetCursorPosition(cx, cy);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("0x{0:X2}", i);
                Console.ForegroundColor = ConsoleColor.Gray;


                // send ping do selected device
                sp.DiscardInBuffer();
                sp.DiscardOutBuffer();
                me.Discard();

                Message ping_message = new Message((byte)i, MessageType.Ping);
                Message msg          = SendAndWaitForResponse(new Device(sp, i), ping_message, timeout, false, 0);

                if (msg != null)
                {
                    Console.WriteLine(" Found!");
                    counter++;
                    intro = true;
                    endpoints.Add(new Device(sp, i));
                }

                //break;
            }

            Console.SetCursorPosition(0, cy);
            Console.WriteLine(" Done. Found {0} devices on serial port {1}.", counter, sp.PortName);
            Console.CursorVisible = true;
            return(endpoints.ToArray());
        }
示例#2
0
        private void Ack(object obj)
        {
            ScanningTask task = obj as ScanningTask;
            int          col  = 0;

            ColorConsole.WriteXY(0, task.Row, task.SerialPort.PortName);
            col += 3 + 3 + 1;

            var sp        = task.SerialPort;
            var me        = new MessageExtractor();
            int timeout   = 100;
            var endpoints = new List <BootloaderClient>();

            foreach (byte scanned_address in task.AddressList)
            {
                // Show scanned address
                int ccol = col;
                ColorConsole.WriteXY(ccol, task.Row, ConsoleColor.Green, $"0x{scanned_address:X2}");

                // send ping do selected device
                sp.DiscardInBuffer();
                sp.DiscardOutBuffer();
                me.Discard();

                Message ping_message = new Message(scanned_address, MessageType.Ping);
                Message msg          = SendAndWaitForResponse(new BootloaderClient(sp, scanned_address), ping_message, timeout, false, 0);

                if (msg != null)
                {
                    endpoints.Add(new BootloaderClient(sp, scanned_address));
                }


                // Show list of found clients
                ccol += 5;
                ColorConsole.WriteXY(ccol, task.Row, ConsoleColor.Yellow, "[" + string.Join(",", endpoints.Select(x => x.BootloaderAddress.ToString("X2"))) + "]");
            }

            lock (task.EndpointsCollection)
                task.EndpointsCollection.AddRange(endpoints);
        }