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.
166 lines
5.9 KiB
166 lines
5.9 KiB
using HybirdFrameworkCore.Autofac.Attribute;
|
|
using HybirdFrameworkDriver.ModbusTcpMaster;
|
|
using HybirdFrameworkDriver.TcpServer;
|
|
using HybirdFrameworkEntity.DbModel;
|
|
using HybirdFrameworkRepository.System;
|
|
using HybirdFrameworkServices.Init.Entity;
|
|
using HybirdFrameworkServices.Plc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace HybirdFrameworkServices
|
|
{
|
|
[Scope("SingleInstance")]
|
|
public class EquipmentInit
|
|
{
|
|
BsNetEqmParamInfoRepository _bsNetEqmParamInfoRepository;
|
|
public EquipmentInit(BsNetEqmParamInfoRepository bsNetEqmParamInfoRepository)
|
|
{
|
|
_bsNetEqmParamInfoRepository = bsNetEqmParamInfoRepository;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 连接所有数据
|
|
/// </summary>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
public void Connect()
|
|
{
|
|
BsNetEqmParamInfo bsNetEqmParamInfo = new BsNetEqmParamInfo()
|
|
{
|
|
EqmTypeNo = 3,
|
|
EqmTypeName = "",
|
|
EqmCode = "",
|
|
EqmSn = 3,
|
|
NetAddr = "127.0.0.1",
|
|
NetPort = "502",
|
|
DestAddr = "1",
|
|
};
|
|
|
|
|
|
// //PLC删除
|
|
// IEnumerable<IGrouping<int, BsNetEqmParamInfo>> queryConnectParams =;
|
|
// _bsNetEqmParamInfoRepository.QueryConnectParams(new List<int>() { 1, 2, 101 });
|
|
// int count = 0;
|
|
// IEnumerable<IGrouping<int, BsNetEqmParamInfo>> connectParams =
|
|
//queryConnectParams as IGrouping<int, BsNetEqmParamInfo>[] ?? queryConnectParams.ToArray();
|
|
// foreach (IGrouping<int, BsNetEqmParamInfo> device in connectParams)
|
|
// {
|
|
// Console.WriteLine(device.Key);
|
|
// count = device.Key switch
|
|
// {
|
|
// //1 => ConnectCharger(device),
|
|
// //101 => ConnectCharger(device),
|
|
// //2 => ConnectWaterCooler(device),
|
|
// 3 => ConnectPlc(device),
|
|
// _ =>
|
|
// // 处理所有其他情况,可能抛出异常、记录日志或返回默认值
|
|
// throw new InvalidOperationException("Unknown device key: " + device.Key),
|
|
// };
|
|
// }
|
|
|
|
ConnectPlc(bsNetEqmParamInfo);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 连接PLC
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
public int ConnectPlc(/*IGrouping<int, BsNetEqmParamInfo> param*/ BsNetEqmParamInfo p)
|
|
{
|
|
/*int connectNum = 0;
|
|
foreach (var p in param)
|
|
{
|
|
connectNum++;*/
|
|
|
|
Thread thread = new Thread(() => ConnectPlcAsync(p));
|
|
thread.Start();
|
|
/*}*/
|
|
|
|
return 0;
|
|
}
|
|
private void ConnectPlcAsync(BsNetEqmParamInfo p)
|
|
{
|
|
bool connected = false;
|
|
|
|
ModbusTcpMaster master = null;
|
|
//PLC连接
|
|
while (!connected)
|
|
{
|
|
|
|
master = new ModbusTcpMaster()
|
|
{
|
|
Ip = "127.0.0.1",
|
|
Port = 102,
|
|
};
|
|
master.ReadAction = BatchReadPlc;//启动线程一直读
|
|
master.Duration = 3000;
|
|
master.Duration = 3000;
|
|
connected = master.Connect();
|
|
|
|
if (connected)
|
|
{
|
|
break;
|
|
}
|
|
|
|
Thread.Sleep(5000);
|
|
}
|
|
|
|
ModbusSession modbusSession = new ModbusSession(master);
|
|
SessionMgr.RegisterModbusSession(master.connectId, modbusSession);
|
|
SessionMgr.SetAttrModbus(modbusSession, "eqm_sn", p.EqmSn);
|
|
PlcInfo waterColdInfo = new PlcInfo(master.connectId, p.EqmSn);
|
|
ChargerStaticInfo.PlcInfos.TryAdd(p.EqmSn, waterColdInfo);
|
|
}
|
|
/**
|
|
* 一直读
|
|
*/
|
|
private static void BatchReadPlc(ModbusTcpMaster master)
|
|
{
|
|
int sn = (int)SessionMgr.GetAttrModbus(master.connectId, "eqm_sn");
|
|
|
|
ChargerStaticInfo.PlcInfos.TryGetValue(sn, out PlcInfo plcInfo);
|
|
if (plcInfo == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
byte[]? bytes01 = master.BatchRead(0, 22);
|
|
if (bytes01 != null)
|
|
{
|
|
ModbusDecoder.DecodeByT<PlcReadAndWrite2>(bytes01, plcInfo.PlcReadAndWritten2);
|
|
}
|
|
byte[]? bytes02 = master.BatchRead(256, 170);
|
|
if (bytes02 != null)
|
|
{
|
|
ModbusDecoder.DecodeByT<PlcReadonly>(bytes02, plcInfo.PlcReadonly);
|
|
}
|
|
byte[]? bytes03 = master.BatchRead(39424, 108);
|
|
if (bytes03 != null)
|
|
{
|
|
ModbusDecoder.DecodeByT<PlcTurnsRatio>(bytes03, plcInfo.PlcTurnsRatio);
|
|
}
|
|
|
|
byte[]? bytes04 = master.BatchRead(40960, 3);//写编程使能。本机modbus地址。波特率
|
|
byte[]? bytes05 = master.BatchRead(2566, 1);//校验位
|
|
byte[]? bytes06 = master.BatchRead(42, 1);//秒脉冲/无功电能选择
|
|
byte[]? bytes07 = master.BatchRead(48, 1);//电流接线反向
|
|
byte[]? bytes08 = master.BatchRead(4576, 1);//电表清零
|
|
|
|
if (bytes04 != null)
|
|
{
|
|
ModbusDecoder.DecodeByT<PlcReadAndWrite1>(bytes04, plcInfo.PlcReadAndWritten1);
|
|
}
|
|
plcInfo.PlcReadAndWritten1.CheckBit = master.ModbusTcpNet.ByteTransform.TransUInt16(bytes05, 0);
|
|
plcInfo.PlcReadAndWritten1.PulsePerSecond = master.ModbusTcpNet.ByteTransform.TransUInt16(bytes06, 0);
|
|
plcInfo.PlcReadAndWritten1.CurrentReversal = master.ModbusTcpNet.ByteTransform.TransUInt16(bytes07, 0);
|
|
plcInfo.PlcReadAndWritten1.MeterReset = master.ModbusTcpNet.ByteTransform.TransUInt16(bytes08, 0);
|
|
|
|
}
|
|
}
|
|
}
|