using HslCommunication; using HslCommunication.ModBus; using Monitor.LogService; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Module.UpsEqm.ModBusTcp { /// /// UPS中使用ModbusTcp协议读取信息 /// public class UpsModbusTool { #region 定义锁 private object lockObj = new object(); //线程同步锁 #endregion 定义锁 #region 字段属性 /// /// 服务端连接IP /// private string _ipaddr = "172.0.50.102"; /// /// 服务端连接IP /// public string F_IpAddr { get { return _ipaddr; } set { lock (lockObj) { _ipaddr = value; } } } /// /// 服务端连接端口 /// private int _port = 502; /// /// 服务端连接端口 /// public int F_Port { get { return _port; } set { lock (lockObj) { _port = value; } } } /// /// 从站号 /// private byte _site = 0x06; /// /// 从站号 /// public byte F_Site { get { return _site; } set { lock (lockObj) { _site = value; } } } /// /// 通讯是否已连接 /// private bool _net_connected = false; /// /// 通讯是否已连接 /// public bool F_NetConnected { get { return _net_connected; } set { lock (lockObj) { _net_connected = value; } } } /// /// ModbusTcp客户端 /// private ModbusTcpNet _client; /// /// ModbusTcp客户端 /// public ModbusTcpNet F_UpsClient { get { return _client; } set { lock (lockObj) { _client = value; } } } /// /// 设备编号 /// private string _eqm_code = "U001"; /// /// 设备编号 /// public string F_EqmCode { get { return _eqm_code; } set { lock (lockObj) { _eqm_code = value; } } } /// /// 设备名称 /// private string _eqm_name = "UPS"; /// /// 设备名称 /// public string F_EqmName { get { return _eqm_name; } set { lock (lockObj) { _eqm_name = value; } } } /// /// 决定去读取参数 /// private bool _is_read = true; /// /// 决定去读取参数 /// public bool F_IsRead { get { return _is_read; } set { lock (lockObj) { _is_read = value; } } } /// /// 故障码 /// private int _fault_code = 0; /// /// 故障码 /// public int F_FaultCode { get { return _fault_code; } set { lock (lockObj) { _fault_code = value; } } } /// /// 故障报警列表 /// private List _fault_alaram_no_list = new List(); /// /// 故障报警列表 /// public List F_FaultAlarmNoList { get { return _fault_alaram_no_list; } set { lock (lockObj) { _fault_alaram_no_list = value; } } } /// /// Ups状态值 /// private MUpsParamsVal _ups_param_value=new MUpsParamsVal(); /// /// Ups状态值 /// public MUpsParamsVal F_UpsParamsVal { get { return _ups_param_value; } set { lock (lockObj) { _ups_param_value = value; } } } #endregion 字段属性 #region 类结构体 public UpsModbusTool() { if (!HslCommunication.Authorization.SetAuthorizationCode("975a841d-7346-489a-80eb-3b68c8b17df1")) { } if (_client == null) { _client = new ModbusTcpNet(_ipaddr, _port); } } /// /// 类结构体 /// /// 服务端连接IP /// 服务端连接端口 public UpsModbusTool(string ipAddr, int port) { _ipaddr = ipAddr; _port = port; if (_client == null) { _client = new ModbusTcpNet(_ipaddr, _port); } } /// /// 类结构体 /// /// 服务端连接IP /// 服务端连接端口 /// 从站号 public UpsModbusTool(string ipAddr, int port, byte site) { _ipaddr = ipAddr; _port = port; _site = site; if (_client == null) { _client = new ModbusTcpNet(_ipaddr, _port); } } /// /// 类结构体 /// /// 服务端连接IP /// 服务端连接端口 /// 从站号 /// 设备编号 public UpsModbusTool(string ipAddr, int port, byte site, string devNO) { _ipaddr = ipAddr; _port = port; _site = site; _eqm_code = devNO; if (_client == null) { _client = new ModbusTcpNet(_ipaddr, _port); } } /// /// 类结构体 /// /// 服务端连接IP /// 服务端连接端口 /// 从站号 /// 设备编号 /// 设备名称 public UpsModbusTool(string ipaddr, int port, byte site, string devno, string devname) { _ipaddr = ipaddr; _port = port; _site = site; _eqm_code = devno; _eqm_name = devname; if (_client == null) { _client = new ModbusTcpNet(_ipaddr, _port); } } #endregion 类结构体 #region 设备连接 /// /// ModbusTcp连接 /// public void Connect() { try { if (_client == null) { _client = new ModbusTcpNet(_ipaddr, _port, _site); } if (_client != null) { _client = new ModbusTcpNet(_ipaddr, _port, _site); OperateResult OptRet = _client.ConnectServer(); if (OptRet.IsSuccess) { _net_connected = true; StartUpsStateThread(); } else { _net_connected = false; } } } catch (Exception ex) { Log.LogInstance.WriteLog("连接失败" + ex.ToString(), LogType.Run, "电表Log"); } } /// /// ModbusTcp连接断开 /// public void DisConnect() { try { OperateResult OptRet = _client.ConnectClose(); if (OptRet.IsSuccess) { _net_connected = false; } } catch (Exception ex) { Log.LogInstance.WriteLog("断开连接失败" + ex.ToString(), LogType.Run, "电表Log"); } } #endregion 设备连接 #region 数据采集 /// /// 开始采集Ups状态值线程 /// public void StartUpsStateThread() { Thread readParamValueThread = new Thread(ReadParamValueFunc); readParamValueThread.IsBackground = true; readParamValueThread.Start(); } /// /// 读取Ups状态方法 /// private void ReadParamValueFunc() { while (_is_read) { if (_net_connected) { //设备信息 OperateResult readResult = _client.Read("x=3;00", 18); _ups_param_value = new MUpsParamsVal(); if (readResult.IsSuccess) { byte[] results = readResult.Content; byte[] byte_str = new byte[30]; Array.Copy(results, byte_str, 30); _ups_param_value.F_UpsName = BitsConvertUtils.BytesTostr(byte_str); _ups_param_value.F_UpsID = _client.ByteTransform.TransUInt16(results, 30); _ups_param_value.F_ProtocolNumber = _client.ByteTransform.TransUInt16(results, 32); _ups_param_value.F_UpsVersion = _client.ByteTransform.TransUInt16(results, 34); CmnUpsBaseInfo.upsPageShowInfo.upsType = _ups_param_value.F_UpsName; CmnUpsBaseInfo.upsPageShowInfo.upsVersion = _ups_param_value.F_UpsVersion; } //状态信息 OperateResult stateResult = _client.Read("x=3;48", 2); if (stateResult.IsSuccess) { byte[] state_results = stateResult.Content; _ups_param_value.F_UpsStateBit0 = BitsConvertUtils.ByteToBitsValue(state_results, 0, 0, 1); _ups_param_value.F_UpsStateBit1 = BitsConvertUtils.ByteToBitsValue(state_results, 0, 1, 1); _ups_param_value.F_UpsStateBit2 = BitsConvertUtils.ByteToBitsValue(state_results, 0, 2, 1); _ups_param_value.F_UpsStateBit3 = BitsConvertUtils.ByteToBitsValue(state_results, 0, 3, 1); _ups_param_value.F_UpsStateBit4 = BitsConvertUtils.ByteToBitsValue(state_results, 0, 4, 1); _ups_param_value.F_UpsStateBit5 = BitsConvertUtils.ByteToBitsValue(state_results, 0, 5, 1); _ups_param_value.F_UpsStateBit6 = BitsConvertUtils.ByteToBitsValue(state_results, 0, 6, 1); _ups_param_value.F_UpsStateBit7 = BitsConvertUtils.ByteToBitsValue(state_results, 0, 7, 1); _ups_param_value.F_BatteryState = state_results[1]; _ups_param_value.F_UpsSystemModal = state_results[2]; } //前端页面信息展示 OperateResult upsStateInfoResult = _client.Read("x=3;96", 60); if (upsStateInfoResult.IsSuccess) { byte[] config_results = upsStateInfoResult.Content; //96 CmnUpsBaseInfo.upsPageShowInfo.inputVolt = _client.ByteTransform.TransUInt16(config_results, 0); //105 CmnUpsBaseInfo.upsPageShowInfo.outputVolt = _client.ByteTransform.TransUInt16(config_results, 18); //122 CmnUpsBaseInfo.upsPageShowInfo.outPutLoadPct = _client.ByteTransform.TransUInt16(config_results, 52); //103 CmnUpsBaseInfo.upsPageShowInfo.inputFreq = _client.ByteTransform.TransUInt16(config_results, 14); //124 CmnUpsBaseInfo.upsPageShowInfo.batteryVolt = _client.ByteTransform.TransUInt16(config_results, 56); //125 CmnUpsBaseInfo.upsPageShowInfo.temp = _client.ByteTransform.TransUInt16(config_results, 58); //111 CmnUpsBaseInfo.upsPageShowInfo.tatedCurrent = _client.ByteTransform.TransUInt16(config_results, 30); //123 CmnUpsBaseInfo.upsPageShowInfo.batteryTotalVolt = _client.ByteTransform.TransUInt16(config_results, 54); //134 CmnUpsBaseInfo.upsPageShowInfo.frequency = _client.ByteTransform.TransUInt16(config_results, 76); } //前端页面信息展示 OperateResult upsInfoResult = _client.Read("x=3;30", 30); if (upsInfoResult.IsSuccess) { byte[] state_results = upsInfoResult.Content; //BitsConvertUtils CmnUpsBaseInfo.upsPageShowInfo.mainsPowerFail = BitsConvertUtils.GetBitValue(state_results[41], 1); CmnUpsBaseInfo.upsPageShowInfo.lowBatteryVolt = BitsConvertUtils.GetBitValue(state_results[41], 4); CmnUpsBaseInfo.upsPageShowInfo.bypassVoltageStableModel = BitsConvertUtils.GetBitValue(state_results[1], 5); CmnUpsBaseInfo.upsPageShowInfo.upsFault = BitsConvertUtils.GetBitValue(state_results[35], 4); CmnUpsBaseInfo.upsPageShowInfo.systemTesting = BitsConvertUtils.GetBitValue(state_results[1], 9); CmnUpsBaseInfo.upsPageShowInfo.remoteSystemShutdown = BitsConvertUtils.GetBitValue(state_results[1], 1); CmnUpsBaseInfo.upsPageShowInfo.alarmSounOn = BitsConvertUtils.GetBitValue(state_results[1], 0); } //配置数据 OperateResult configResult = _client.Read("x=3;160", 16); if (configResult.IsSuccess) { byte[] config_results = configResult.Content; _ups_param_value.F_MainVoltage = _client.ByteTransform.TransUInt16(config_results, 0); _ups_param_value.F_MainFreQuency = _client.ByteTransform.TransUInt16(config_results, 2); _ups_param_value.F_CellVoltage = _client.ByteTransform.TransUInt16(config_results, 4); _ups_param_value.F_MaxMainVoltage = _client.ByteTransform.TransUInt16(config_results, 6); _ups_param_value.F_MinMainVoltage = _client.ByteTransform.TransUInt16(config_results, 8); _ups_param_value.F_MaxMainFreQuency = _client.ByteTransform.TransUInt16(config_results, 10); _ups_param_value.F_MinMainFreQuency = _client.ByteTransform.TransUInt16(config_results, 12); _ups_param_value.F_MaxATemperature = _client.ByteTransform.TransUInt16(config_results, 14); _ups_param_value.F_MinATemperature = _client.ByteTransform.TransUInt16(config_results, 16); _ups_param_value.F_BatCellNumber = _client.ByteTransform.TransUInt16(config_results, 18); _ups_param_value.F_BatCellSeries = _client.ByteTransform.TransUInt16(config_results, 20); CmnUpsBaseInfo.upsPageShowInfo.ratedVolt = _ups_param_value.F_MainVoltage; } //故障信息 OperateResult faultResult = _client.Read("x=3;64", 9); if (faultResult.IsSuccess) { if (_ups_param_value.F_FaultInfo != null) { _ups_param_value.F_FaultInfo.Clear(); } else { _ups_param_value.F_FaultInfo = new List(); } byte[] fault_results = faultResult.Content; for (int i = 0; i < fault_results.Length; i++) { for (int j = 0; j < 8; j++) { int numVal = i * 8 + j; byte byteVal = BitsConvertUtils.ByteToBitsValue(fault_results, i, j, 1); _ups_param_value.F_FaultInfo.Add("F" + IntToStringTwo(numVal) + "-" + byteVal.ToString()); } } } //告警信息 OperateResult warningResult = _client.Read("x=3;80", 6); if (warningResult.IsSuccess) { if(_ups_param_value.F_WarningInfo!=null) { _ups_param_value.F_WarningInfo.Clear(); } else { _ups_param_value.F_WarningInfo = new List(); } byte[] warning_results = warningResult.Content; for (int i=0;i < warning_results.Length;i++) { for (int j=0;j<8;j++) { int numVal = i * 8 + j; byte byteVal = BitsConvertUtils.ByteToBitsValue(warning_results, i, j, 1); _ups_param_value.F_WarningInfo.Add("A" + IntToStringTwo(numVal) + "-" + byteVal.ToString()); } } } new UpsValueToRedis().StoredUpsParamValue(_eqm_code, _ups_param_value); } Thread.Sleep(500); } } /// /// 将Int类型转换字符,不足两位前面自动补"0" /// /// 接收数据 /// private string IntToStringTwo(int numVal) { string str_result = ""; string str_n = numVal.ToString(); if (str_n.Length == 1) { str_result = "0" + str_n; } else { str_result = str_n; } return str_result; } #endregion 数据采集 } }