合并框架

master
smartwyy 7 months ago
parent b1d28cc4d4
commit 12cf6f2067

@ -1,40 +0,0 @@
using HybirdFrameworkDriver.ModbusTcpMaster;
using log4net;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybirdFrameworkDriver.TcpServer
{
public class ModbusSession
{
private readonly ILog Log = LogManager.GetLogger(typeof(ModbusSession));
public ModbusTcpMaster.ModbusTcpMaster ModbusTcpMaster;
private String IpAddr { get; }
public String Key { get; set; }
public ConcurrentDictionary<String, Object> BusinessMap { get; set; }
public ModbusSession(ModbusTcpMaster.ModbusTcpMaster modbusTcpMaster)
{
this.ModbusTcpMaster = modbusTcpMaster;
this.IpAddr = modbusTcpMaster.Ip;
this.Key = modbusTcpMaster.connectId;
}
public bool Write<T>(ModbusProperty<T> property)
{
return ModbusTcpMaster.WriteValue(property);
}
public byte[]? Read(int registerNo, int length)
{
return ModbusTcpMaster.BatchRead(registerNo, length);
}
}
}

@ -0,0 +1,140 @@
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkDriver.ModbusTcpMaster;
using HybirdFrameworkDriver.Session;
using HybirdFrameworkServices.Plc;
using Service.Init.Entity;
namespace Service
{
[Scope("SingleInstance")]
public class EquipmentInit
{
/// <summary>
/// 连接所有数据
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
public void Connect()
{
ConnectPlc();
}
/// <summary>
/// 连接PLC
/// </summary>
/// <param name="param"></param>
/// <returns></returns>
public int ConnectPlc()
{
Thread thread = new Thread(() => ConnectPlcAsync());
thread.Start();
return 0;
}
/// <summary>
/// 连接PLC
/// </summary>
private void ConnectPlcAsync()
{
bool connected = false;
ModbusTcpMaster master = null;
//PLC连接
while (!connected)
{
master = new ModbusTcpMaster()
{
Ip = "172.0.20.66",
Port = 502,
};
master.ReadAction = BatchReadPlc; //启动线程一直读
master.Duration = 3000;
connected = master.Connect();
if (connected)
{
break;
}
Thread.Sleep(5000);
}
ModbusSession modbusSession = new ModbusSession(master);
master.connectId = master.Ip + master.Port;
SessionMgr.RegisterModbusSession(master.connectId, modbusSession);
SessionMgr.SetAttrModbus(modbusSession, "eqm_sn", 1);
PlcInfo waterColdInfo = new PlcInfo(master.connectId, 1);
ChargerStaticInfo.PlcInfos.TryAdd(1, waterColdInfo);
}
/**
*
*/
private static void BatchReadPlc(ModbusTcpMaster master)
{
while (true)
{
int sn = 3; //(int)SessionMgr.GetAttrModbus(master.connectId, "eqm_sn");
ChargerStaticInfo.PlcInfos.TryGetValue(sn, out PlcInfo plcInfo);
if (plcInfo != null)
{
var bytes01 = master.BatchRead(1, 115);
if (bytes01 != null)
{
ModbusDecoder.Decode<HostToPlc>(bytes01, plcInfo.hostToPlc);
}
var bytes02 = master.BatchRead(201, 222);
if (bytes02 != null)
{
ModbusDecoder.Decode<PlcToHost>(bytes02, plcInfo.plcToHost);
}
var bytes03 = master.BatchRead(701, 10);
if (bytes03 != null)
{
ModbusDecoder.Decode<PlcFault>(bytes03, plcInfo.plcFault);
}
//OperateResult<byte[]> result2 = ModbusTcpNet.Read("x=3;201", 222);
}
/*
if (plcInfo != null)
{
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);
}
*/
}
}
}
}

@ -0,0 +1,13 @@
using System.Collections.Concurrent;
namespace Service.Init.Entity
{
public class ChargerStaticInfo
{
/// <summary>
/// PLC管理
/// </summary>
public static readonly ConcurrentDictionary<int, PlcInfo> PlcInfos =
new ConcurrentDictionary<int, PlcInfo>();
}
}

