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.

626 lines
20 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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.FireCtrl.ModBusTcp
{
/// <summary>
/// 消防设备通讯
/// </summary>
public class FireModbusTool
{
#region 定义锁
private object lockObj = new object(); //线程同步锁
#endregion 定义锁
#region 字段属性
/// <summary>
/// 服务端连接IP
/// </summary>
private string _ipaddr = "172.0.50.100";
/// <summary>
/// 服务端连接IP
/// </summary>
public string F_IpAddr
{
get
{
return _ipaddr;
}
set
{
lock (lockObj)
{
_ipaddr = value;
}
}
}
/// <summary>
/// 服务端连接端口
/// </summary>
private int _port = 404;
/// <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_UpsClient
{
get
{
return _client;
}
set
{
lock (lockObj)
{
_client = value;
}
}
}
/// <summary>
/// 设备编号
/// </summary>
private string _eqm_code = "F001";
/// <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 MFireStatusValue _fire_status_value;
/// <summary>
/// 消防设备状态值
/// </summary>
public MFireStatusValue F_FireStatusValue
{
get
{
return _fire_status_value;
}
set
{
_fire_status_value = value;
}
}
/// <summary>
/// 故障信息值
/// </summary>
private List<MFaultInformation> _fault_informations = new List<MFaultInformation>();
/// <summary>
/// 消防故障信息值
/// </summary>
public List<MFaultInformation> F_FaultInformations
{
get
{
return _fault_informations;
}
set
{
_fault_informations = value;
}
}
/// <summary>
/// 传感器信息
/// </summary>
private List<MSensorInformation> _sensor_informations = new List<MSensorInformation>();
/// <summary>
/// 消防传感器信息值
/// </summary>
public List<MSensorInformation> F_SensorInformations
{
get
{
return _sensor_informations;
}
set
{
_sensor_informations = value;
}
}
/// <summary>
/// 分区信息
/// </summary>
private MPartitionValue _partition;
/// <summary>
/// 消防分区信息值
/// </summary>
public MPartitionValue F_Partition
{
get
{
return _partition;
}
set
{
_partition = value;
}
}
/// <summary>
/// 消防灭火器状态信息值
/// </summary>
private MFireExtinguisher _fire_extinguisher;
/// <summary>
/// 消防灭火器状态信息值
/// </summary>
public MFireExtinguisher F_FireExtinguisher
{
get
{
return _fire_extinguisher;
}
set
{
_fire_extinguisher = value;
}
}
#endregion 字段属性
#region 类结构体
public FireModbusTool()
{
if (!HslCommunication.Authorization.SetAuthorizationCode("975a841d-7346-489a-80eb-3b68c8b17df1"))
{
}
if (_client == null)
{
_client = new ModbusTcpNet(_ipaddr, _port);
}
}
/// <summary>
/// 类结构体
/// </summary>
/// <param name="ipaddr">服务端连接IP</param>
/// <param name="port">服务端连接端口</param>
public FireModbusTool(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 FireModbusTool(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 FireModbusTool(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 FireModbusTool(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;
StartFireStateThread();
}
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>
public void StartFireStateThread()
{
Thread readParamValueThread = new Thread(ReadParamValueFunc);
readParamValueThread.IsBackground = true;
readParamValueThread.Start();
}
/// <summary>
/// 读取Ups状态方法
/// </summary>
private void ReadParamValueFunc()
{
while (_is_read)
{
if (_net_connected)
{
#region 系统状态
OperateResult<byte[]> statusValue = _client.Read("x=1;0", 17);
_fire_status_value = new MFireStatusValue();
if (statusValue.IsSuccess)
{
byte[] results_16 = statusValue.Content;
_fire_status_value.F_Alarm = BitsConvertUtils.ByteToBitValue(results_16, 0, 0);
_fire_status_value.F_Fault = BitsConvertUtils.ByteToBitValue(results_16, 0, 1);
_fire_status_value.F_Silencers = BitsConvertUtils.ByteToBitValue(results_16, 0, 2);
_fire_status_value.F_SelfTest = BitsConvertUtils.ByteToBitValue(results_16, 0, 3);
_fire_status_value.F_AutomaticMode = BitsConvertUtils.ByteToBitValue(results_16, 0, 7);
_fire_status_value.F_ManualMode = BitsConvertUtils.ByteToBitValue(results_16, 1, 0);
_fire_status_value.F_StartManually = BitsConvertUtils.ByteToBitValue(results_16, 1, 1);
_fire_status_value.F_ManualEmergencyStop = BitsConvertUtils.ByteToBitValue(results_16, 1, 2);
_fire_status_value.F_AudibleVisualStatus = BitsConvertUtils.ByteToBitValue(results_16, 1, 7);
_fire_status_value.F_DeflatedState = BitsConvertUtils.ByteToBitValue(results_16, 2, 0);
//new FireValueToRedis().StoredFireStatusValue(_eqm_code, _fire_status_value);
}
#endregion
#region 传感器信息
OperateResult<byte[]> sensorInformation = _client.Read("x=4;0", 96);//6个传感器一个传感器1占16个字
_sensor_informations = new List<MSensorInformation>();
if (sensorInformation.IsSuccess)
{
byte[] results_2048 = sensorInformation.Content;
for (int i = 0; i < results_2048.Length / 32; i++)
{
MSensorInformation _sensor_inform = new MSensorInformation();
_sensor_inform.F_Obligate = _client.ByteTransform.TransUInt16(results_2048, 0 + i * 32);
_sensor_inform.F_ProtectiveArea = _client.ByteTransform.TransUInt16(results_2048, 2 + i * 32);
_sensor_inform.F_DeviceType = _client.ByteTransform.TransUInt16(results_2048, 4 + i * 32);
_sensor_inform.F_DetectorID = _client.ByteTransform.TransUInt16(results_2048, 6 + i * 32);
_sensor_inform.F_AlarmLevel = _client.ByteTransform.TransUInt16(results_2048, 8 + i * 32);
_sensor_inform.F_Temperature = _client.ByteTransform.TransUInt16(results_2048, 10 + i * 32);
_sensor_inform.F_CO = _client.ByteTransform.TransUInt16(results_2048, 12 + i * 32);
_sensor_inform.F_VOC = _client.ByteTransform.TransUInt16(results_2048, 14 + i * 32);
_sensor_inform.F_Fumes = _client.ByteTransform.TransUInt16(results_2048, 16 + i * 32);
_sensor_inform.F_Obligate01 = _client.ByteTransform.TransUInt16(results_2048, 18 + i * 32);
_sensor_inform.F_Obligate02 = _client.ByteTransform.TransUInt16(results_2048, 20 + i * 32);
_sensor_inform.F_Obligate03 = _client.ByteTransform.TransUInt16(results_2048, 22 + i * 32);
_sensor_inform.F_Obligate04 = _client.ByteTransform.TransUInt16(results_2048, 24 + i * 32);
_sensor_inform.F_Obligate05 = _client.ByteTransform.TransUInt16(results_2048, 26 + i * 32);
_sensor_inform.F_Obligate06 = _client.ByteTransform.TransUInt16(results_2048, 28 + i * 32);
_sensor_inform.F_Obligate07 = _client.ByteTransform.TransUInt16(results_2048, 30 + i * 32);
_sensor_informations.Add(_sensor_inform);
}
//new FireValueToRedis().StoredFireSensorInfo(_eqm_code, _sensor_informations);
}
#endregion
#region 分区信息
OperateResult<byte[]> partition = _client.Read("x=4;2048", 16); //有1个分区,
_partition = new MPartitionValue();
if (partition.IsSuccess)
{
byte[] results_16 = partition.Content;
_partition.F_Obligate = _client.ByteTransform.TransUInt16(results_16, 0);
_partition.F_ProtectiveArea = _client.ByteTransform.TransUInt16(results_16, 2);
_partition.F_AlarmLevel = _client.ByteTransform.TransUInt16(results_16, 4);
_partition.F_StartManually = _client.ByteTransform.TransUInt16(results_16, 12);
_partition.F_EmergencyStop = _client.ByteTransform.TransUInt16(results_16, 14);
_partition.F_BootControl = _client.ByteTransform.TransUInt16(results_16, 16);
_partition.F_Delay = _client.ByteTransform.TransUInt16(results_16, 18);
_partition.F_DelayTime = _client.ByteTransform.TransUInt16(results_16, 20);
_partition.F_StartSpraying = _client.ByteTransform.TransUInt16(results_16, 22);
//new FireValueToRedis().StoredFirePartitionValue(_eqm_code, _partition);
}
#endregion
#region 灭火器信息
OperateResult<byte[]> fireExtinguisherStatus = _client.Read("x=4;4096", 16);//读灭火器状态,实际有6个灭火器
_fire_extinguisher = new MFireExtinguisher();
if (fireExtinguisherStatus.IsSuccess)
{
byte[] results_16 = fireExtinguisherStatus.Content;
_fire_extinguisher.F_FireExtinguisherValue01 = _client.ByteTransform.TransUInt16(results_16, 0);
_fire_extinguisher.F_FireExtinguisherValue02 = _client.ByteTransform.TransUInt16(results_16, 2);
_fire_extinguisher.F_FireExtinguisherValue03 = _client.ByteTransform.TransUInt16(results_16, 4);
_fire_extinguisher.F_FireExtinguisherValue04 = _client.ByteTransform.TransUInt16(results_16, 6);
_fire_extinguisher.F_FireExtinguisherValue05 = _client.ByteTransform.TransUInt16(results_16, 8);
_fire_extinguisher.F_FireExtinguisherValue06 = _client.ByteTransform.TransUInt16(results_16, 10);
//new FireValueToRedis().StoredFireExtinguisher(_eqm_code, _fire_extinguisher);
}
#endregion
#region 故障信息
OperateResult<byte[]> faultInformation = _client.Read("x=3;256", 384);
_fault_informations = new List<MFaultInformation>();
if (faultInformation.IsSuccess)
{
byte[] results_384 = faultInformation.Content;
for (int i = 0; i < results_384.Length / 6; i++)
{
MFaultInformation fault_inform = new MFaultInformation();
fault_inform.F_ObligateHigh = results_384[1 + i * 6];
fault_inform.F_ObligateLow = results_384[0 + i * 6];
fault_inform.F_ProtectiveArea = results_384[3 + i * 6];
fault_inform.F_DeviceType = results_384[2 + i * 6];
fault_inform.F_DeviceNumber = results_384[5 + i * 6];
fault_inform.F_FaultCode = results_384[4 + i * 6];
_fault_informations.Add(fault_inform);
}
//new FireValueToRedis().StoredFireFaultInfo(_eqm_code, _fault_informations);
}
#endregion
}
Thread.Sleep(500);
}
}
#endregion 数据采集
}
}