You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
448 lines
11 KiB
448 lines
11 KiB
using HslCommunication;
|
|
using HslCommunication.ModBus;
|
|
using Monitor.LogService;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
|
|
|
|
namespace Module.HMeter.ModBusTcp
|
|
{
|
|
public class HumiMeterModbusTool
|
|
{
|
|
#region 定义锁
|
|
private object lockObj = new object(); //线程同步锁
|
|
#endregion 定义锁
|
|
|
|
#region 字段属性
|
|
/// <summary>
|
|
/// 服务端连接IP
|
|
/// </summary>
|
|
private string _ipaddr = "172.0.30.100";
|
|
/// <summary>
|
|
/// 服务端连接IP
|
|
/// </summary>
|
|
public string F_IpAddr
|
|
{
|
|
get
|
|
{
|
|
return _ipaddr;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_ipaddr = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 服务端连接端口
|
|
/// </summary>
|
|
private int _port = 502;
|
|
/// <summary>
|
|
/// 服务端连接端口
|
|
/// </summary>
|
|
public int F_Port
|
|
{
|
|
get
|
|
{
|
|
return _port;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_port = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从站号
|
|
/// </summary>
|
|
private byte _site = 0x01;
|
|
/// <summary>
|
|
/// 从站号
|
|
/// </summary>
|
|
public byte F_Site
|
|
{
|
|
get
|
|
{
|
|
return _site;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_site = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通讯是否已连接
|
|
/// </summary>
|
|
private bool _net_connected = false;
|
|
/// <summary>
|
|
/// 通讯是否已连接
|
|
/// </summary>
|
|
public bool F_NetConnected
|
|
{
|
|
get
|
|
{
|
|
return _net_connected;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_net_connected = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ModbusTcp客户端
|
|
/// </summary>
|
|
private ModbusTcpNet _client;
|
|
/// <summary>
|
|
/// ModbusTcp客户端
|
|
/// </summary>
|
|
public ModbusTcpNet F_ElecClient
|
|
{
|
|
get
|
|
{
|
|
return _client;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_client = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设备编号
|
|
/// </summary>
|
|
private string _eqm_code = "001";
|
|
/// <summary>
|
|
/// 设备编号
|
|
/// </summary>
|
|
public string F_EqmCode
|
|
{
|
|
get
|
|
{
|
|
return _eqm_code;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_eqm_code = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设备名称
|
|
/// </summary>
|
|
private string _eqm_name = "温湿度仪表";
|
|
/// <summary>
|
|
/// 设备名称
|
|
/// </summary>
|
|
public string F_EqmName
|
|
{
|
|
get
|
|
{
|
|
return _eqm_name;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_eqm_name = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 决定去读取参数
|
|
/// </summary>
|
|
private bool _is_read = true;
|
|
/// <summary>
|
|
/// 决定去读取参数
|
|
/// </summary>
|
|
public bool F_IsRead
|
|
{
|
|
get
|
|
{
|
|
return _is_read;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_is_read = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 故障码
|
|
/// </summary>
|
|
private int _fault_code = 0;
|
|
/// <summary>
|
|
/// 故障码
|
|
/// </summary>
|
|
public int F_FaultCode
|
|
{
|
|
get
|
|
{
|
|
return _fault_code;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_fault_code = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 故障报警列表
|
|
/// </summary>
|
|
private List<int> _fault_alaram_no_list = new List<int>();
|
|
/// <summary>
|
|
/// 故障报警列表
|
|
/// </summary>
|
|
public List<int> F_FaultAlarmNoList
|
|
{
|
|
get
|
|
{
|
|
return _fault_alaram_no_list;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_fault_alaram_no_list = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 温湿度常用参数结果
|
|
/// </summary>
|
|
private MHumiParamResult _humi_param_result = new MHumiParamResult();
|
|
/// <summary>
|
|
/// 温湿度常用参数结果
|
|
/// </summary>
|
|
public MHumiParamResult F_HumiParamResult
|
|
{
|
|
get
|
|
{
|
|
return _humi_param_result;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_humi_param_result = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion 字段属性
|
|
|
|
#region 类结构体
|
|
|
|
public HumiMeterModbusTool()
|
|
{
|
|
if (_client == null)
|
|
{
|
|
_client = new ModbusTcpNet(_ipaddr, _port);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 温湿度仪表类结构体
|
|
/// </summary>
|
|
/// <param name="ipaddr">服务端连接IP</param>
|
|
/// <param name="port">服务端连接端口</param>
|
|
public HumiMeterModbusTool(string ipAddr, int port)
|
|
{
|
|
_ipaddr = ipAddr;
|
|
_port = port;
|
|
if (_client == null)
|
|
{
|
|
_client = new ModbusTcpNet(_ipaddr, _port);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 温湿度仪表类结构体
|
|
/// </summary>
|
|
/// <param name="ipaddr">服务端连接IP</param>
|
|
/// <param name="port">服务端连接端口</param>
|
|
/// <param name="site">从站号</param>
|
|
public HumiMeterModbusTool(string ipAddr, int port, byte site)
|
|
{
|
|
_ipaddr = ipAddr;
|
|
_port = port;
|
|
_site = site;
|
|
if (_client == null)
|
|
{
|
|
_client = new ModbusTcpNet(_ipaddr, _port);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 温湿度仪表类结构体
|
|
/// </summary>
|
|
/// <param name="ipaddr">服务端连接IP</param>
|
|
/// <param name="port">服务端连接端口</param>
|
|
/// <param name="site">从站号</param>
|
|
/// <param name="devno">设备编号</param>
|
|
public HumiMeterModbusTool(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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 温湿度仪表类结构体
|
|
/// </summary>
|
|
/// <param name="ipaddr">服务端连接IP</param>
|
|
/// <param name="port">服务端连接端口</param>
|
|
/// <param name="site">从站号</param>
|
|
/// <param name="devno">设备编号</param>
|
|
/// <param name="devname">设备名称</param>
|
|
public HumiMeterModbusTool(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 设备连接
|
|
|
|
/// <summary>
|
|
/// ModbusTcp连接
|
|
/// </summary>
|
|
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");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ModbusTcp连接断开
|
|
/// </summary>
|
|
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 参数读写
|
|
|
|
/// <summary>
|
|
/// 开始读参数值线程
|
|
/// </summary>
|
|
private void StartReadParamValueThread()
|
|
{
|
|
Thread readParamValueThread = new Thread(ReadParamValueFunc);
|
|
readParamValueThread.IsBackground = true;
|
|
readParamValueThread.Start();
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 读取参数值方法
|
|
/// </summary>
|
|
private void ReadParamValueFunc()
|
|
{
|
|
while (_is_read)
|
|
{
|
|
if (_net_connected)
|
|
{
|
|
OperateResult<byte[]> readResult = _client.Read("x=3;0",8);
|
|
if (readResult.IsSuccess)
|
|
{
|
|
byte[] byteResult = readResult.Content;
|
|
UInt16 humiVal = _client.ByteTransform.TransUInt16(byteResult, 0);
|
|
UInt16 tempVal= _client.ByteTransform.TransUInt16(byteResult, 2);
|
|
MHumiParamResult paramRlt = new MHumiParamResult()
|
|
{
|
|
F_TempValue = (float)tempVal/10,
|
|
F_HumiValue = (float)humiVal/10
|
|
};
|
|
//new HumiMeterValueToRedis().StoredHumiParamValue(_eqm_code, paramRlt);
|
|
|
|
_humi_param_result = paramRlt;
|
|
}
|
|
}
|
|
Thread.Sleep(2000);
|
|
}
|
|
}
|
|
|
|
#endregion 参数读写
|
|
}
|
|
}
|