@ -0,0 +1,12 @@
namespace Service.Init.Entity
{
/// <summary>
///PLC连接状态枚举
/// </summary>
public enum ConnectState
{
Disconnect = 0,
Connecting = 1,
Connected = 2
}
}

@ -0,0 +1,84 @@
using HybirdFrameworkDriver.ModbusTcpMaster;
using HybirdFrameworkDriver.Session;
using HybirdFrameworkServices.Plc;
namespace Service.Init.Entity
{
public class PlcInfo
{
#region 字段
/// <summary>
/// 设备编号
/// </summary>
private string _devNo = "001";
/// <summary>
/// 设备名称
/// </summary>
private string _devname = "换电PLC";
/// <summary>
/// 服务端连接IP
/// </summary>
private string _ipaddr = "172.0.20.48";
/// <summary>
/// 服务端连接端口
/// </summary>
private int _port = 502;
/// <summary>
/// 站号
/// </summary>
private byte _site = 0x01;
/// <summary>
/// 连接超时时间。单位秒
/// </summary>
private int _connecttimeout = 10;
/// <summary>
/// 保持活跃时间。单位秒
/// </summary>
private int _keepalive = 30;
/// <summary>
/// 连接状态
/// </summary>
private ConnectState _connect_state = ConnectState.Disconnect;
#endregion 字段
public string ChannelId;
public int EqmSn;
public HostToPlc hostToPlc = new HostToPlc();
public PlcToHost plcToHost = new PlcToHost();
public PlcFault plcFault = new PlcFault();
public PlcInfo(string channelId, int eqmSn)
{
ChannelId = channelId;
EqmSn = eqmSn;
}
public bool WriteUint16(ModbusProperty<UInt16> value)
{
bool bResult = false;
ModbusSession session = SessionMgr.GetModbusSession(ChannelId);
bResult = session.Write<UInt16>(value);
return bResult;
}
public bool Writeint16(ModbusProperty<Int16> value)
{
bool bResult = false;
ModbusSession session = SessionMgr.GetModbusSession(ChannelId);
bResult = session.Write<Int16>(value);
return bResult;
}
}
}

