/******************************************************************************* * This class is wrapper for the dll's of the messure modules HB627 and HB628 * * manufactured by Htronic (www.www.h-tronic.de). * * * * Author: Eike Herwig * * * * Changelog: 2012-09-05, Version 1.0, initial, only HB627 * * 2012-11-02, Version 1.1, enhanced to HB628 * * * *******************************************************************************/ using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Diagnostics; using System.IO.Ports; namespace HtronicMessureModule { public enum eWichModule { HB627, // Modul HB627 -> without output ports HB628 // Modul HB628 -> with output orts } public enum eErrorKey { ERR_NO = 0, // no error ERR_TX, // Tx error ERR_RX, // Rx error ERR_CS, // control sum error ERR_PARAM, // parameters value error ERR_DEFAULT // default error, set by this class, not implemented in the dll's } public enum eChannel : byte // Messure channels { Channel_1 = 1, Channel_2, Channel_3, Channel_4, Channel_5, Channel_6, Channel_7, Channel_8 } public enum eSwitchPort : byte // Digital output ports { Port_1 =1, Port_2, Port_3, Port_4, Port_5, Port_6, Port_7, Port_8 } public enum eRange { MinMilliVolt = 0, // No negative voltage, minimum is 0mV MaxMilliVolt = 4095 // maximal voltage value of the moduls is 4095mV, this means a resolution of 1mV per digit } public enum eTimeoutIO : byte { TimeoutEnabled = 1, TimeoutDisabled = 0 } public enum ePortState : byte { PortOff = 0, PortOn = 1 } class HtronicMessmodul { private const eErrorKey DefaultErrorkey = eErrorKey.ERR_DEFAULT; private const byte DefaultComport = 0; private const Int32 DefaultTimeout = 10; private eErrorKey eerrorkey; private eWichModule ewichModule; private eRange erange; private eChannel echannel; private eSwitchPort eswitchport; private eTimeoutIO etimeoutio; private ePortState eportstate; private bool isOpen = false; private byte comport; private Int32 timeout; /**********************************Functions in DLL for Module HB627**********************************/ [DllImport("hb627.dll", EntryPoint = "HB627_Open")] private static extern bool bOpenHB627( System.Byte comport, System.Int32 timeout ); [DllImport("hb627.dll", EntryPoint = "HB627_Close")] private static extern bool bCloseHB627(); [DllImport("hb627.dll", EntryPoint = "HB627_Read_Channel")] private static extern bool bReadHB627Channel(System.Byte channel, out int result, out string error); [DllImport("hb627.dll", EntryPoint = "HB627_Read_All_Channel")] private static extern bool bReadHB627AllChannel([In, Out] short[] Result_Array, out string error); /**********************************Functions in DLL for Module HB628**********************************/ [DllImport("hb628.dll", EntryPoint = "HB628_Open")] private static extern bool bOpenHB628(System.Byte comport, System.Int32 timeout); [DllImport("hb628.dll", EntryPoint = "HB628_Close")] private static extern bool bCloseHB628(); [DllImport("hb628.dll", EntryPoint = "HB628_Read_Channel")] private static extern bool bReadHB628Channel(System.Byte channel, out int result, out string error); [DllImport("hb628.dll", EntryPoint = "HB628_Read_All_Channel")] private static extern bool bReadHB628AllChannel([In, Out] short[] Result_Array, out string error); [DllImport("hb628.dll", EntryPoint = "HB628_Set_All_Digital_Output")] private static extern bool bSetHB628AllDigitalOutputs(System.Byte output, out string error); [DllImport("hb628.dll", EntryPoint = "HB628_Set_Digital_Output")] private static extern bool bSetHB628DigitalOutput(System.Byte channel, System.Byte value, out string error); [DllImport("hb628.dll", EntryPoint = "HB628_Set_TimeOut")] private static extern bool bSetHB628TimeOut(System.Byte value, out string error); /**Constructors***********************************************************************/ public HtronicMessmodul() : this (DefaultComport, DefaultTimeout) { } public HtronicMessmodul(byte comport) : this(comport, DefaultTimeout) { } public HtronicMessmodul(Int32 timeout) : this(DefaultComport, timeout) { } public HtronicMessmodul(byte Comport, Int32 timeout) { this.comport = Comport; this.timeout = timeout; } /**Get/Set***************************************************************************/ public eErrorKey eErrorKey { get { return eerrorkey; } set { eerrorkey = value; } } public eWichModule eWichModule { get { return ewichModule; } set { ewichModule = value; } } public eChannel eChannel { get { return echannel; } set { echannel = value; } } public eSwitchPort eSwitchPort { get { return eswitchport; } set { eswitchport = value; } } public eRange eRange { get { return erange; } set { erange = value; } } public eTimeoutIO eTimeoutIO { get { return etimeoutio; } set { etimeoutio = value; } } public ePortState ePortState { get { return eportstate; } set { eportstate = value; } } public bool IsOpen { get { return isOpen; } } public byte Comport { get { return comport; } set { if (isOpen) throw new InvalidOperationException("Setting port number while port is open is not possible"); if (value < 1) throw new ArgumentException("Value must be greater than 0"); comport = value; } } public Int32 Timeout { get { return timeout; } set { timeout = value; } } /* Class helpers**********************************************************************/ private static void vCatchedExeptionMessage(string message, string functionName) { /* comment in/out the needed function (both, one of them or none) */ //Debug..WriteLine("Exception" + message + "\n\nin Function: " + functionName + "\n"); Console.WriteLine("Exception" + message + "\n\nin Function: " + functionName + "\n"); } private static string sEvaluateErrorKey(string errorkey) { switch (errorkey) { case "0": case null: return eErrorKey.ERR_NO.ToString(); case "1": return eErrorKey.ERR_TX.ToString(); case "2": return eErrorKey.ERR_TX.ToString(); case "3": return eErrorKey.ERR_CS.ToString(); case "4": return eErrorKey.ERR_PARAM.ToString(); default: return eErrorKey.ERR_DEFAULT.ToString(); } } /**Methods for both modules************************************************************/ public bool bSetOpenModule(eWichModule modul, System.Int32 timeout) { if (isOpen) throw new InvalidOperationException("The choosen port is already open"); // Check if a comport is choosen try { if (this.comport < 1) throw (new ModulBasedException("No choosen comport")); // no comport, throw exeption } catch(ModulBasedException e) { vCatchedExeptionMessage(e.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()); return false; } #region ToDo -----> Prüfung ob Comport belegt // Check if the choosen comport is not used // TODO Prüfung ob Port offen funioniert so noch nicht try { SerialPort port = new SerialPort(this.comport.ToString()); if (port.IsOpen) throw new ModulBasedException(); } catch(ModulBasedException e) { vCatchedExeptionMessage(e.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()); return false; } #endregion // if all OK open port try { switch (modul) { case eWichModule.HB627: if (bOpenHB627(this.Comport, timeout)) { isOpen = true; return true; } else return false; case eWichModule.HB628: if (bOpenHB628(this.Comport, timeout)) { isOpen = true; return true; } else return false; default: return false; } } catch (Exception e) { vCatchedExeptionMessage(e.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()); return false; } } public bool bSetCloseModule(eWichModule modul) { try { switch (modul) { case eWichModule.HB627: if (bCloseHB627()) { isOpen = false; return true; } else return false; case eWichModule.HB628: if (bCloseHB628()) { isOpen = false; return true; } else return false; default: return false; } } catch (Exception e) { vCatchedExeptionMessage(e.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()); return false; } } public int iGetMilliVoltageOfOneChannel(eWichModule modul, System.Byte channel, ref string Error) { string error = eErrorKey.ERR_DEFAULT.ToString(); try { int value = 0; switch (modul) { case eWichModule.HB627: if (bReadHB627Channel(channel, out value, out error) == true) { Error = sEvaluateErrorKey(error); return value; } else { Error = error; return -1; } case eWichModule.HB628: if (bReadHB628Channel(channel, out value, out error) == true) { Error = sEvaluateErrorKey(error); return value; } else { Error = error; return -1; } default: Error = error; return -1; } } catch (Exception e) { vCatchedExeptionMessage(e.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()); Error = error; return -1; } } public short[] iGetMilliVoltageOfAllChannels(eWichModule modul, ref string Error) { short[] values = new short[8] { -1, -1, -1, -1, -1, -1, -1, -1 }; string error = eErrorKey.ERR_DEFAULT.ToString(); try { switch (modul) { case eWichModule.HB627: // read all channels HB627 if (bReadHB627AllChannel(values, out error)) { Error = sEvaluateErrorKey(error); return values; } else { Error = error; return new short[] { -1, -1, -1, -1, -1, -1, -1, -1 }; } case eWichModule.HB628: // read all channels HB628 if (bReadHB628AllChannel(values, out error)) { Error = sEvaluateErrorKey(error); return values; } else { Error = error; return new short[] { -1, -1, -1, -1, -1, -1, -1, -1 }; } default: return new short[] { -1, -1, -1, -1, -1, -1, -1, -1 }; } } catch (Exception e) { vCatchedExeptionMessage(e.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()); return new short[] { -1, -1, -1, -1, -1, -1, -1, -1 }; } } void CheckIfOpen() { if (!isOpen) throw new InvalidOperationException("Specified port is not open."); } /**Methods only for HB628************************************************************/ public bool bSetModuleAllDigitalOutputs(System.Byte output, ref string Error) { string error = eErrorKey.ERR_DEFAULT.ToString(); try { if (bSetHB628AllDigitalOutputs(output, out error)) { Error = sEvaluateErrorKey(error); return true; } else { Error = sEvaluateErrorKey(error); return false; } } catch (Exception e) { vCatchedExeptionMessage(e.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()); return false; } } public bool bSetModuleTimeOut(eTimeoutIO timeout, ref string Error) { string error = eErrorKey.ERR_DEFAULT.ToString(); try { if (bSetHB628TimeOut((byte)timeout, out error)) { return true; } else { return false; } } catch (Exception e) { vCatchedExeptionMessage(e.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name.ToString()); return false; } } } /* Exeptions for the class *******************************************************************************/ public class ModulBasedException : ApplicationException { public ModulBasedException() { } public ModulBasedException(string message) : base(message) { } public ModulBasedException(string message, Exception inner) : base(message, inner) { } } }