HumitureController.cs 5.3 KB
 
using log4net;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text; 

namespace OnlineStore.Common
{
    /// <summary>
    /// 壁挂王字壳温湿度变送器(485型)
    /// </summary>
    public class HumitureController
    {
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public static bool IsRun = false;
        public static string serialPort = "";

        private static int bautRate = 4800;//波特率
        private static Parity parity = Parity.None;//校验位
        private static int dataBits = 8;//数据位
        private static StopBits stopBits = StopBits.One; //停止位
        private static AcSerialBean sb = null;
         
        private static string LogName = "";
        public static bool Init(string port)
        {
            if (IsRun && port.Equals(serialPort))
            {
                return true;
            }
            else if (IsRun)
            {
                Release();
            }
            LogName = "温湿度传感器[" + port + "]";

            if (sb == null)
            {
                serialPort = port;
                sb = new AcSerialBean(serialPort, bautRate, parity, dataBits, stopBits);
            }
            try
            {
                if (sb.openPort())
                {
                    IsRun = true;
                    return true;
                }
                else
                {
                    LogUtil.error(LOGGER, LogName + "串口打开失败!");
                    IsRun = false;
                    return false;
                }
            }
            catch (Exception ex)
            {
                IsRun = false;
            }
            return true;
        }
        /// <summary>
        /// 释放资源
        /// </summary>
        public static void Release()
        {
            if (sb != null)
            {
                sb.closePort();
            }
            IsRun = false;
        }
        public static  ASTemperateParam LastData = new ASTemperateParam(0, 0);
        public static ASTemperateParam QueryData()
        {
            ASTemperateParam param = new ASTemperateParam(0, 0);
            List<double> data = queryData();
            if (data.Count.Equals(2))
            {
                param = new ASTemperateParam(data[1], data[0]);
            }
            LastData = param;
            return param;
        }
        /// <summary>
        /// 返回温度和湿度
        /// </summary>
        /// <returns></returns>
        private static List<double> queryData()
        {
            // 温度计算:
            //当温度低于 0 ℃ 时温度数据以补码的形式上传。 
            //温度:FF9B H(十六进制)= -101 => 温度 = -10.1℃
            //湿度计算:
            //湿度:292 H(十六进制) = 658 => 湿度 = 65.8 % RH
            if (IsRun.Equals(false))
            {
                return new List<double>();
            }
            byte[] sendData = new byte[8];
            sendData[0] = 0x01;
            sendData[1] = 0x03;
            sendData[2] = 0x00;
            sendData[3] = 0x00;
            sendData[4] = 0x00;
            sendData[5] = 0x02;
            sendData[6] = 0x00;
            sendData[7] = 0x00;
            sendData = buildCheckData(sendData, sendData.Length-2);
            string str = AcSerialBean.byteToHexStr(sendData);
            LogUtil.debug("温湿度控制器发送数据:" + str);
            byte[] reviceData = new byte[9];
            bool isOk = false;
            sb.SendCommand(sendData, ref reviceData, 100, out isOk);
            return getReviceData(reviceData);
        }


        private static byte[] buildCheckData(byte[] sendData, int length)
        {
            ushort pChecksum = 0;
            AcSerialBean.CalculateCRC(sendData, length, out pChecksum);
            string checkStr = Convert.ToString(pChecksum, 16);
            byte[] checkByte = AcSerialBean.StringToByte(checkStr);
            if (checkByte.Length == 1)
            {
                sendData[length] = checkByte[0];
                sendData[length + 1] = 0x00;
            }
            else
            {
                sendData[length + 1] = checkByte[0];
                sendData[length] = checkByte[1];
            }
            return sendData;
        }


        private static List<double> getReviceData(byte[] dataArray)
        {
            List<double> list = new List<double>(); 
            try
            {
                if (dataArray == null)
                {
                    return list;
                }
                if (dataArray.Length >= 9)
                {

                    string temp = String.Format("{0:X2}", dataArray[3])+ String.Format("{0:X2}", dataArray[4]);
                    string hum = String.Format("{0:X2}", dataArray[5]) + String.Format("{0:X2}", dataArray[6]);
                    double tempV = (double)Convert.ToInt32(temp, 16)/10;
                    double humV =(double) Convert.ToInt32(hum, 16)/10;
                    list.Add(tempV);
                    list.Add(humV);
                }
            }
            catch (Exception ex)
            {
                LOGGER.Info(LogName + "转换出错:" + ex.ToString());
            }
            return list;
        }
         
    }
}