@ -0,0 +1,87 @@
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkDriver.ModbusTcpMaster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybirdFrameworkServices.Plc
{
public class HostToPlc
{
public ModbusProperty<short> ProtocolVersion1 { get; set; } = new(40001); // 协议版本
public ModbusProperty<short> ProtocolVersion2 { get; set; } = new(40002); // 协议版本
public ModbusProperty<short> ProtocolVersion3 { get; set; } = new(40003); // 协议版本
public ModbusProperty<ushort> CommunicationDiagnosis { get; set; } = new(40004); // 通讯诊断
public ModbusProperty<ushort> ModeControl { get; set; } = new(40005); // 模式控制
public ModbusProperty<ushort> EquipmentControl { get; set; } = new(40006); // 设备控制
public ModbusProperty<ushort> ExhaustFanControl { get; set; } = new(40007); // 排风扇控制
public ModbusProperty<ushort> LightingControl { get; set; } = new(40008); // 灯光控制
public ModbusProperty<ushort> Standby1 { get; set; } = new(40009); // 备用
public ModbusProperty<ushort> Standby2 { get; set; } = new(40010); // 备用
public ModbusProperty<ushort> Seconds { get; set; } = new(40011); // 对时时钟:秒
public ModbusProperty<ushort> Points { get; set; } = new(40012); // 对时时钟:分
public ModbusProperty<ushort> Hour { get; set; } = new(40013); // 对时时钟:时
public ModbusProperty<ushort> Day { get; set; } = new(40014); // 对时时钟:天
public ModbusProperty<ushort> Month { get; set; } = new(40015); // 对时时钟:月
public ModbusProperty<ushort> Years { get; set; } = new(40016); // 对时时钟:年
public ModbusProperty<string> Standby3 { get; set; } = new(40017, length: 4); // 备用3
public ModbusProperty<string> ChargingStatus { get; set; } = new(40021, length: 40); // 仓位充电状态
public ModbusProperty<short> ChargingStatus01 { get; set; } = new(40021); // 仓位充电状态
public ModbusProperty<short> ChargingStatus02 { get; set; } = new(40022); // 仓位充电状态
public ModbusProperty<short> ChargingStatus03 { get; set; } = new(40023); // 仓位充电状态
public ModbusProperty<short> ChargingStatus04 { get; set; } = new(40024); // 仓位充电状态
public ModbusProperty<short> ChargingStatus05 { get; set; } = new(40025); // 仓位充电状态
public ModbusProperty<short> ChargingStatus06 { get; set; } = new(40026); // 仓位充电状态
public ModbusProperty<short> ChargingStatus07 { get; set; } = new(40027); // 仓位充电状态
public ModbusProperty<short> ChargingStatus08 { get; set; } = new(40028); // 仓位充电状态
public ModbusProperty<short> ChargingStatus09 { get; set; } = new(40029); // 仓位充电状态
public ModbusProperty<short> ChargingStatus10 { get; set; } = new(40030); // 仓位充电状态
public ModbusProperty<short> ChargingStatus11 { get; set; } = new(40031); // 仓位充电状态
public ModbusProperty<short> ChargingStatus12 { get; set; } = new(40032); // 仓位充电状态
public ModbusProperty<short> ChargingStatus13 { get; set; } = new(40033); // 仓位充电状态
public ModbusProperty<short> ChargingStatus14 { get; set; } = new(40034); // 仓位充电状态
public ModbusProperty<short> ChargingStatus15 { get; set; } = new(40035); // 仓位充电状态
public ModbusProperty<short> ChargingStatus16 { get; set; } = new(40036); // 仓位充电状态
public ModbusProperty<short> ChargingStatus17 { get; set; } = new(40037); // 仓位充电状态
public ModbusProperty<short> ChargingStatus18 { get; set; } = new(40038); // 仓位充电状态
public ModbusProperty<short> ChargingStatus19 { get; set; } = new(40039); // 仓位充电状态
public ModbusProperty<short> ChargingStatus20 { get; set; } = new(40040); // 仓位充电状态
public ModbusProperty<string> BatteryType { get; set; } = new(40061, length: 40); // 仓位所在电池型号
public ModbusProperty<short> BatteryType01 { get; set; } = new(40061); // 仓位所在电池型号
public ModbusProperty<short> BatteryType02 { get; set; } = new(40062); // 仓位所在电池型号
public ModbusProperty<short> BatteryType03 { get; set; } = new(40063); // 仓位所在电池型号
public ModbusProperty<short> BatteryType04 { get; set; } = new(40064); // 仓位所在电池型号
public ModbusProperty<short> BatteryType05 { get; set; } = new(40065); // 仓位所在电池型号
public ModbusProperty<short> BatteryType06 { get; set; } = new(40066); // 仓位所在电池型号
public ModbusProperty<short> BatteryType07 { get; set; } = new(40067); // 仓位所在电池型号
public ModbusProperty<short> BatteryType08 { get; set; } = new(40068); // 仓位所在电池型号
public ModbusProperty<short> BatteryType09 { get; set; } = new(40069); // 仓位所在电池型号
public ModbusProperty<short> BatteryType10 { get; set; } = new(40070); // 仓位所在电池型号
public ModbusProperty<short> BatteryType11 { get; set; } = new(40071); // 仓位所在电池型号
public ModbusProperty<short> BatteryType12 { get; set; } = new(40072); // 仓位所在电池型号
public ModbusProperty<short> BatteryType13 { get; set; } = new(40073); // 仓位所在电池型号
public ModbusProperty<short> BatteryType14 { get; set; } = new(40074); // 仓位所在电池型号
public ModbusProperty<short> BatteryType15 { get; set; } = new(40075); // 仓位所在电池型号
public ModbusProperty<short> BatteryType16 { get; set; } = new(40076); // 仓位所在电池型号
public ModbusProperty<short> BatteryType17 { get; set; } = new(40077); // 仓位所在电池型号
public ModbusProperty<short> BatteryType18 { get; set; } = new(40078); // 仓位所在电池型号
public ModbusProperty<short> BatteryType19 { get; set; } = new(40079); // 仓位所在电池型号
public ModbusProperty<short> BatteryType20 { get; set; } = new(40080); // 仓位所在电池型号
public ModbusProperty<ushort> VehicleParkingLocation { get; set; } = new(40101); // 车辆驻车位置
public ModbusProperty<ushort> VehicleParkingStatus { get; set; } = new(40102); // 车辆驻车状态
public ModbusProperty<ushort> LightIn { get; set; } = new(40103); // "三色灯控制整站状态(入口)"
public ModbusProperty<ushort> LightOut { get; set; } = new(40104); // "三色灯控制整站状态(出口)"
public ModbusProperty<ushort> StopCommand { get; set; } = new(40105); // 电池包锁止异常,暂停命令
public ModbusProperty<string> Standby4 { get; set; } = new(40106, length: 5); // 备用4
public ModbusProperty<ushort> TaskType { get; set; } = new(40111); // 任务类型
public ModbusProperty<ushort> EntrySelection { get; set; } = new(40112); // 入仓位选择
public ModbusProperty<ushort> ExitSelection { get; set; } = new(40113); // 出仓仓位选择
public ModbusProperty<ushort> BatteryPackType { get; set; } = new(40114); // 电池包类型
public ModbusProperty<ushort> TaskEnablement { get; set; } = new(40115); // 任务使能
}
}

