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.
606 lines
20 KiB
606 lines
20 KiB
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
|
|
{
|
|
/// <summary>
|
|
/// UPS中使用ModbusTcp协议读取信息
|
|
/// </summary>
|
|
public class UpsModbusTool
|
|
{
|
|
#region 定义锁
|
|
private object lockObj = new object(); //线程同步锁
|
|
#endregion 定义锁
|
|
|
|
#region 字段属性
|
|
/// <summary>
|
|
/// 服务端连接IP
|
|
/// </summary>
|
|
private string _ipaddr = "172.0.50.102";
|
|
/// <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 = 0x06;
|
|
/// <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 = "U001";
|
|
/// <summary>
|
|
/// 设备编号
|
|
/// </summary>
|
|
public string F_EqmCode
|
|
{
|
|
get
|
|
{
|
|
return _eqm_code;
|
|
}
|
|
set
|
|
{
|
|
lock (lockObj)
|
|
{
|
|
_eqm_code = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设备名称
|
|
/// </summary>
|
|
private string _eqm_name = "UPS";
|
|
/// <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>
|
|
/// Ups状态值
|
|
/// </summary>
|
|
private MUpsParamsVal _ups_param_value=new MUpsParamsVal();
|
|
/// <summary>
|
|
/// Ups状态值
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 类结构体
|
|
/// </summary>
|
|
/// <param name="ipaddr">服务端连接IP</param>
|
|
/// <param name="port">服务端连接端口</param>
|
|
public UpsModbusTool(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 UpsModbusTool(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 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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 类结构体
|
|
/// </summary>
|
|
/// <param name="ipaddr">服务端连接IP</param>
|
|
/// <param name="port">服务端连接端口</param>
|
|
/// <param name="site">从站号</param>
|
|
/// <param name="devno">设备编号</param>
|
|
/// <param name="devname">设备名称</param>
|
|
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 设备连接
|
|
|
|
/// <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;
|
|
StartUpsStateThread();
|
|
}
|
|
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>
|
|
/// 开始采集Ups状态值线程
|
|
/// </summary>
|
|
public void StartUpsStateThread()
|
|
{
|
|
Thread readParamValueThread = new Thread(ReadParamValueFunc);
|
|
readParamValueThread.IsBackground = true;
|
|
readParamValueThread.Start();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读取Ups状态方法
|
|
/// </summary>
|
|
private void ReadParamValueFunc()
|
|
{
|
|
while (_is_read)
|
|
{
|
|
if (_net_connected)
|
|
{
|
|
//设备信息
|
|
OperateResult<byte[]> 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<byte[]> 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<byte[]> 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<byte[]> 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<byte[]> 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<byte[]> 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<string>();
|
|
}
|
|
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<byte[]> 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<string>();
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将Int类型转换字符,不足两位前面自动补"0"
|
|
/// </summary>
|
|
/// <param name="numVal">接收数据</param>
|
|
/// <returns></returns>
|
|
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 数据采集
|
|
|
|
}
|
|
}
|