using HslCommunication; using HslCommunication.ModBus; using Monitor.LogService; using Monitor.Models; using System; using System.Collections.Generic; using System.Threading; namespace Module.EMeter.ModbusTcp { /// /// 电表527中使用ModbusTcp协议读取信息 /// public class EMeterModbus527Tool { #region 定义锁 private object lockObj = new object(); //线程同步锁 #endregion 定义锁 #region 字段属性 /// /// 服务端连接IP /// private string _ipaddr = "172.0.30.100"; /// /// 服务端连接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 = 0x01; /// /// 从站号 /// 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_ElecClient { get { return _client; } set { lock (lockObj) { _client = value; } } } /// /// 设备编号 /// private string _eqm_code = "001"; /// /// 设备编号 /// public string F_EqmCode { get { return _eqm_code; } set { lock (lockObj) { _eqm_code = value; } } } /// /// 设备名称 /// private string _eqm_name = "水冷电表"; /// /// 设备名称 /// 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; } } } /// /// 总有功功率[0031H] /// private UInt16 _total_active_power = 0; /// /// 总有功功率[0031H] /// public UInt16 F_TotalActivePower { get { return _total_active_power; } set { lock (lockObj) { _total_active_power = value; } } } /// /// 吸收有功电能二次侧 /// private float _show_energy; /// /// 吸收有功电能二次侧 003FH~0040H(吸收有功电能) /// public float F_ShowEnergy { get { return _show_energy; } set { lock (lockObj) { _show_energy = value; } } } /// /// 总有功电能二次侧[0052H~0053H] /// private float _total_active_energy; /// /// 总有功电能二次侧[0052H~0053H] /// public float F_TotalActiveEnergy { get { return _total_active_energy; } set { lock (lockObj) { _total_active_energy = value; } } } /// /// 电表常用参数结果 /// private MEMeterParamResult _emeter_param_result = new MEMeterParamResult(); /// /// 电表常用参数结果 /// public MEMeterParamResult F_EMeterParamResult { get { return _emeter_param_result; } set { lock (lockObj) { _emeter_param_result = value; } } } #endregion 字段属性 #region 类结构体 public EMeterModbus527Tool() { if (!HslCommunication.Authorization.SetAuthorizationCode("975a841d-7346-489a-80eb-3b68c8b17df1")) { } if (_client == null) { _client = new ModbusTcpNet(_ipaddr, _port); } } /// /// 电表类结构体 /// /// 服务端连接IP /// 服务端连接端口 public EMeterModbus527Tool(string ipAddr, int port) { if (!HslCommunication.Authorization.SetAuthorizationCode("975a841d-7346-489a-80eb-3b68c8b17df1")) { } _ipaddr = ipAddr; _port = port; if (_client == null) { _client = new ModbusTcpNet(_ipaddr, _port); } } /// /// 电表类结构体 /// /// 服务端连接IP /// 服务端连接端口 /// 从站号 public EMeterModbus527Tool(string ipAddr, int port, byte site) { if (!HslCommunication.Authorization.SetAuthorizationCode("975a841d-7346-489a-80eb-3b68c8b17df1")) { } _ipaddr = ipAddr; _port = port; _site = site; if (_client == null) { _client = new ModbusTcpNet(_ipaddr, _port); } } /// /// 电表类结构体 /// /// 服务端连接IP /// 服务端连接端口 /// 从站号 /// 设备编号 public EMeterModbus527Tool(string ipAddr, int port, byte site, string devNO) { if (!HslCommunication.Authorization.SetAuthorizationCode("975a841d-7346-489a-80eb-3b68c8b17df1")) { } _ipaddr = ipAddr; _port = port; _site=site; _eqm_code = devNO; if (_client == null) { _client = new ModbusTcpNet(_ipaddr, _port, _site); } } /// /// 电表类结构体 /// /// 服务端连接IP /// 服务端连接端口 /// 从站号 /// 设备编号 /// 设备名称 public EMeterModbus527Tool(string ipaddr, int port, byte site, string devno, string devname) { if (!HslCommunication.Authorization.SetAuthorizationCode("975a841d-7346-489a-80eb-3b68c8b17df1")) { } _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; StartReadParamValueThread(); } 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 参数读写 /// /// 开始读参数值线程 /// private void StartReadParamValueThread() { Thread readParamValueThread = new Thread(ReadParamValueFunc); readParamValueThread.IsBackground = true; readParamValueThread.Start(); } /// /// 读取参数值方法 /// private void ReadParamValueFunc() { while (_is_read) { // var operateResult = _client.Read("x=3;0", 65); // if (_net_connected && operateResult.IsSuccess) // { // var PT = _client.ByteTransform.TransInt16(operateResult.Content, 6); // var CT = _client.ByteTransform.TransInt16(operateResult.Content, 8); // //_total_active_power = _client.ByteTransform.TransUInt16(_client.Read("x=3;45", 150).Content, 4); // //_total_active_energy = _client.ByteTransform.TransSingle(_client.Read("x=3;45", 150).Content, 56) / 1000f; // var transInt16 = (new decimal(_client.ByteTransform.TransUInt16(operateResult.Content, 126))*65536+_client.ByteTransform.TransUInt16(operateResult.Content, 128)/ new decimal(1000f))*new decimal(PT) *new decimal(CT); // _show_energy = (float)transInt16; // MEMeterParamResult paramRlt = new MEMeterParamResult() // { // F_EqmCode = _eqm_code, // F_ShowEnergy=_show_energy, // F_TotalActivePower = _total_active_power, // F_TotalActiveEnergy = Convert.ToUInt32(_total_active_energy) // }; // _emeter_param_result = paramRlt; // new ElecMeterValToRedis().StoredMeterParamResult(_eqm_code, paramRlt); // } var operateResult1 = _client.Read("x=3;0", 10); var operateResult2 = _client.Read("x=3;63", 10); if (_net_connected && operateResult1.IsSuccess && operateResult2.IsSuccess) { var PT = _client.ByteTransform.TransInt16(operateResult1.Content, 6); var CT = _client.ByteTransform.TransInt16(operateResult1.Content, 8); //_total_active_power = _client.ByteTransform.TransUInt16(_client.Read("x=3;45", 150).Content, 4); //_total_active_energy = _client.ByteTransform.TransSingle(_client.Read("x=3;45", 150).Content, 56) / 1000f; var transInt16 = (new decimal(_client.ByteTransform.TransUInt16(operateResult2.Content, 0))*65536+_client.ByteTransform.TransUInt16(operateResult2.Content, 2)/ new decimal(1000f))*new decimal(PT) *new decimal(CT); _show_energy = (float)transInt16; MEMeterParamResult paramRlt = new MEMeterParamResult() { F_EqmCode = _eqm_code, F_ShowEnergy=_show_energy, F_TotalActivePower = _total_active_power, F_TotalActiveEnergy = Convert.ToUInt32(_total_active_energy) }; _emeter_param_result = paramRlt; new ElecMeterValToRedis().StoredMeterParamResult(_eqm_code, paramRlt); } Thread.Sleep(2000); } } #endregion 参数读写 } }