[chronojump/FS-TFT-Menu] New class: ArduinoDiscover to discover devices, also timed waitResponse on Wichro



commit ba09a1c11b776fab4cbe43637c401616e2d38474
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Apr 27 17:22:16 2022 +0200

    New class: ArduinoDiscover to discover devices, also timed waitResponse on Wichro

 src/chronopicRegister.cs      |   2 +
 src/execute/arduinoCapture.cs | 214 ++++++++++++++++++++++++++++++++----------
 2 files changed, 165 insertions(+), 51 deletions(-)
---
diff --git a/src/chronopicRegister.cs b/src/chronopicRegister.cs
index 484b0ab84..01e3991a5 100644
--- a/src/chronopicRegister.cs
+++ b/src/chronopicRegister.cs
@@ -34,6 +34,8 @@ public class ChronopicRegisterPort
        public string Port;
        public bool FTDI;
        public string SerialNumber;
+
+       //Note: if this changes, change also on execute/arduinoCapture.cs
        public enum Types { UNKNOWN, CONTACTS, ENCODER, ARDUINO_RFID, ARDUINO_FORCE, ARDUINO_RUN_ENCODER, 
ACCELEROMETER, RUN_WIRELESS }
        public Types Type;
 
diff --git a/src/execute/arduinoCapture.cs b/src/execute/arduinoCapture.cs
index 78a71c393..67a87ae06 100644
--- a/src/execute/arduinoCapture.cs
+++ b/src/execute/arduinoCapture.cs
@@ -13,7 +13,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  *
- * Copyright (C) 2021  Xavier de Blas <xaviblas gmail com>
+ * Copyright (C) 2022  Xavier de Blas <xaviblas gmail com>
  */
 
 using System;
@@ -24,46 +24,20 @@ using System.Threading;
 
 //inspired on RFID.cs (unique class that reads arduino separated of gui
 
-public abstract class ArduinoCapture
+//ArduinoCommunications
+public abstract class ArduinoComms
 {
        public static SerialPort ArduinoPort; //on Windows we cannot pass the SerialPort to another class, so 
use this.
        public static bool PortOpened;
 
-
        protected string portName;
        protected int bauds;
 //     protected SerialPort port;
 //     protected bool portOpened;
-       protected int readedPos; //position already readed from list
-
-       // public stuff ---->
 
-       public abstract bool CaptureStart();
-       public abstract bool CaptureLine();
-       public abstract bool Stop();
-       public abstract bool CanRead();
+       protected string response; //the response on waitResponse () if is what expected
 
-       //have methods for get objects on each of the derived classes
-       public abstract List<PhotocellWirelessEvent> PhotocellWirelessCaptureGetList();
-       public abstract PhotocellWirelessEvent PhotocellWirelessCaptureReadNext();
-
-       public int ReadedPos
-       {
-               get { return readedPos; }
-       }
-
-       // protected stuff ---->
-
-       protected void initialize (string portName, int bauds)
-       {
-               this.portName = portName;
-               this.bauds = bauds;
-               readedPos = 0;
-
-               emptyList();
-       }
-
-       protected bool portConnect()
+       protected bool portConnect ()
        {
                //port = new SerialPort(portName, bauds);
                ArduinoPort = new SerialPort(portName, bauds);
@@ -80,15 +54,19 @@ public abstract class ArduinoCapture
                LogB.Information("port successfully opened");
 
                //TODO: Val, caldria que quedés clar a la interficie que estem esperant aquest temps, a veure 
com ho fa el sensor de força, ...
-               //just print on gui somthing like "please, wait, ..."
+               //just print on gui something like "please, wait, ..."
                //
                Thread.Sleep(3000); //sleep to let arduino start reading serial event
 
-               if(! sendCommand("local:get_version;", "error getting version"))
+               return true;
+       }
+
+       protected bool getVersion (string getVersionStr, List<string> responseExpected_l)
+       {
+               if(! sendCommand(getVersionStr, "error getting version")) //note this is for Wichro
                        return false;
-               waitResponse("Wifi-Controller");
 
-               return true;
+               return waitResponse(responseExpected_l);
        }
 
        protected bool sendCommand (string command, string errorMessage)
@@ -112,25 +90,93 @@ public abstract class ArduinoCapture
                return true;
        }
 
-       protected void waitResponse (string expected)
+       protected bool waitResponse (List<string> responseExpected_l)
        {
                string str = "";
+               bool success = false;
+               int waitLimitMs = 2000; //wait 2s //don't wait 1s because response will not come
+               Stopwatch sw = new Stopwatch();
+               sw.Start();
+               LogB.Information("starting waitResponse");
                do {
                        Thread.Sleep(25);
                        if (ArduinoPort.BytesToRead > 0)
                        {
                                try {
-                                       str = ArduinoPort.ReadLine();
+                                       //str = ArduinoPort.ReadLine();
+                                       //use this because if 9600 call an old Wichro that has no comm at 
this speed, will answer things and maybe never a line
+                                       str += ArduinoPort.ReadExisting(); //The += is because maybe it 
receives part of the string
                                } catch {
-                                       LogB.Information(string.Format("Catched waiting: |{0}|", expected));
+                                       if(responseExpected_l.Count == 1)
+                                               LogB.Information(string.Format("Catched waiting: |{0}|", 
responseExpected_l[0]));
+                                       else if(responseExpected_l.Count > 1)
+                                       {
+                                               LogB.Information("Catched waiting any of:");
+                                               foreach(string expected in responseExpected_l)
+                                                       LogB.Information("- " + expected);
+
+                                       }
+                                       return false;
                                }
-                               //LogB.Information(string.Format("waiting \"{0}\", received: {1}", expected, 
str));
+                               LogB.Information(string.Format("received: |{0}|", str));
                        }
