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()); }
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); }
private Message SendAndWaitForResponse(BootloaderClient ep, Message request, int timeout, bool throw_timeout_exception = true, int retries = 3) { Debug.Assert(ep.BootloaderAddress == request.Address); MessageExtractor me = new MessageExtractor(); byte[] buffer = new byte[1024]; byte[] empty = new byte[128 + 4 + 5]; Message msg = null; while (retries-- >= 0) { // setup serial port ep.Port.DiscardInBuffer(); ep.Port.DiscardOutBuffer(); ep.Port.ReadTimeout = 200; // send data ep.Port.Write(request.Binary, 0, request.BinarySize); Thread.Sleep(0); //Debug.WriteLine("sent " + request.BinarySize.ToString()); // and wait for response DateTime start = DateTime.Now; do { int read = -1; try { read = ep.Port.Read(buffer, 0, buffer.Length); } catch (TimeoutException tex) { Debug.WriteLine("TO"); continue; // ignore timeouts } catch (Exception ex) { Debug.WriteLine("EX"); break; // shit happens } me.AddData(buffer, read); if (me.TryExtract(ref msg, request.Address, request.Type)) { break; // ok, got message! } } while ((DateTime.Now - start).TotalMilliseconds <= timeout && timeout != -1); // if message was correctly received then stop communication if (msg != null) { break; } Debug.WriteLine("RETRY"); } if (msg == null && throw_timeout_exception) { throw new TimeoutException(string.Format("No response from bootloader device 0x{0:X2} on {1}", ep.BootloaderAddress, ep.Port.PortName)); } return(msg); }