127 lines
3.7 KiB
C#
127 lines
3.7 KiB
C#
using Cowain.Bake.BLL;
|
|
using Cowain.Bake.Communication.Interface;
|
|
using Cowain.Bake.Common.Models;
|
|
using HslCommunication;
|
|
using Newtonsoft.Json;
|
|
using Prism.Services.Dialogs;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Unity;
|
|
using Hsl = HslCommunication.Profinet.Omron;
|
|
using Cowain.Bake.Model.Entity;
|
|
using Cowain.Bake.Common.Core;
|
|
using Cowain.Bake.Common;
|
|
|
|
namespace Cowain.Bake.Communication.PLC
|
|
{
|
|
public class PLC_OmronFins : PLCBase
|
|
{
|
|
public override int Port { get; set; } = 9600;
|
|
private Hsl.OmronFinsNet plc;
|
|
|
|
public PLC_OmronFins(IUnityContainer unityContainer, IDialogService dialogService) : base(unityContainer, dialogService)
|
|
{
|
|
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
IsConnect = false;
|
|
try
|
|
{
|
|
plc?.ConnectClose();
|
|
plc?.Dispose();
|
|
plc = null;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogHelper.Instance.Error($"关闭PLC连接失败{ex}");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public override void Connect()
|
|
{
|
|
Close();
|
|
if (plc == null)
|
|
{
|
|
//HslCommunication.Profinet.OpenProtocol aa = new HslCommunication.Profinet.OpenProtocol();
|
|
plc = new Hsl.OmronFinsNet();
|
|
this.PLC = plc;
|
|
this.ByteTransform = plc.ByteTransform;
|
|
plc.ConnectTimeOut = 3000;
|
|
//plc.SetPersistentConnection();
|
|
}
|
|
try
|
|
{
|
|
plc.IpAddress = this.IpAddress;
|
|
plc.Port = this.Port;
|
|
plc.SA1 = 0;
|
|
plc.DA2 = this.Slot;
|
|
plc.ByteTransform.DataFormat = HslCommunication.Core.DataFormat.CDAB;
|
|
var result = plc.ConnectServer();
|
|
if (result.IsSuccess)
|
|
{
|
|
IsConnect = true;
|
|
}
|
|
else
|
|
{
|
|
IsConnect = false;
|
|
LogHelper.Instance.Error("连接PLC失败,请检查网络和参数");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
IsConnect = false;
|
|
LogHelper.Instance.Error($"连接PLC失败{ex}");
|
|
}
|
|
}
|
|
|
|
public override void GetJsonParam(string param)
|
|
{
|
|
dynamic d = JsonConvert.DeserializeObject<dynamic>(param);
|
|
this.IpAddress = d.Ip;
|
|
this.Port = d.Port;
|
|
this.Slot = d.Slot;
|
|
}
|
|
public override void AddressSplit(TagEntity tag)
|
|
{
|
|
//格式:D100,C100,W100,H100
|
|
Match m = Regex.Match(tag.Address, @"[a-zA-Z]{1}[0-9]+", RegexOptions.Singleline);
|
|
if (m.Success)
|
|
{
|
|
VariableDic[(int)tag.Id].AddressType = m.Value.Substring(0, 1);
|
|
VariableDic[(int)tag.Id].Address = m.Value.Substring(1);
|
|
}
|
|
}
|
|
|
|
public override void GetReadAddress()
|
|
{
|
|
foreach (var storage in Storages)
|
|
{
|
|
storage.ReadAddress = storage.AddressType + storage.StartAddress;
|
|
}
|
|
}
|
|
|
|
public override OperateResult Write<T>(string address, T data, int maxCount = Global.MAX_READS)
|
|
{
|
|
return Write<T>(plc, address, data, maxCount);
|
|
}
|
|
|
|
public override OperateResult Writes(string[] tags, object[] values, int maxCount = Global.MAX_READS)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
}
|