|  |  |  | 
|---|
|  |  |  | #region << 版 本 注 释 >> | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /*---------------------------------------------------------------- | 
|---|
|  |  |  | * 命名空间:WIDESEAWCS_QuartzJob | 
|---|
|  |  |  | * 创建者:胡童庆 | 
|---|
|  |  |  | 
|---|
|  |  |  | * 修改时间: | 
|---|
|  |  |  | * 版本:V1.0.1 | 
|---|
|  |  |  | * 修改说明: | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * | 
|---|
|  |  |  | *----------------------------------------------------------------*/ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #endregion << 版 本 注 释 >> | 
|---|
|  |  |  |  | 
|---|
|  |  |  | using HslCommunication; | 
|---|
|  |  |  | 
|---|
|  |  |  | public class CommonConveyorLine : IConveyorLine | 
|---|
|  |  |  | { | 
|---|
|  |  |  | #region Private Member | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 堆垛机通讯对象 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | 
|---|
|  |  |  | private bool _heartStatr = true; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private bool _isConnected = true; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #endregion | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #region Public Member | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 输送线通讯对象 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | 
|---|
|  |  |  | /// 设备状态 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | public DeviceStatus Status => DeviceStatus.Offline; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #endregion | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #region Constructor Function | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 构造函数 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | 
|---|
|  |  |  | _deviceName = deviceName; | 
|---|
|  |  |  | CheckConnect(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #endregion | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #region Private Method | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void CheckConnect() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | Task.Run(() => | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #endregion | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #region Public Method | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 读取PLC协议地址的数据 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | 
|---|
|  |  |  | /// <returns>读取到的数据</returns> | 
|---|
|  |  |  | public TRsult GetValue<TEnum, TRsult>(TEnum value, string deviceChildCode) where TEnum : Enum | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); | 
|---|
|  |  |  | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络【 {DeviceName} 】"); | 
|---|
|  |  |  | DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == value.ToString() && x.DeviceChildCode == deviceChildCode); | 
|---|
|  |  |  | return devicePro == null ? throw new Exception() : (TRsult)Communicator.ReadAsObj(devicePro.DeviceProAddress, devicePro.DeviceDataType); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// | 
|---|
|  |  |  | /// | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | /// <typeparam name="T"></typeparam> | 
|---|
|  |  |  | /// <param name="command"></param> | 
|---|
|  |  |  | 
|---|
|  |  |  | /// <exception cref="Exception"></exception> | 
|---|
|  |  |  | public bool SendCommand<T>(T command, string deviceChildCode) where T : IDataTransfer, new() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); | 
|---|
|  |  |  | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络【{DeviceName}】"); | 
|---|
|  |  |  | DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == nameof(DeviceCommand) && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); | 
|---|
|  |  |  | if (devicePro == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | /// <exception cref="Exception"></exception> | 
|---|
|  |  |  | public T ReadCustomer<T>(string deviceChildCode) where T : IDataTransfer, new() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); | 
|---|
|  |  |  | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络【{DeviceName}】"); | 
|---|
|  |  |  | DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == "ReadDeviceCommand" && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (devicePro == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception("未找到协议信息"); | 
|---|
|  |  |  | throw new Exception("未找到协议信息:" + deviceChildCode); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return Communicator.ReadCustomer<T>(devicePro.DeviceProAddress); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 读取PLC数据,返回自定义对象 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | /// <typeparam name="T">泛型</typeparam> | 
|---|
|  |  |  | /// <param name="deviceChildCode">子设备编号</param> | 
|---|
|  |  |  | /// <param name="deviceProParamType">参数类型</param> | 
|---|
|  |  |  | /// <returns>返回自定义对象或抛出异常</returns> | 
|---|
|  |  |  | /// <exception cref="Exception"></exception> | 
|---|
|  |  |  | public T ReadCustomer<T>(string deviceChildCode, string deviceProParamType) where T : IDataTransfer, new() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络【{DeviceName}】"); | 
|---|
|  |  |  | DeviceProDTO? devicePro = _deviceProDTOs.Where(x => x.DeviceProParamType == deviceProParamType && x.DeviceChildCode == deviceChildCode).OrderBy(x => x.DeviceProOffset).FirstOrDefault(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (devicePro == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | throw new Exception("未找到协议信息:" + deviceChildCode); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | where TEnum : Enum | 
|---|
|  |  |  | where TValue : notnull | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络"); | 
|---|
|  |  |  | if (!IsConnected) throw new Exception($"通讯连接错误,请检查网络【 {DeviceName} 】"); | 
|---|
|  |  |  | DeviceProDTO? devicePro = _deviceProDTOs.FirstOrDefault(x => x.DeviceProParamName == @enum.ToString() && x.DeviceChildCode == deviceChildCode); | 
|---|
|  |  |  | return devicePro == null ? throw new Exception() : Communicator.WriteObj(devicePro.DeviceProAddress, devicePro.DeviceDataType, value); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// | 
|---|
|  |  |  | /// 检测站台是否有货 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | /// <param name="deviceChildCode"></param> | 
|---|
|  |  |  | /// <returns></returns> | 
|---|
|  |  |  | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 检测站台是否有货 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | /// <param name="deviceChildCode"></param> | 
|---|
|  |  |  | /// <returns></returns> | 
|---|
|  |  |  | /// <exception cref="Exception"></exception> | 
|---|
|  |  |  | public bool IsOccupiedx(string deviceChildCode) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (Communicator.IsConnected) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | var device = _deviceProDTOs.Where(x => x.DeviceChildCode == deviceChildCode && x.DeviceProParamName == "InteractiveSignal").FirstOrDefault(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | object readStatus = Communicator.ReadAsObj(device.DeviceProAddress, device.DeviceDataType); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //todo 协议明细信息未获取到时抛出异常 | 
|---|
|  |  |  | if (readStatus.ToString() != "2") | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //todo 通讯未连接时抛出异常 | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void Dispose() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | _heartStatr = false; | 
|---|
|  |  |  | _communicator.Dispose(); | 
|---|
|  |  |  | GC.SuppressFinalize(this); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | #endregion | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|