| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- using HslCommunication;
- using HslCommunication.Core;
- using HslCommunication.ModBus;
- using HslCommunication.Profinet.Siemens;
- using SWRIS.Enums;
- using SWRIS.Models;
- using System;
- using System.Threading;
- using System.Threading.Tasks;
- namespace SWRIS.Core
- {
- public class CalibrationCore : IDisposable
- {
- private IReadWriteNet moduleDevice;
- private readonly CalibrationSettings config;
- private readonly ModuleStateModel moduleState;
- private readonly CancellationTokenSource ctsToken = new CancellationTokenSource();
- public bool IsConnected { get; set; }
- public event EventHandler<LimitData> LimitStateChanged;
- public event EventHandler<bool> ConnectionStateChanged;
- public CalibrationCore(CalibrationSettings config)
- {
- this.config = config;
- moduleState = new ModuleStateModel(false);
- InitializeModule();
- ConnectServer();
- ThreadPool.QueueUserWorkItem(o => ReadLimitData(ctsToken.Token));
- }
- private void InitializeModule()
- {
- // 根据模块类型创建相应的设备实例
- switch (config.ModuleType)
- {
- case ModuleType.S1200:
- moduleDevice = new SiemensS7Net(SiemensPLCS.S1200)
- {
- IpAddress = config.Tcp.IpAddress,
- Port = config.Tcp.Port,
- ConnectTimeOut = 3000,
- ReceiveTimeOut = 2000
- };
- break;
- case ModuleType.S1500:
- moduleDevice = new SiemensS7Net(SiemensPLCS.S1500)
- {
- IpAddress = config.Tcp.IpAddress,
- Port = config.Tcp.Port,
- ConnectTimeOut = 3000,
- ReceiveTimeOut = 2000
- };
- break;
- case ModuleType.S300:
- moduleDevice = new SiemensS7Net(SiemensPLCS.S300)
- {
- IpAddress = config.Tcp.IpAddress,
- Port = config.Tcp.Port,
- ConnectTimeOut = 3000,
- ReceiveTimeOut = 2000
- };
- break;
- case ModuleType.S400:
- moduleDevice = new SiemensS7Net(SiemensPLCS.S400)
- {
- IpAddress = config.Tcp.IpAddress,
- Port = config.Tcp.Port,
- ConnectTimeOut = 3000,
- ReceiveTimeOut = 2000
- };
- break;
- case ModuleType.S200:
- moduleDevice = new SiemensS7Net(SiemensPLCS.S200)
- {
- IpAddress = config.Tcp.IpAddress,
- Port = config.Tcp.Port,
- ConnectTimeOut = 3000,
- ReceiveTimeOut = 2000
- };
- break;
- case ModuleType.S200Smart:
- moduleDevice = new SiemensS7Net(SiemensPLCS.S200Smart)
- {
- IpAddress = config.Tcp.IpAddress,
- Port = config.Tcp.Port,
- ConnectTimeOut = 3000,
- ReceiveTimeOut = 2000
- };
- break;
- case ModuleType.ModbusTcp:
- moduleDevice = new ModbusTcpNet(config.Tcp.IpAddress, config.Tcp.Port, config.Tcp.Station)
- {
- ConnectTimeOut = 3000,
- ReceiveTimeOut = 2000
- };
- break;
- case ModuleType.ModbusRtu:
- moduleDevice = new ModbusRtu(config.Serial.Station);
- (moduleDevice as ModbusRtu).SerialPortInni(config.Serial.PortName,
- config.Serial.BaudRate,
- config.Serial.DataBits,
- config.Serial.StopBits,
- config.Serial.Parity);
- break;
- default:
- throw new NotSupportedException($"不支持的模块类型: {config.ModuleType}");
- }
- }
- private async void ReadLimitData(CancellationToken token)
- {
- OperateResult<bool> readLimitResult;
- while (!token.IsCancellationRequested)
- {
- try
- {
- if (IsConnected)
- {
- foreach (var limit in config.Limits)
- {
- if (!limit.IsEnable)
- continue;
- readLimitResult = await moduleDevice.ReadBoolAsync(limit.Address);
- if (StateMonitor(readLimitResult))
- {
- var limitState = (limit.IsReverse ? !readLimitResult.Content : readLimitResult.Content) ? LimitState.AtLimit : LimitState.NotInLimit;
- if (limit.State != limitState)
- {
- limit.State = limitState;
- LimitStateChanged?.Invoke(this, limit);
- }
- }
- await Task.Delay(50);
- }
- }
- }
- catch (Exception ex)
- {
- LogHelper.Error("读取限位信号时发生错误:" + ex.Message, ex);
- }
- finally
- {
- Thread.Sleep(200);
- }
- }
- }
- private bool StateMonitor(OperateResult<bool> readLimitResult)
- {
- if (readLimitResult.IsSuccess)
- {
- moduleState.Depth = moduleState.Depth < 0 ? 1 : moduleState.Depth > 20 ? 20 : moduleState.Depth + 1;
- if (!moduleState.IsOnline && moduleState.Depth >= 3)
- {
- ChangeModuleState(true);
- }
- return true;
- }
- else
- {
- moduleState.Depth = moduleState.Depth > 0 ? -1 : moduleState.Depth < -20 ? -20 : moduleState.Depth - 1;
- if (moduleState.IsOnline && moduleState.Depth <= -3)
- {
- ChangeModuleState(false);
- }
- return false;
- }
- }
- private void ChangeModuleState(bool isOnline)
- {
- moduleState.IsOnline = isOnline;
- moduleState.Depth = 0;
- ConnectionStateChanged?.Invoke(this, isOnline);
- }
- private bool ConnectDevice()
- {
- try
- {
- if (moduleDevice is SiemensS7Net siemensDevice)
- {
- return siemensDevice.ConnectServer().IsSuccess;
- }
- else if (moduleDevice is ModbusTcpNet modbusTcpDevice)
- {
- return modbusTcpDevice.ConnectServer().IsSuccess;
- }
- else if (moduleDevice is ModbusRtu modbusRtuDevice)
- {
- return modbusRtuDevice.Open().IsSuccess;
- }
- return false;
- }
- catch (Exception ex)
- {
- LogHelper.Error($"设备连接异常: {ex.Message}", ex);
- return false;
- }
- }
- private void CloseDeviceConnection()
- {
- try
- {
- if (moduleDevice is SiemensS7Net siemensDevice)
- {
- siemensDevice.ConnectClose();
- }
- else if (moduleDevice is ModbusTcpNet modbusTcpDevice)
- {
- modbusTcpDevice.ConnectClose();
- }
- else if (moduleDevice is ModbusRtu modbusRtuDevice)
- {
- modbusRtuDevice.Close();
- }
- if (moduleDevice is IDisposable disposable)
- {
- disposable.Dispose();
- }
- }
- catch (Exception ex)
- {
- LogHelper.Error($"关闭设备连接异常: {ex.Message}", ex);
- }
- finally
- {
- moduleDevice = null;
- IsConnected = false;
- }
- }
- public void ConnectServer()
- {
- ThreadPool.QueueUserWorkItem(action =>
- {
- while (!ctsToken.IsCancellationRequested)
- {
- if (!IsConnected)
- {
- try
- {
- if (ConnectDevice())
- {
- IsConnected = true;
- LogHelper.Info($"连接成功: {config.ModuleType} - {config.Tcp.IpAddress}:{config.Tcp.Port}");
- }
- }
- catch (Exception ex)
- {
- LogHelper.Error($"连接异常: {ex.Message}", ex);
- }
- }
- Thread.Sleep(5000);
- }
- });
- }
- public void CloseServer()
- {
- CloseDeviceConnection();
- }
- public void Dispose()
- {
- ctsToken?.Cancel();
- ctsToken?.Dispose();
- CloseDeviceConnection();
- }
- }
- }
|