using System; using System.Collections.Generic; using System.IO.Ports; using System.Runtime.InteropServices; using System.Text; using System.Diagnostics; using Microsoft.Win32; using Microsoft.VisualBasic; using Microsoft.VisualBasic.CompilerServices; namespace TEMPer.Communication { public class TEMPerInterface { [DllImport("kernel32.dll", SetLastError = true)] internal static extern bool CloseHandle(IntPtr hObject); [DllImport("kernel32.dll", SetLastError = true)] internal static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile); [DllImport("kernel32.dll")] internal static extern bool GetCommProperties(IntPtr hFile, out COMMPROP cp); private static readonly string LogFileName = "LOG-TEMPERINTERFACE.txt"; private string m_CHic; private bool m_Init; private readonly string m_PortName; private SerialPort m_SerialPort; private static readonly double myMinValue = -99.0; /// /// Initializes a new instance of the class. /// /// The COM port. public TEMPerInterface(string COMPort) { this.m_PortName = COMPort; this.Init(); } /// /// Bin2s the dec. /// /// The STR bin. /// private static double Bin2Dec(string strBin) { try { double num = 0.0; if ((strBin == null) || (strBin.Length == 0)) { strBin = "0"; } double num2 = Strings.Len(strBin); double num3 = num2; for (double i = 1.0; i <= num3; i++) { num += Convert.ToInt32(Strings.Left(strBin, 1)) * Math.Pow(2.0, (double)(Strings.Len(strBin) - 1)); strBin = Strings.Right(strBin, Strings.Len(strBin) - 1); } return num; } catch (Exception exception) { log(exception.Message); } return myMinValue; } /// /// Checks the COM port. /// /// The COM port. /// public static bool CheckCOMPort(string COMPort) { try { COMMPROP commprop; IntPtr hFile = CreateFile(COMPort, 0xc0000000, 3, IntPtr.Zero, 3, 0x800, IntPtr.Zero); if (hFile.ToInt32() == -1) { return false; } commprop.dwProvSpec2 = 0; commprop.wPacketLength = 0x40; commprop.dwProvSpec1 = 0xe73cf52e; GetCommProperties(hFile, out commprop); CloseHandle(hFile); log("COMMPROP:" + commprop.dwProvSpec2); return ((commprop.dwProvSpec2 == 0x43485523) || (commprop.dwProvSpec2 == 0x43485512)); } catch (Exception exception) { if (GlobalVars.config_log) { log(exception.Message); } } return false; } /// /// Checks the device. /// public void CheckDevice() { int num = 0; string str = this.TEMPerName(); while ((num <= 20) && (str == "n/a")) { str = this.TEMPerName(); num++; } } /// /// Comms the port exists. /// /// The COM port. /// public static bool CommPortExists(string COMPort) { foreach (string str in SerialPort.GetPortNames()) { if (COMPort == str) { return true; } } return false; } /// /// Ctoes the F. /// /// The celsius. /// public static double CtoF(double celsius) { return ((1.8 * celsius) + 32.0); } /// /// Dec2s the bin. /// /// The b dec. /// public string Dec2Bin(double bDec) { string str = ""; try { if (bDec > 255.0) { return "-1"; } string expression = ""; while (bDec > 0.0) { expression = (bDec % 2.0) + expression; bDec = Conversion.Fix((double)(bDec / 2.0)); } if (Strings.Len(expression) < 9) { while (Strings.Len(expression) < 8) { expression = "0" + expression; } } str = expression; } catch (Exception) { } return str; } /// /// Delays the specified x. /// /// The x. private static void Delay(int x) { for (int i = x; i <= 3; i++) { } } /// /// Finds the devices. /// /// public static string[] FindDevices() { List list = new List(); string[] portNames = SerialPort.GetPortNames(); if (portNames == null) { return null; } for (int i = 0; i < portNames.Length; i++) { if (CheckCOMPort(@"\\.\" + portNames[i] + "\0")) { list.Add(portNames[i]); } } list.Sort(); return list.ToArray(); } /// /// Gets the hum full text. /// /// The hum. /// public static string getHumFullText(object hum) { double num = Convert.ToDouble(hum); if (num == -99.0) { return "-"; } return (num.ToString("###0.0000") + " %"); } /// /// Gets the hum full text HTML. /// /// The hum. /// public static string getHumFullTextHTML(object hum) { double num = Convert.ToDouble(hum); if (num == -99.0) { return "-"; } return (num.ToString("###0.0000") + " %"); } /// /// Gets the hum full text short. /// /// The hum. /// public static string getHumFullTextShort(object hum) { double num = Convert.ToDouble(hum); if (num == -99.0) { return "-"; } return (num.ToString("###0.00") + " %"); } /// /// Gets the hum full text short HTML. /// /// The hum. /// public static string getHumFullTextShortHTML(object hum) { double num = Convert.ToDouble(hum); if (num == -99.0) { return "-"; } return (num.ToString("###0.00") + " %"); } /// /// Gets the temp double. /// /// The temp. /// public static double getTempDouble(object temp) { if (GlobalVars.config_fahrenheit) { return CtoF(Convert.ToDouble(temp)); } return Convert.ToDouble(temp); } /// /// Gets the temp full text. /// /// The temp. /// public static string getTempFullText(object temp) { if (Convert.ToDouble(temp) == -99.0) { return "-"; } if (GlobalVars.config_fahrenheit) { return (CtoF(Convert.ToDouble(temp)).ToString("###0.0000") + " \x00b0F"); } return (Convert.ToDouble(temp).ToString("###0.0000") + " \x00b0C"); } /// /// Gets the temp full text HTML. /// /// The temp. /// public static string getTempFullTextHTML(object temp) { if (Convert.ToDouble(temp) == -99.0) { return "-"; } if (GlobalVars.config_fahrenheit) { return (CtoF(Convert.ToDouble(temp)).ToString("###0.0000") + " °F"); } return (Convert.ToDouble(temp).ToString("###0.0000") + " °C"); } /// /// Gets the temp full text short. /// /// The temp. /// public static string getTempFullTextShort(object temp) { if (Convert.ToDouble(temp) == -99.0) { return "-"; } if (GlobalVars.config_fahrenheit) { return (CtoF(Convert.ToDouble(temp)).ToString("###0.00") + " \x00b0F"); } return (Convert.ToDouble(temp).ToString("###0.00") + " \x00b0C"); } /// /// Gets the temp full text short HTML. /// /// The temp. /// public static string getTempFullTextShortHTML(object temp) { if (Convert.ToDouble(temp) == -99.0) { return "-"; } if (GlobalVars.config_fahrenheit) { return (CtoF(Convert.ToDouble(temp)).ToString("###0.00") + " °F"); } return (Convert.ToDouble(temp).ToString("###0.00") + " °C"); } /// /// His the low SCLK. /// private void HiLowSCLK() { Delay(1); this.Sclk(1); Delay(1); this.Sclk(0); Delay(1); } /// /// Inits this instance. /// private void Init() { if (this.m_PortName != null) { this.m_SerialPort = new SerialPort(this.m_PortName); this.m_CHic = "R"; this.CheckDevice(); if (CommPortExists(this.m_PortName)) { this.m_SerialPort.Open(); this.WriteP1P0(1, "0110000"); this.WriteP1P0(0, "00000000"); if (this.m_SerialPort.IsOpen) { this.m_SerialPort.Close(); } this.m_Init = true; } } } /// /// Logs the specified new log line. /// /// The new log line. public static void log(string newLogLine) { string str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); if (GlobalVars.config_log) { try { System.IO.StreamWriter writer = System.IO.File.AppendText(LogFileName); writer.WriteLine("[" + str + "] " + newLogLine); writer.Close(); } catch (Exception) { } } } /// /// Reads this instance. /// /// public double[] Read() { if ((GlobalVars.TEMPerName == "TEMPerV1") || (GlobalVars.TEMPerName == "TEMPerV2")) { return this.ReadTEMPer(); } if (GlobalVars.TEMPerName == "TEMPerHum") { return this.ReadTEMPerHum(); } return new double[] { -99.0, -99.0 }; } /// /// Reads the ACK. /// public void ReadACK() { byte num = this.SDin(); this.HiLowSCLK(); if (num == 1) { if (this.m_CHic == "T") { this.m_CHic = "R"; } else { this.m_CHic = "T"; } } } /// /// Reads the EEPROM. /// /// The E eadd. /// public double ReadEEPROM(byte EEadd) { if (this.m_SerialPort.IsOpen) { this.m_SerialPort.Close(); } this.m_SerialPort.Open(); this.Start_IIC(); this.writeX(160); Delay(100); this.ReadACK(); this.writeX(EEadd); Delay(100); this.ReadACK(); this.Start_IIC(); this.writeX(0xa1); Delay(100); this.ReadACK(); string strBin = this.SDin().ToString(); this.HiLowSCLK(); strBin = strBin + this.SDin(); this.HiLowSCLK(); strBin = strBin + this.SDin(); this.HiLowSCLK(); strBin = strBin + this.SDin(); this.HiLowSCLK(); strBin = strBin + this.SDin(); this.HiLowSCLK(); strBin = strBin + this.SDin(); this.HiLowSCLK(); strBin = strBin + this.SDin(); this.HiLowSCLK(); strBin = strBin + this.SDin(); this.HiLowSCLK(); this.Stop_IIC(); double num = Bin2Dec(strBin); this.m_SerialPort.Close(); return num; } /// /// Reads the SHT. /// /// The TH. /// private double ReadSHT(string TH) { string str; int num = 0; string strBin = ""; string str3 = ""; if (!this.m_SerialPort.IsOpen) { this.m_SerialPort.Open(); } Delay(1); this.TransStart(); this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); if (TH == "T") { this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); } if (TH == "H") { this.SDout(0); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); } this.SDout(1); this.Sclk(1); this.Sclk(0); while ((this.SDin() == 1) & (num < 0x3e8)) { Delay(1); num++; } if (this.SDin() == 1) { log("BAD"); } this.SDout(1); this.Sclk(1); string str4 = this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); this.SDout(0); this.Sclk(1); Delay(1); this.Sclk(0); this.SDout(1); this.Sclk(1); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.HiLowSCLK(); str4 = str4 + this.SDin().ToString(); this.SDout(1); this.Sclk(1); Delay(1); this.Sclk(0); this.SDout(1); if (TH == "T") { str = Strings.Right(str4, 14); strBin = Strings.Left(str, 6); str3 = Strings.Right(str, 8); } if (TH == "H") { str = Strings.Right(str4, 12); strBin = Strings.Left(str, 4); str3 = Strings.Right(str, 8); } double num2 = Bin2Dec(strBin); double num3 = Bin2Dec(str3); double num4 = (num2 * 256.0) + num3; double num5 = num4; this.Stop_IIC(); this.m_SerialPort.Close(); return num5; } /// /// Reads the TEM per. /// /// public double[] ReadTEMPer() { double[] numArray = new double[] { -99.0, -99.0 }; try { if (this.m_SerialPort == null) { return numArray; } if (!this.m_Init) { this.Init(); } if (!this.m_Init) { return numArray; } if (!this.m_SerialPort.IsOpen) { this.m_SerialPort.Open(); } double myMinValue = TEMPerInterface.myMinValue; this.SDout(1); this.SDin(); this.SDout(0); this.SDin(); this.Start_IIC(); this.SDout(1); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.Sclk(1); byte num2 = this.SDin(); this.HiLowSCLK(); if (num2 == 1) { if (this.m_CHic == "T") { this.m_CHic = "R"; } else { this.m_CHic = "T"; } } if (num2 == 1) { return numArray; } string str = this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); this.SDin(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.HiLowSCLK(); str = str + this.SDin().ToString(); this.Sclk(0); Delay(1); string str2 = Strings.Left(str, 1); string str3 = Strings.Left(str, 12); string strBin = Strings.Left(str3, 4); string str5 = Strings.Right(str3, 8); double num3 = Bin2Dec(strBin); double num4 = Bin2Dec(str5); double num5 = (num3 * 256.0) + num4; string str6 = str2; if (str6 != null) { if (!(str6 == "0")) { if (str6 == "1") { goto Label_0395; } } else { myMinValue = num5 * 0.0625; } } goto Label_03AD; Label_0395: myMinValue = -(4096.0 - num5) * 0.0625; Label_03AD: this.SDout(0); this.HiLowSCLK(); this.Stop_IIC(); if (this.m_SerialPort.IsOpen) { this.m_SerialPort.Close(); } log(myMinValue.ToString("###0.#####")); numArray[0] = myMinValue + GlobalVars.config_calibration_temp; numArray[1] = TEMPerInterface.myMinValue; return numArray; } catch (Exception exception) { if (GlobalVars.config_log) { log(exception.Message); } } this.m_Init = false; numArray[0] = TEMPerInterface.myMinValue; numArray[1] = TEMPerInterface.myMinValue; return numArray; } /// /// Reads the TEM per hum. /// /// private double[] ReadTEMPerHum() { decimal num = new decimal((this.ReadSHT("T") * 0.01) - 40.0); decimal right = -4M; decimal num3 = 0.0405M; object left = -2.8E-06; decimal num4 = 0.01M; decimal num5 = 0.00008M; Delay(4); int num6 = (int)Math.Round(this.ReadSHT("H")); decimal num7 = Conversions.ToDecimal(Operators.AddObject(Operators.AddObject(Operators.MultiplyObject(Operators.MultiplyObject(left, num6), num6), decimal.Multiply(num3, new decimal(num6))), right)); decimal num8 = decimal.Add(decimal.Multiply(decimal.Subtract(num, 25M), decimal.Add(num4, decimal.Multiply(num5, new decimal(num6)))), num7); if (decimal.Compare(num8, 100M) > 0) { num8 = 100M; } if (Convert.ToDouble(num8) < 0.1) { num8 = 0.1M; } double[] numArray = new double[] { Convert.ToDouble(num) + GlobalVars.config_calibration_temp, Convert.ToDouble(num8) + GlobalVars.config_calibration_humidity }; log("TEMP: " + numArray[0].ToString() + ", HUM: " + numArray[1].ToString()); return numArray; } /// /// SCLKs the specified ad01. /// /// The ad01. private void Sclk(byte ad01) { if (this.m_CHic == "T") { if (ad01 == 0) { this.m_SerialPort.DtrEnable = true; } if (ad01 == 1) { this.m_SerialPort.DtrEnable = false; } } if (this.m_CHic == "R") { if (ad01 == 0) { this.m_SerialPort.DtrEnable = false; } if (ad01 == 1) { this.m_SerialPort.DtrEnable = true; } } } /// /// Ss the din. /// /// private byte SDin() { byte num = 0; this.SDout(1); Delay(50); Delay(50); bool ctsHolding = this.m_SerialPort.CtsHolding; if (this.m_CHic == "T") { if (!ctsHolding) { num = 1; } else { num = 0; } } if (this.m_CHic != "R") { return num; } if (!ctsHolding) { return 0; } return 1; } /// /// Ss the dout. /// /// The ad01. private void SDout(byte ad01) { if (this.m_CHic == "T") { if (ad01 == 0) { this.m_SerialPort.RtsEnable = true; } if (ad01 == 1) { this.m_SerialPort.RtsEnable = false; } } if (this.m_CHic == "R") { if (ad01 == 0) { this.m_SerialPort.RtsEnable = false; } if (ad01 == 1) { this.m_SerialPort.RtsEnable = true; } } } /// /// Start_s the IIC. /// private void Start_IIC() { this.SDout(1); Delay(1); this.Sclk(1); Delay(1); this.SDout(0); Delay(1); this.Sclk(0); } /// /// Stop_s the IIC. /// private void Stop_IIC() { this.SDout(0); Delay(1); this.Sclk(1); Delay(1); this.SDout(1); Delay(1); } /// /// TEMs the name of the per. /// /// public string TEMPerName() { byte num = (byte)Math.Round(this.ReadEEPROM(0x58)); byte num2 = (byte)Math.Round(this.ReadEEPROM(0x59)); if ((num == 0x58) & (num2 == 0x59)) { GlobalVars.TEMPerName = "TEMPerV1"; } if ((num == 0x58) & (num2 != 0x59)) { GlobalVars.TEMPerName = "TEMPerV2"; } if ((num == 0x59) & (num2 == 90)) { GlobalVars.TEMPerName = "TEMPerHum"; } log("Found: " + GlobalVars.TEMPerName + " (y88: " + num.ToString() + ", y89: " + num2.ToString() + ", chic: " + this.m_CHic); return GlobalVars.TEMPerName; } /// /// Transes the start. /// private void TransStart() { this.SDout(1); this.Sclk(0); Delay(1); this.Sclk(1); Delay(1); this.SDout(0); Delay(1); this.Sclk(0); Delay(1); this.Sclk(1); Delay(1); this.SDout(1); Delay(1); this.Sclk(0); } /// /// Writes the p1 p0. /// /// The P0123. /// The data S. private void WriteP1P0(byte P0123, string dataS) { this.Start_IIC(); this.SDout(1); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.Sclk(1); Delay(200); byte num = this.SDin(); this.HiLowSCLK(); if (num == 1) { log("err1 ack by p1p0"); } this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); if (P0123 == 0) { this.SDout(0); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); } if (P0123 == 1) { this.SDout(0); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); } if (P0123 == 2) { this.SDout(1); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); } if (P0123 == 3) { this.SDout(1); this.HiLowSCLK(); this.SDout(1); this.HiLowSCLK(); } this.Sclk(1); Delay(200); this.SDin(); this.HiLowSCLK(); if (num == 1) { log("err2 ack by p1p0"); } byte start = 1; do { if (Strings.Mid(dataS, start, 1) == "1") { this.SDout(1); } else { this.SDout(0); } Delay(10); this.HiLowSCLK(); start = (byte)(start + 1); } while (start <= 8); this.Sclk(1); Delay(100); Delay(100); this.SDin(); this.HiLowSCLK(); this.SDout(0); this.HiLowSCLK(); this.Stop_IIC(); } /// /// Writes the X. /// /// The data. public void writeX(byte data) { string str = this.Dec2Bin((double)data); byte num = 0; do { byte num2 = Convert.ToByte(Strings.Right(Strings.Left(str, num + 1), 1)); this.SDout(num2); Delay(20); this.HiLowSCLK(); num = (byte)(num + 1); } while (num <= 7); } /// /// Gets the name of the port. /// /// The name of the port. public string PortName { get { return this.m_PortName; } } /// /// Dews the point. /// /// The x temp. /// The rh_true. /// public decimal DewPoint(decimal xTemp, decimal rh_true) { try { decimal num3 = new decimal((0.66077 + ((7.5 * Convert.ToDouble(xTemp)) / (237.3 + Convert.ToDouble(xTemp)))) + (Math.Log10(Convert.ToDouble(rh_true)) - 2.0)); return new decimal(((Convert.ToDouble(num3) - 0.66077) * 237.3) / (8.16077 - Convert.ToDouble(num3))); } catch { return new decimal(0.0); } } [StructLayout(LayoutKind.Sequential)] internal struct COMMPROP { internal ushort wPacketLength; internal ushort wPacketVersion; internal uint dwServiceMask; internal uint dwReserved1; internal uint dwMaxTxQueue; internal uint dwMaxRxQueue; internal uint dwMaxBaud; internal uint dwProvSubType; internal uint dwProvCapabilities; internal uint dwSettableParams; internal uint dwSettableBaud; internal ushort wSettableData; internal ushort wSettableStopParity; internal uint dwCurrentTxQueue; internal uint dwCurrentRxQueue; internal uint dwProvSpec1; internal uint dwProvSpec2; internal byte wcProvChar; } } }