| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- //PLC参数管理类
- using MvvmScaffoldFrame48.DLL.AlarmTools;
- using MvvmScaffoldFrame48.DLL.LogTools;
- using MvvmScaffoldFrame48.Model.StorageModel.PlcParameter;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MvvmScaffoldFrame48.DLL.CommunicationTools
- {
- public class PlcCommunManager
- {
- private static readonly Dictionary<int, ushort> BitMasks = new Dictionary<int, ushort>
- {
- // 寄存器位掩码
- {0, (ushort)(1 << 0)},
- {1, (ushort)(1 << 1)},
- {2, (ushort)(1 << 2)},
- {3, (ushort)(1 << 3)},
- {4, (ushort)(1 << 4)},
- {5, (ushort)(1 << 5)},
- {6, (ushort)(1 << 6)},
- {7, (ushort)(1 << 7)},
- {8, (ushort)(1 << 8)},
- {9, (ushort)(1 << 9)},
- {10, (ushort)(1 << 10)},
- {11, (ushort)(1 << 11)},
- {12, (ushort)(1 << 12)},
- {13, (ushort)(1 << 13)},
- {14, (ushort)(1 << 14)},
- {15, (ushort)(1 << 15)},
- };
- private static readonly Dictionary<string, PlcParameterModel> RegisterMap = new Dictionary<string, PlcParameterModel>
- {
- {"test1",new PlcValueBitParameterModel(0,0) }
- };
- private bool isConnect = false;
- public bool IsConnect
- {
- get
- {
- return isConnect ? modbusTcpClient.IsTcpClientConnected() : isConnect;
- }
- }
- public ModbusTcpClient modbusTcpClient = new ModbusTcpClient();
- public PlcCommunManager(string IpAddress)
- {
- ConnectModbus(IpAddress);
- }
- public void ConnectModbus(string ipAddress)
- {
- int i = 10;
- while (!modbusTcpClient.Connect(ipAddress) && i > 0)
- {
- isConnect = false;
- //SystemAlarm.AlarmAlert(AlarmMessageList.PLC通讯连接失败, $"Modbus通讯连接失败,目标IP:{ipAddress}", "DLL:MainThreadClass-ConnectModbus");
- SystemAlarm.AlarmAlert(AlarmMessageList.PLC通讯连接失败,
- $"PLC communication connection failed, target IP:{ipAddress}",
- $"PLC通讯连接失败, IP地址:{ipAddress}",
- "DLL:MainThreadClass-ConnectModbus");
- i--;
- Task.Delay(1000);
- }
- if (modbusTcpClient.Connect(ipAddress))
- {
- SystemAlarm.AlarmCancel(AlarmMessageList.PLC通讯连接失败);
- TxtLog.log("PLC通讯成功", 6);
- //FaultLog.RecordLogMessage($"Modbus通讯连接成功,目标IP:{ipAddress}", 6);
- isConnect = true;
- }
- }
- }
- }
|