diff --git a/HybirdFrameworkDriver/TcpServer/ModbusSession.cs b/HybirdFrameworkDriver/TcpServer/ModbusSession.cs deleted file mode 100644 index ecf6ed2..0000000 --- a/HybirdFrameworkDriver/TcpServer/ModbusSession.cs +++ /dev/null @@ -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 BusinessMap { get; set; } - public ModbusSession(ModbusTcpMaster.ModbusTcpMaster modbusTcpMaster) - { - this.ModbusTcpMaster = modbusTcpMaster; - this.IpAddr = modbusTcpMaster.Ip; - this.Key = modbusTcpMaster.connectId; - } - - - public bool Write(ModbusProperty property) - { - return ModbusTcpMaster.WriteValue(property); - } - public byte[]? Read(int registerNo, int length) - { - return ModbusTcpMaster.BatchRead(registerNo, length); - } - - - } -} diff --git a/Service/EquipmentInit.cs b/Service/EquipmentInit.cs new file mode 100644 index 0000000..30fde60 --- /dev/null +++ b/Service/EquipmentInit.cs @@ -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 + { + /// + /// 连接所有数据 + /// + /// + public void Connect() + { + ConnectPlc(); + } + + /// + /// 连接PLC + /// + /// + /// + public int ConnectPlc() + { + Thread thread = new Thread(() => ConnectPlcAsync()); + thread.Start(); + + return 0; + } + + /// + /// 连接PLC + /// + 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(bytes01, plcInfo.hostToPlc); + } + + var bytes02 = master.BatchRead(201, 222); + if (bytes02 != null) + { + ModbusDecoder.Decode(bytes02, plcInfo.plcToHost); + } + + var bytes03 = master.BatchRead(701, 10); + if (bytes03 != null) + { + ModbusDecoder.Decode(bytes03, plcInfo.plcFault); + } + //OperateResult result2 = ModbusTcpNet.Read("x=3;201", 222); + } + /* + if (plcInfo != null) + { + byte[]? bytes01 = master.BatchRead(0, 22); + if (bytes01 != null) + { + ModbusDecoder.DecodeByT(bytes01, plcInfo.PlcReadAndWritten2); + } + byte[]? bytes02 = master.BatchRead(256, 170); + if (bytes02 != null) + { + ModbusDecoder.DecodeByT(bytes02, plcInfo.PlcReadonly); + } + byte[]? bytes03 = master.BatchRead(39424, 108); + if (bytes03 != null) + { + ModbusDecoder.DecodeByT(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(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); + + } + */ + } + } + } +} \ No newline at end of file diff --git a/Service/Init/Entity/ChargerStaticInfo.cs b/Service/Init/Entity/ChargerStaticInfo.cs new file mode 100644 index 0000000..28267db --- /dev/null +++ b/Service/Init/Entity/ChargerStaticInfo.cs @@ -0,0 +1,13 @@ +using System.Collections.Concurrent; + +namespace Service.Init.Entity +{ + public class ChargerStaticInfo + { + /// + /// PLC管理 + /// + public static readonly ConcurrentDictionary PlcInfos = + new ConcurrentDictionary(); + } +} \ No newline at end of file diff --git a/Service/Init/Entity/ConnectState.cs b/Service/Init/Entity/ConnectState.cs new file mode 100644 index 0000000..8474612 --- /dev/null +++ b/Service/Init/Entity/ConnectState.cs @@ -0,0 +1,12 @@ +namespace Service.Init.Entity +{ + /// + ///PLC连接状态枚举 + /// + public enum ConnectState + { + Disconnect = 0, + Connecting = 1, + Connected = 2 + } +} \ No newline at end of file diff --git a/Service/Init/Entity/PlcInfo.cs b/Service/Init/Entity/PlcInfo.cs new file mode 100644 index 0000000..7af99c0 --- /dev/null +++ b/Service/Init/Entity/PlcInfo.cs @@ -0,0 +1,84 @@ +using HybirdFrameworkDriver.ModbusTcpMaster; +using HybirdFrameworkDriver.Session; +using HybirdFrameworkServices.Plc; + +namespace Service.Init.Entity +{ + public class PlcInfo + { + #region 字段 + + /// + /// 设备编号 + /// + private string _devNo = "001"; + + /// + /// 设备名称 + /// + private string _devname = "换电PLC"; + + /// + /// 服务端连接IP + /// + private string _ipaddr = "172.0.20.48"; + + /// + /// 服务端连接端口 + /// + private int _port = 502; + + /// + /// 站号 + /// + private byte _site = 0x01; + + /// + /// 连接超时时间。单位秒 + /// + private int _connecttimeout = 10; + + /// + /// 保持活跃时间。单位秒 + /// + private int _keepalive = 30; + + /// + /// 连接状态 + /// + 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 value) + { + bool bResult = false; + ModbusSession session = SessionMgr.GetModbusSession(ChannelId); + bResult = session.Write(value); + return bResult; + } + + public bool Writeint16(ModbusProperty value) + { + bool bResult = false; + ModbusSession session = SessionMgr.GetModbusSession(ChannelId); + bResult = session.Write(value); + return bResult; + } + } +} \ No newline at end of file diff --git a/Service/Plc/HostToPlc.cs b/Service/Plc/HostToPlc.cs new file mode 100644 index 0000000..88eae77 --- /dev/null +++ b/Service/Plc/HostToPlc.cs @@ -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 ProtocolVersion1 { get; set; } = new(40001); // 协议版本 + public ModbusProperty ProtocolVersion2 { get; set; } = new(40002); // 协议版本 + public ModbusProperty ProtocolVersion3 { get; set; } = new(40003); // 协议版本 + public ModbusProperty CommunicationDiagnosis { get; set; } = new(40004); // 通讯诊断 + public ModbusProperty ModeControl { get; set; } = new(40005); // 模式控制 + public ModbusProperty EquipmentControl { get; set; } = new(40006); // 设备控制 + public ModbusProperty ExhaustFanControl { get; set; } = new(40007); // 排风扇控制 + public ModbusProperty LightingControl { get; set; } = new(40008); // 灯光控制 + public ModbusProperty Standby1 { get; set; } = new(40009); // 备用 + public ModbusProperty Standby2 { get; set; } = new(40010); // 备用 + public ModbusProperty Seconds { get; set; } = new(40011); // 对时时钟:秒 + public ModbusProperty Points { get; set; } = new(40012); // 对时时钟:分 + public ModbusProperty Hour { get; set; } = new(40013); // 对时时钟:时 + public ModbusProperty Day { get; set; } = new(40014); // 对时时钟:天 + public ModbusProperty Month { get; set; } = new(40015); // 对时时钟:月 + public ModbusProperty Years { get; set; } = new(40016); // 对时时钟:年 + public ModbusProperty Standby3 { get; set; } = new(40017, length: 4); // 备用3 + public ModbusProperty ChargingStatus { get; set; } = new(40021, length: 40); // 仓位充电状态 + public ModbusProperty ChargingStatus01 { get; set; } = new(40021); // 仓位充电状态 + public ModbusProperty ChargingStatus02 { get; set; } = new(40022); // 仓位充电状态 + public ModbusProperty ChargingStatus03 { get; set; } = new(40023); // 仓位充电状态 + public ModbusProperty ChargingStatus04 { get; set; } = new(40024); // 仓位充电状态 + public ModbusProperty ChargingStatus05 { get; set; } = new(40025); // 仓位充电状态 + public ModbusProperty ChargingStatus06 { get; set; } = new(40026); // 仓位充电状态 + public ModbusProperty ChargingStatus07 { get; set; } = new(40027); // 仓位充电状态 + public ModbusProperty ChargingStatus08 { get; set; } = new(40028); // 仓位充电状态 + public ModbusProperty ChargingStatus09 { get; set; } = new(40029); // 仓位充电状态 + public ModbusProperty ChargingStatus10 { get; set; } = new(40030); // 仓位充电状态 + public ModbusProperty ChargingStatus11 { get; set; } = new(40031); // 仓位充电状态 + public ModbusProperty ChargingStatus12 { get; set; } = new(40032); // 仓位充电状态 + public ModbusProperty ChargingStatus13 { get; set; } = new(40033); // 仓位充电状态 + public ModbusProperty ChargingStatus14 { get; set; } = new(40034); // 仓位充电状态 + public ModbusProperty ChargingStatus15 { get; set; } = new(40035); // 仓位充电状态 + public ModbusProperty ChargingStatus16 { get; set; } = new(40036); // 仓位充电状态 + public ModbusProperty ChargingStatus17 { get; set; } = new(40037); // 仓位充电状态 + public ModbusProperty ChargingStatus18 { get; set; } = new(40038); // 仓位充电状态 + public ModbusProperty ChargingStatus19 { get; set; } = new(40039); // 仓位充电状态 + public ModbusProperty ChargingStatus20 { get; set; } = new(40040); // 仓位充电状态 + public ModbusProperty BatteryType { get; set; } = new(40061, length: 40); // 仓位所在电池型号 + + public ModbusProperty BatteryType01 { get; set; } = new(40061); // 仓位所在电池型号 + public ModbusProperty BatteryType02 { get; set; } = new(40062); // 仓位所在电池型号 + public ModbusProperty BatteryType03 { get; set; } = new(40063); // 仓位所在电池型号 + public ModbusProperty BatteryType04 { get; set; } = new(40064); // 仓位所在电池型号 + public ModbusProperty BatteryType05 { get; set; } = new(40065); // 仓位所在电池型号 + public ModbusProperty BatteryType06 { get; set; } = new(40066); // 仓位所在电池型号 + public ModbusProperty BatteryType07 { get; set; } = new(40067); // 仓位所在电池型号 + public ModbusProperty BatteryType08 { get; set; } = new(40068); // 仓位所在电池型号 + public ModbusProperty BatteryType09 { get; set; } = new(40069); // 仓位所在电池型号 + public ModbusProperty BatteryType10 { get; set; } = new(40070); // 仓位所在电池型号 + public ModbusProperty BatteryType11 { get; set; } = new(40071); // 仓位所在电池型号 + public ModbusProperty BatteryType12 { get; set; } = new(40072); // 仓位所在电池型号 + public ModbusProperty BatteryType13 { get; set; } = new(40073); // 仓位所在电池型号 + public ModbusProperty BatteryType14 { get; set; } = new(40074); // 仓位所在电池型号 + public ModbusProperty BatteryType15 { get; set; } = new(40075); // 仓位所在电池型号 + public ModbusProperty BatteryType16 { get; set; } = new(40076); // 仓位所在电池型号 + public ModbusProperty BatteryType17 { get; set; } = new(40077); // 仓位所在电池型号 + public ModbusProperty BatteryType18 { get; set; } = new(40078); // 仓位所在电池型号 + public ModbusProperty BatteryType19 { get; set; } = new(40079); // 仓位所在电池型号 + public ModbusProperty BatteryType20 { get; set; } = new(40080); // 仓位所在电池型号 + + public ModbusProperty VehicleParkingLocation { get; set; } = new(40101); // 车辆驻车位置 + public ModbusProperty VehicleParkingStatus { get; set; } = new(40102); // 车辆驻车状态 + public ModbusProperty LightIn { get; set; } = new(40103); // "三色灯控制整站状态(入口)" + public ModbusProperty LightOut { get; set; } = new(40104); // "三色灯控制整站状态(出口)" + public ModbusProperty StopCommand { get; set; } = new(40105); // 电池包锁止异常,暂停命令 + public ModbusProperty Standby4 { get; set; } = new(40106, length: 5); // 备用4 + public ModbusProperty TaskType { get; set; } = new(40111); // 任务类型 + public ModbusProperty EntrySelection { get; set; } = new(40112); // 入仓位选择 + public ModbusProperty ExitSelection { get; set; } = new(40113); // 出仓仓位选择 + public ModbusProperty BatteryPackType { get; set; } = new(40114); // 电池包类型 + public ModbusProperty TaskEnablement { get; set; } = new(40115); // 任务使能 + + } +} diff --git a/Service/Plc/PlcFault.cs b/Service/Plc/PlcFault.cs new file mode 100644 index 0000000..0b35d9a --- /dev/null +++ b/Service/Plc/PlcFault.cs @@ -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 ErrorCode01 { get; set; } = new(40701); // 错误码 + public ModbusProperty ErrorCode02 { get; set; } = new(40702); // 错误码 + public ModbusProperty ErrorCode03 { get; set; } = new(40703); // 错误码 + public ModbusProperty ErrorCode04 { get; set; } = new(40704); // 错误码 + public ModbusProperty ErrorCode05 { get; set; } = new(40705); // 错误码 + public ModbusProperty ErrorCode06 { get; set; } = new(40706); // 错误码 + public ModbusProperty ErrorCode07 { get; set; } = new(40707); // 错误码 + public ModbusProperty ErrorCode08 { get; set; } = new(40708); // 错误码 + public ModbusProperty ErrorCode09 { get; set; } = new(40709); // 错误码 + public ModbusProperty ErrorCode10 { get; set; } = new(40710); // 错误码 + } +} diff --git a/Service/Plc/PlcToHost.cs b/Service/Plc/PlcToHost.cs new file mode 100644 index 0000000..3345a61 --- /dev/null +++ b/Service/Plc/PlcToHost.cs @@ -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 +{ + /// + /// + /// + public class PlcToHost + { + public ModbusProperty ProtocolVersion1 { get; set; } = new(40201); // 协议版本 + public ModbusProperty ProtocolVersion2 { get; set; } = new(40202); // 协议版本 + public ModbusProperty ProtocolVersion3 { get; set; } = new(40203); // 协议版本 + public ModbusProperty CommunicationDiagnosis { get; set; } = new(40204); // 通讯诊断 + public ModbusProperty ModeControl { get; set; } = new(40205); // 模式状态 + public ModbusProperty DeviceSystemStatus { get; set; } = new(40206); // 设备系统状态 + + //public ModbusProperty type00 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT0:复位 + //public ModbusProperty type01 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT1:启动 + //public ModbusProperty type02 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT2:暂停 + //public ModbusProperty type03 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT3:急停 + //public ModbusProperty type04 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT4: + //public ModbusProperty type05 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT5:整站断电 + //public ModbusProperty type06 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT6:自动运行中 + //public ModbusProperty type07 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT7:PLC停机报警 + //public ModbusProperty type08 { get; set; } = new(400206,length:1, type: ModbusDataType.Bit); //BIT8:PLC提示预警 + + public ModbusProperty RemoteLocalControlState { get; set; } = new(40207); // 遥本控状态 + public ModbusProperty LightStatus { get; set; } = new(40208); // 灯光状态 + public ModbusProperty ExhaustFanStatus { get; set; } = new(40209); // 排风扇状态 + public ModbusProperty WaterStatusStation { get; set; } = new(40210); // 站内积水状态 + public ModbusProperty value09 { get; set; } = new(40211, length: 30); // 备用 + public ModbusProperty InPosition { get; set; } = new(40241, length: 40); // 在位状态 + public ModbusProperty value11 { get; set; } = new(40281, length: 20); // 备用 + public ModbusProperty CarStatus { get; set; } = new(40301); // 载行车设备状态 + + public ModbusProperty value13 { get; set; } = new(40302); // 备用 + public ModbusProperty value14 { get; set; } = new(40303); // 备用 + public ModbusProperty value15 { get; set; } = new(40304); // 备用 + public ModbusProperty value16 { get; set; } = new(40305); // 备用 + public ModbusProperty PercentageTorqueX { get; set; } = new(40306); // x轴扭矩百分比 + public ModbusProperty PercentageTorqueY { get; set; } = new(40307); // y轴扭矩百分比 + public ModbusProperty PercentageTorqueZ { get; set; } = new(40308); // z轴扭矩百分比 + public ModbusProperty value20 { get; set; } = new(40309, length: 92); // 备用 + public ModbusProperty RadarStatesIn{ get; set; } = new(40401); // " 入口雷达状态" + public ModbusProperty RadarStatesOut { get; set; } = new(40402); // " 出口雷达状态" + public ModbusProperty SpotPhotoelectricSignal { get; set; } = new(40403); // 到位光电信号 + public ModbusProperty PhysicalButtonState { get; set; } = new(40404); // 实体按钮状态 + public ModbusProperty LightIn { get; set; } = new(40405); // "三色灯控制通道状态(入口)" + public ModbusProperty LightOut { get; set; } = new(40406); // "三色灯控制通道状态(出口)" + public ModbusProperty TaskType { get; set; } = new(40407); // "任务类型状态(是否允许允许执行任务)" + public ModbusProperty TaskStates { get; set; } = new(40408); // 任务状态 + public ModbusProperty OperationalTrainNumber { get; set; } = new(40409); // 执行任务行车号 + public ModbusProperty ChannelLocationState { get; set; } = new(40410); // 通道定位状态 + public ModbusProperty DeviationX { get; set; } = new(40411); // x偏差 + public ModbusProperty DeviationY { get; set; } = new(40412); // y偏差 + public ModbusProperty DeviationZ { get; set; } = new(40413); // z偏差 + public ModbusProperty value44 { get; set; } = new(40414, length: 7); // 备用 + + + + } +} diff --git a/WinFormStarter/FrmPlc.cs b/WinFormStarter/FrmPlc.cs index 8c10e8b..105783f 100644 --- a/WinFormStarter/FrmPlc.cs +++ b/WinFormStarter/FrmPlc.cs @@ -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 diff --git a/WinFormStarter/Program.cs b/WinFormStarter/Program.cs index ebd446a..e574e9c 100644 --- a/WinFormStarter/Program.cs +++ b/WinFormStarter/Program.cs @@ -3,6 +3,7 @@ using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Configuration; using HybirdFrameworkServices; using log4net.Config; +using Service; using SqlSugar; using SqlSugar.IOC;