@ -0,0 +1,23 @@
using HybirdFrameworkDriver.ModbusTcpMaster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybirdFrameworkServices.Plc
{
public class PlcFault
{
public ModbusProperty<ushort> ErrorCode01 { get; set; } = new(40701); // 错误码
public ModbusProperty<ushort> ErrorCode02 { get; set; } = new(40702); // 错误码
public ModbusProperty<ushort> ErrorCode03 { get; set; } = new(40703); // 错误码
public ModbusProperty<ushort> ErrorCode04 { get; set; } = new(40704); // 错误码
public ModbusProperty<ushort> ErrorCode05 { get; set; } = new(40705); // 错误码
public ModbusProperty<ushort> ErrorCode06 { get; set; } = new(40706); // 错误码
public ModbusProperty<ushort> ErrorCode07 { get; set; } = new(40707); // 错误码
public ModbusProperty<ushort> ErrorCode08 { get; set; } = new(40708); // 错误码
public ModbusProperty<ushort> ErrorCode09 { get; set; } = new(40709); // 错误码
public ModbusProperty<ushort> ErrorCode10 { get; set; } = new(40710); // 错误码
}
}

@ -0,0 +1,69 @@
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkDriver.ModbusTcpMaster;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybirdFrameworkServices.Plc
{
/// <summary>
///
/// </summary>
public class PlcToHost
{
public ModbusProperty<ushort> ProtocolVersion1 { get; set; } = new(40201); // 协议版本
public ModbusProperty<ushort> ProtocolVersion2 { get; set; } = new(40202); // 协议版本
public ModbusProperty<ushort> ProtocolVersion3 { get; set; } = new(40203); // 协议版本
public ModbusProperty<ushort> CommunicationDiagnosis { get; set; } = new(40204); // 通讯诊断
public ModbusProperty<ushort> ModeControl { get; set; } = new(40205); // 模式状态
public ModbusProperty<ushort> DeviceSystemStatus { get; set; } = new(40206); // 设备系统状态
//public ModbusProperty<bool> type00 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT0:复位
//public ModbusProperty<bool> type01 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT1:启动
//public ModbusProperty<bool> type02 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT2:暂停
//public ModbusProperty<bool> type03 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT3:急停
//public ModbusProperty<bool> type04 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT4:
//public ModbusProperty<bool> type05 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT5:整站断电
//public ModbusProperty<bool> type06 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT6:自动运行中
//public ModbusProperty<bool> type07 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT7:PLC停机报警
//public ModbusProperty<bool> type08 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT8:PLC提示预警
public ModbusProperty<ushort> RemoteLocalControlState { get; set; } = new(40207); // 遥本控状态
public ModbusProperty<ushort> LightStatus { get; set; } = new(40208); // 灯光状态
public ModbusProperty<ushort> ExhaustFanStatus { get; set; } = new(40209); // 排风扇状态
public ModbusProperty<ushort> WaterStatusStation { get; set; } = new(40210); // 站内积水状态
public ModbusProperty<string> value09 { get; set; } = new(40211, length: 30); // 备用
public ModbusProperty<string> InPosition { get; set; } = new(40241, length: 40); // 在位状态
public ModbusProperty<string> value11 { get; set; } = new(40281, length: 20); // 备用
public ModbusProperty<ushort> CarStatus { get; set; } = new(40301); // 载行车设备状态
public ModbusProperty<ushort> value13 { get; set; } = new(40302); // 备用
public ModbusProperty<ushort> value14 { get; set; } = new(40303); // 备用
public ModbusProperty<ushort> value15 { get; set; } = new(40304); // 备用
public ModbusProperty<ushort> value16 { get; set; } = new(40305); // 备用
public ModbusProperty<short> PercentageTorqueX { get; set; } = new(40306); // x轴扭矩百分比
public ModbusProperty<short> PercentageTorqueY { get; set; } = new(40307); // y轴扭矩百分比
public ModbusProperty<short> PercentageTorqueZ { get; set; } = new(40308); // z轴扭矩百分比
public ModbusProperty<string> value20 { get; set; } = new(40309, length: 92); // 备用
public ModbusProperty<ushort> RadarStatesIn{ get; set; } = new(40401); // " 入口雷达状态"
public ModbusProperty<ushort> RadarStatesOut { get; set; } = new(40402); // " 出口雷达状态"
public ModbusProperty<ushort> SpotPhotoelectricSignal { get; set; } = new(40403); // 到位光电信号
public ModbusProperty<ushort> PhysicalButtonState { get; set; } = new(40404); // 实体按钮状态
public ModbusProperty<ushort> LightIn { get; set; } = new(40405); // "三色灯控制通道状态(入口)"
public ModbusProperty<ushort> LightOut { get; set; } = new(40406); // "三色灯控制通道状态(出口)"
public ModbusProperty<ushort> TaskType { get; set; } = new(40407); // "任务类型状态(是否允许允许执行任务)"
public ModbusProperty<ushort> TaskStates { get; set; } = new(40408); // 任务状态
public ModbusProperty<ushort> OperationalTrainNumber { get; set; } = new(40409); // 执行任务行车号
public ModbusProperty<ushort> ChannelLocationState { get; set; } = new(40410); // 通道定位状态
public ModbusProperty<short> DeviationX { get; set; } = new(40411); // x偏差
public ModbusProperty<short> DeviationY { get; set; } = new(40412); // y偏差
public ModbusProperty<short> DeviationZ { get; set; } = new(40413); // z偏差
public ModbusProperty<string> value44 { get; set; } = new(40414, length: 7); // 备用
}
}

@ -10,10 +10,10 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HybirdFrameworkDriver.ModbusTcpMaster;
using HybirdFrameworkServices.Init.Entity;
using HybirdFrameworkServices.Plc;
using HybirdFrameworkDriver.TcpServer;
using Newtonsoft.Json.Linq;
using Service.Init.Entity;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WinFormStarter

@ -3,6 +3,7 @@ using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Configuration;
using HybirdFrameworkServices;
using log4net.Config;
using Service;
using SqlSugar;
using SqlSugar.IOC;

Loading…
Cancel
Save