+
+                       foreach(string expected in responseExpected_l)
+                               if(str.Contains(expected))
+                               {
+                                       success = true;
+                                       response = str;
+                               }
                }
-               while(! str.Contains(expected));
-               LogB.Information("waitResponse success: " + str);
+               while(! (success || sw.Elapsed.TotalMilliseconds > waitLimitMs) );
+               LogB.Information("ended waitResponse");
+
+               return (success);
+       }
+
+       protected void flush ()
+       {
+               string str = "";
+               if (ArduinoPort.BytesToRead > 0)
+                       str = ArduinoPort.ReadExisting();
+
+               LogB.Information(string.Format("flushed: |{0}|", str));
+       }
+}
+
+public abstract class ArduinoCapture : ArduinoComms
+{
+       protected int readedPos; //position already readed from list
+
+       // public stuff ---->
+
+       public abstract bool CaptureStart();
+       public abstract bool CaptureLine();
+       public abstract bool Stop();
+       public abstract bool CanRead();
+
+       //have methods for get objects on each of the derived classes
+       public abstract List<PhotocellWirelessEvent> PhotocellWirelessCaptureGetList();
+       public abstract PhotocellWirelessEvent PhotocellWirelessCaptureReadNext();
+
+       public int ReadedPos
+       {
+               get { return readedPos; }
        }
 
+       // protected stuff ---->
+
+       protected void initialize (string portName, int bauds)
+       {
+               this.portName = portName;
+               this.bauds = bauds;
+               readedPos = 0;
+               response = "";
+
+               emptyList();
+       }
+
+
        protected bool readLine (out string str)
        {
                str = "";
@@ -187,8 +233,15 @@ public class PhotocellWirelessCapture: ArduinoCapture
                LogB.Information("portOpened: " + ArduinoCapture.PortOpened.ToString());
                // 0 connect if needed
                if(! ArduinoCapture.PortOpened)
-                       if(! portConnect())
+               {
+                       List<string> responseExpected_l = new List<string>();
+                       responseExpected_l.Add("Wifi-Controller");
+
+                       if(! portConnect ())
+                               return false;
+                       if(! getVersion ("local:get_version;", responseExpected_l))
                                return false;
+               }
 
                ArduinoCapture.PortOpened = true;
 
@@ -225,15 +278,6 @@ public class PhotocellWirelessCapture: ArduinoCapture
                return true;
        }
 
-       private void flush ()
-       {
-               string str = "";
-               if (ArduinoPort.BytesToRead > 0)
-                       str = ArduinoPort.ReadExisting();
-
-               LogB.Information(string.Format("flushed: |{0}|", str));
-       }
-
        public override bool Stop()
        {
                LogB.Information("AT Capture: STOPPING");
@@ -351,3 +395,71 @@ public class PhotocellWirelessEvent
                return (string.Format("{0};{1};{2}", photocell, timeMs, status));
        }
 }
+
+//New firmwares enable communication at 9600 (event devices working at higher speeds) to get the version 
(contains the product)
+public class ArduinoDiscover : ArduinoComms
+{
+       protected ChronopicRegisterPort.Types discovered;
+
+       public ArduinoDiscover (string portName)
+       {
+               this.portName = portName;
+               discovered = ChronopicRegisterPort.Types.UNKNOWN;
+       }
+
+       public ChronopicRegisterPort.Types Discover ()
+       {
+               if(! connect ())
+                       return discovered;
+
+               discoverDo ();
+               if(discovered == ChronopicRegisterPort.Types.UNKNOWN)
+                       discoverOldWichros ();
+
+               ArduinoPort.Close();
+               return discovered;
+       }
+
+       private bool connect ()
+       {
+               this.bauds = 9600;
+               return portConnect();
+       }
+
+       // check with common get_version (any device except the first Wichros)
+       private void discoverDo ()
+       {
+               string forceSensorStr = "Force_Sensor-";
+               string raceAnalyzerStr = "Race_Analyzer-";
+               string wichroStr = "Wichro";
+
+               List<string> responseExpected_l = new List<string>();
+               responseExpected_l.Add(forceSensorStr);
+               responseExpected_l.Add(raceAnalyzerStr);
+               responseExpected_l.Add(wichroStr);
+
+               if(getVersion ("get_version:", responseExpected_l))
+               {
+                       LogB.Information("Discover found this device: " + response);
+                       if(response.Contains(forceSensorStr))
+                               discovered = ChronopicRegisterPort.Types.ARDUINO_FORCE;
+                       else if(response.Contains(raceAnalyzerStr))
+                               discovered = ChronopicRegisterPort.Types.ARDUINO_RUN_ENCODER;
+                       else if(response.Contains(wichroStr))
+                               discovered = ChronopicRegisterPort.Types.RUN_WIRELESS;
+               }
+               flush(); //empty the port for future use
+       }
+
+       // check if it is an old Wichro (has different get_version command)
+       private void discoverOldWichros ()
+       {
+               List<string> responseExpected_l = new List<string>();
+               responseExpected_l.Add("Wichro");
+
+               if(getVersion ("local:get_version;", responseExpected_l))
+                       discovered = ChronopicRegisterPort.Types.RUN_WIRELESS;
+
+               flush(); //empty the port for future use
+       }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]