From b39ea3815b054bb2fcd1f2203eb3e18373d27550 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Fri, 30 Oct 2020 11:58:21 +0100 Subject: [PATCH] Fix gestioen ,/. per float ThermoCam --- .../ThermocameraComunicator.cs | 310 ++++++++++-------- 1 file changed, 167 insertions(+), 143 deletions(-) diff --git a/THermo.Active.Thermocamera/ThermocameraComunicator.cs b/THermo.Active.Thermocamera/ThermocameraComunicator.cs index f78b0bd0..c3246746 100644 --- a/THermo.Active.Thermocamera/ThermocameraComunicator.cs +++ b/THermo.Active.Thermocamera/ThermocameraComunicator.cs @@ -14,38 +14,39 @@ using Thermo.Active.Model.DTOModels.ThWarmers; namespace Thermo.Active.Thermocamera { - public class ThermocameraComunicator { - const string MMF_REQ = "CMS_MMF_REQ"; - const string MMF_RES = "CMS_MMF_RES"; - const int NUM_CHAR_MSG = 5000; - const int REQ = 1; - const int RES = 2; - const int DIM_MMF = NUM_CHAR_MSG * 2 + 2; + #region Private Fields - MemoryMappedFile mmf; - MemoryMappedFile mmfRes; - MemoryMappedViewAccessor accessor; - MemoryMappedViewAccessor accessorResp; + private const int DIM_MMF = NUM_CHAR_MSG * 2 + 2; + private const string MMF_REQ = "CMS_MMF_REQ"; + private const string MMF_RES = "CMS_MMF_RES"; + private const int NUM_CHAR_MSG = 5000; + private const int REQ = 1; + private const int RES = 2; + private static ThermocameraComunicator _instance; + private MemoryMappedViewAccessor accessor; + private MemoryMappedViewAccessor accessorResp; + private MemoryMappedFile mmf; + private MemoryMappedFile mmfRes; + + #endregion Private Fields + + #region Protected Fields - /// - /// Booleana dell'uso sw ext: true = Utilizzo vero sw lettura FLIR / false = simulazione SW RAND - /// - protected bool useTCamSw = true; /// /// Generatore random /// protected Random rnd = new Random(); - private static ThermocameraComunicator _instance; + /// + /// Booleana dell'uso sw ext: true = Utilizzo vero sw lettura FLIR / false = simulazione SW RAND + /// + protected bool useTCamSw = true; - public static ThermocameraComunicator getInstance() - { - if (_instance == null) - _instance = new ThermocameraComunicator(); - return _instance; - } + #endregion Protected Fields + + #region Private Constructors private ThermocameraComunicator() { @@ -54,80 +55,95 @@ namespace Thermo.Active.Thermocamera accessor = mmf.CreateViewAccessor(); accessorResp = mmfRes.CreateViewAccessor(); } + + #endregion Private Constructors + + #region Private Properties + /// - /// Richiesta di scattare fotografia + /// Simulazione valore tra 100 e 300 °C /// - /// attesa in ms, 10000 std, da conf - /// - public bool takePicture(int timeoutMS) + private float simVal { - // modalità impiego vero sw esterno - if (useTCamSw) + get { - const string tempCommand = "SetParameter_Integer"; - string response; + float answ = 100 + 200 * (float)rnd.NextDouble(); + return answ; + } + } - if (!writeCommand(accessor, tempCommand + ";TAKE_IMAGE;")) - return false; - if (!readCommand(accessorResp, tempCommand, timeoutMS, out response)) - return false; - if (response != "True;") - return false; - } - else + #endregion Private Properties + + #region Private Methods + + private bool readCommand(MemoryMappedViewAccessor accessor, string command, int timeoutMS, out string resp) + { + resp = ""; + byte response = 0; + int totalCycle = 0; + int sleepTime = 500; + byte[] bytesToRead = new byte[NUM_CHAR_MSG]; + + if (!accessor.CanRead) return false; + + //Read Pre-Command + while (response != RES) { - // attende 5 sec - Thread.Sleep(5000); + totalCycle++; + accessor.Read(0, out response); + if (response != RES) + Thread.Sleep(sleepTime); + if (totalCycle * sleepTime > timeoutMS) + return false; } + + //Read Command + accessor.ReadArray(1, bytesToRead, 0, bytesToRead.Length); + + //Elaborate String + string textRecieved = Encoding.UTF8.GetString(bytesToRead); + if (textRecieved == null) return false; + + string[] textSplitted = textRecieved.Replace("\0", string.Empty).Split(';'); + if (textSplitted.Length < 2) return false; + if (textSplitted[0] != command) return false; + + char[] bufferToClean = Enumerable.Repeat('\0', NUM_CHAR_MSG).ToArray(); + accessor.WriteArray(0, bufferToClean, 0, bufferToClean.Length); + + //Output + textSplitted = textSplitted.Skip(1).ToArray(); + resp = String.Join(";", textSplitted); return true; } - public bool showWindow(int x, int y, int dimX, int dimY, int timeoutMS) + + private bool writeCommand(MemoryMappedViewAccessor accessor, string command) { - const string tempCommand = "ShowWindow"; - string response; - if (!writeCommand(accessor, tempCommand + ";" + x + ";" + y + ";" + dimX + ";" + dimY + ";")) - return false; - if (!readCommand(accessorResp, tempCommand, timeoutMS, out response)) - return false; - if (response != "1;") - return false; + char[] bufferToClean = Enumerable.Repeat('\0', NUM_CHAR_MSG).ToArray(); + accessor.WriteArray(0, bufferToClean, 0, bufferToClean.Length); + + char[] bufferToWrite = command.ToCharArray(); + + //Write Pre-Command + accessor.Write(0, REQ); + + //Write Command + accessor.WriteArray(1, bufferToWrite, 0, bufferToWrite.Length); + return true; } - /// - /// Lettura temp singolo punto - /// - /// - /// - /// - /// - /// - public bool readTemperature(int x, int y, int timeoutMS, out float temp) + + #endregion Private Methods + + #region Public Methods + + public static ThermocameraComunicator getInstance() { - temp = 0f; - - // modalità impiego vero sw esterno - if (useTCamSw) - { - const string tempCommand = "GetTemperature"; - string response; - - if (!writeCommand(accessor, tempCommand + ";" + x + ";" + y + ";")) - return false; - if (!readCommand(accessorResp, tempCommand, timeoutMS, out response)) - return false; - response = response.TrimEnd(';'); - if (!float.TryParse(response, NumberStyles.Float, CultureInfo.InvariantCulture, out temp)) - return false; - } - else - { - // simulo! - temp = simVal; - // attende 5 sec - Thread.Sleep(5000); - } - return true; + if (_instance == null) + _instance = new ThermocameraComunicator(); + return _instance; } + /// /// Restituisce lettura di tutti i punti (cetroidi) richeisti /// @@ -184,75 +200,83 @@ namespace Thermo.Active.Thermocamera return true; } - - private bool writeCommand(MemoryMappedViewAccessor accessor, string command) + /// + /// Lettura temp singolo punto + /// + /// + /// + /// + /// + /// + public bool readTemperature(int x, int y, int timeoutMS, out float temp) { - char[] bufferToClean = Enumerable.Repeat('\0', NUM_CHAR_MSG).ToArray(); - accessor.WriteArray(0, bufferToClean, 0, bufferToClean.Length); + temp = 0f; - char[] bufferToWrite = command.ToCharArray(); - - //Write Pre-Command - accessor.Write(0, REQ); - - //Write Command - accessor.WriteArray(1, bufferToWrite, 0, bufferToWrite.Length); - - return true; - } - - - private bool readCommand(MemoryMappedViewAccessor accessor, string command, int timeoutMS, out string resp) - { - resp = ""; - byte response = 0; - int totalCycle = 0; - int sleepTime = 500; - byte[] bytesToRead = new byte[NUM_CHAR_MSG]; - - if (!accessor.CanRead) return false; - - //Read Pre-Command - while (response != RES) + // modalità impiego vero sw esterno + if (useTCamSw) { - totalCycle++; - accessor.Read(0, out response); - if (response != RES) - Thread.Sleep(sleepTime); - if (totalCycle * sleepTime > timeoutMS) + const string tempCommand = "GetTemperature"; + string response; + + if (!writeCommand(accessor, tempCommand + ";" + x + ";" + y + ";")) + return false; + if (!readCommand(accessorResp, tempCommand, timeoutMS, out response)) + return false; + response = response.TrimEnd(';'); + if (!float.TryParse(response, NumberStyles.Float, CultureInfo.InvariantCulture, out temp)) return false; } - - //Read Command - accessor.ReadArray(1, bytesToRead, 0, bytesToRead.Length); - - //Elaborate String - string textRecieved = Encoding.UTF8.GetString(bytesToRead); - if (textRecieved == null) return false; - - string[] textSplitted = textRecieved.Replace("\0", string.Empty).Split(';'); - if (textSplitted.Length < 2) return false; - if (textSplitted[0] != command) return false; - - char[] bufferToClean = Enumerable.Repeat('\0', NUM_CHAR_MSG).ToArray(); - accessor.WriteArray(0, bufferToClean, 0, bufferToClean.Length); - - //Output - textSplitted = textSplitted.Skip(1).ToArray(); - resp = String.Join(";", textSplitted); + else + { + // simulo! + temp = simVal; + // attende 5 sec + Thread.Sleep(5000); + } return true; } - /// - /// Simulazione valore tra 100 e 300 °C - /// - private float simVal + + public bool showWindow(int x, int y, int dimX, int dimY, int timeoutMS) { - get - { - float answ = 100 + 200 * (float)rnd.NextDouble(); - return answ; - } + const string tempCommand = "ShowWindow"; + string response; + if (!writeCommand(accessor, tempCommand + ";" + x + ";" + y + ";" + dimX + ";" + dimY + ";")) + return false; + if (!readCommand(accessorResp, tempCommand, timeoutMS, out response)) + return false; + if (response != "1;") + return false; + return true; } + /// + /// Richiesta di scattare fotografia + /// + /// attesa in ms, 10000 std, da conf + /// + public bool takePicture(int timeoutMS) + { + // modalità impiego vero sw esterno + if (useTCamSw) + { + const string tempCommand = "SetParameter_Integer"; + string response; + + if (!writeCommand(accessor, tempCommand + ";TAKE_IMAGE;")) + return false; + if (!readCommand(accessorResp, tempCommand, timeoutMS, out response)) + return false; + if (response != "True;") + return false; + } + else + { + // attende 5 sec + Thread.Sleep(5000); + } + return true; + } + + #endregion Public Methods } -} +} \ No newline at end of file