using ControlzEx.Standard; using MahApps.Metro.Controls; using MathNet.Numerics; using Opc.Ua; using OpcUaHelper; using OxyPlot.Axes; using Prism; using Prism.Ioc; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Net; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; using TeamAAS_VP; using TeamAAS_VP.Enums; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using TouchSocket.Core; using TouchSocket.Sockets; using static NPOI.HSSF.Util.HSSFColor; using static TeamAAS_VP.Core.CoordinateTransformer; namespace TeamAAS_VP.Core.Robots { public class XYZ_Platform : IRobot, INotifyPropertyChanged { public event Action ConnectedEvent; public event Action DisconnectedEvent; public event Action ReceivedEvent; public event Action> TrggerChangeEvent; public event Action SendEvent; public XYZ_Platform(RobotInfo robot) { RobotInfo = robot; Id = robot.Id; RobotIp = robot.IP; RobotPort = robot.Port; ConnectType = robot.ConnectType; Terminator = robot.Terminator; DataEncoding = robot.DataEncoding; Brand = RobotBrand.XYZ_Platform; var app = (PrismApplicationBase)App.Current; Management management = app.Container.Resolve(); Plc = management.PlcService.GetPlc(robot.PLC); Plc.ConnectChangedEvent += Plc_ConnectChangedEvent; } private void Plc_ConnectChangedEvent(object arg1, bool arg2) { CanExecute = arg2; } #region 属性 public TcpClient TcpClient { get; private set; } public TcpService TcpService { get; private set; } public Guid Id { get; set; } public int RobotPort { get; private set; } public string RobotIp { get; private set; } public TCPConnectType ConnectType { get; private set; } public Terminator Terminator { get; private set; } public DataEncoding DataEncoding { get; private set; } public bool IsConnected { get { if (Plc != null) { return Plc.IsConnected; } else { return false; } } } public int Timeout { get; set; } = 20000; private bool _CanExecute = true; public bool CanExecute { get { return _CanExecute; } set { SetProperty(ref _CanExecute, value); } } public int SelectedTool { get; private set; } = 0; public RobotBrand Brand { get; private set; } public IPlc Plc { get; private set; } public RobotInfo RobotInfo { get; private set; } #endregion #region 连接 public void Connect() { } public Task ConnectAsync() { return Task.CompletedTask; } public void Disconnect() { } public void Dispose() { } #endregion #region 控制 public bool Reset() { return false; } public async Task ResetAsync() { return false; } public bool Motor(bool state) { return false; } public async Task MotorAsync(bool state) { return false; } public bool Power(bool state) { return true; } public async Task PowerAsync(bool state) { return true; } public bool Speed(int value) { if (Plc == null || !Plc.IsConnected) { return false; } return Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.Speed, value); } public async Task SpeedAsync(int value) { if (Plc == null || !Plc.IsConnected) { return false; } return await Plc.OpcUaClient.WriteNodeAsync(RobotInfo.PlcRobotParameter.CommandParameter.Speed, value); } public bool Speedfactor(int value) { return true; } public async Task SpeedfactorAsync(int value) { return true; } public bool Speeds(double value) { return true; } public async Task SpeedsAsync(double value) { return true; } public bool Accel(int value) { if (Plc == null || !Plc.IsConnected) { return false; } return Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.Accel, value); } public async Task AccelAsync(int value) { if (Plc == null || !Plc.IsConnected) { return false; } return await Plc.OpcUaClient.WriteNodeAsync(RobotInfo.PlcRobotParameter.CommandParameter.Accel, value); } public bool Accels(double value) { return true; } public async Task AccelsAsync(double value) { return true; } public RPoint GetRobotPos() { if (Plc == null || !Plc.IsConnected) { return null; } try { for (int i = 0; i < 3; i++) { var data = Plc.OpcUaClient.ReadNodes(new List { RobotInfo.PlcRobotParameter.StateParameter.X, RobotInfo.PlcRobotParameter.StateParameter.Y, RobotInfo.PlcRobotParameter.StateParameter.Z, RobotInfo.PlcRobotParameter.StateParameter.U, }.Select(p => new Opc.Ua.NodeId(p)).ToArray()); if (StatusCode.IsGood(data[0].StatusCode) && StatusCode.IsGood(data[1].StatusCode) && StatusCode.IsGood(data[2].StatusCode) && StatusCode.IsGood(data[3].StatusCode)) { RPoint point = new RPoint(); point.X = Convert.ToSingle(data[0].Value); point.Y = Convert.ToSingle(data[1].Value); point.Z = Convert.ToSingle(data[2].Value); point.U = Convert.ToSingle(data[3].Value); point.V = 0; point.W = 0; point.Hand = RobotHand.Right; point.Local = 0; point.Tool = 0; return point; } } return null; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); throw ex; } } public async Task GetRobotPosAsync() { if (Plc == null || !Plc.IsConnected) { return null; } try { for (int i = 0; i < 3; i++) { var data = await Plc.OpcUaClient.ReadNodesAsync(new List { RobotInfo.PlcRobotParameter.StateParameter.X, RobotInfo.PlcRobotParameter.StateParameter.Y, RobotInfo.PlcRobotParameter.StateParameter.Z, RobotInfo.PlcRobotParameter.StateParameter.U, }.Select(p => new Opc.Ua.NodeId(p)).ToArray()); if (StatusCode.IsGood(data[0].StatusCode) && StatusCode.IsGood(data[1].StatusCode) && StatusCode.IsGood(data[2].StatusCode) && StatusCode.IsGood(data[3].StatusCode)) { RPoint point = new RPoint(); point.X = Convert.ToSingle(data[0].Value); point.Y = Convert.ToSingle(data[1].Value); point.Z = Convert.ToSingle(data[2].Value); point.U = Convert.ToSingle(data[3].Value); point.V = 0; point.W = 0; point.Hand = RobotHand.Right; point.Local = 0; point.Tool = 0; return point; } } return null; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); throw ex; } } public bool Jog(string axis, double distance) { if (Plc == null || !Plc.IsConnected) { return false; } try { CanExecute = false; int jogAxis = 0; switch (axis) { case "X": jogAxis = 1; break; case "Y": jogAxis = 2; break; case "Z": jogAxis = 3; break; case "U": jogAxis = 4; break; default: break; } if (jogAxis == 0) { return false; } List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.Distance, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, RobotInfo.PlcRobotParameter.CommandParameter.JogCmd, }; List values = new List() { (float)distance, false, jogAxis, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); return false; } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } } public async Task JogAsync(string axis, double distance) { if (Plc == null || !Plc.IsConnected) { return false; } return await Task.Run(() => { try { CanExecute = false; int jogAxis = 0; switch (axis) { case "X": jogAxis = 1; break; case "Y": jogAxis = 2; break; case "Z": jogAxis = 3; break; case "U": jogAxis = 4; break; default: break; } if (jogAxis == 0) { return false; } List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.Distance, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, RobotInfo.PlcRobotParameter.CommandParameter.JogCmd, }; List values = new List() { (float)distance, false, jogAxis, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); return false; } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } }); } public bool Joint(int joint, double distance) { return true; } public async Task JointAsync(int joint, double distance) { return true; } public bool Go(RPoint position) { if (Plc == null || !Plc.IsConnected) { return false; } try { CanExecute = false; List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.X, RobotInfo.PlcRobotParameter.CommandParameter.Y, RobotInfo.PlcRobotParameter.CommandParameter.Z, RobotInfo.PlcRobotParameter.CommandParameter.U, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, }; List values = new List() { position.X, position.Y, position.Z, position.U, false, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { if (Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true)) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); return false; } } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } } public async Task GoAsync(RPoint position) { return await Task.Run(() => { if (Plc == null || !Plc.IsConnected) { return false; } try { CanExecute = false; List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.X, RobotInfo.PlcRobotParameter.CommandParameter.Y, RobotInfo.PlcRobotParameter.CommandParameter.Z, RobotInfo.PlcRobotParameter.CommandParameter.U, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, }; List values = new List() { position.X, position.Y, position.Z, position.U, false, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { if (Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true)) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); return false; } } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } }); } public bool Jump(RPoint position, double? LimZ) { if (Plc == null || !Plc.IsConnected) { return false; } try { CanExecute = false; double limz = LimZ == null ? 0 : (double)LimZ; List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.X, RobotInfo.PlcRobotParameter.CommandParameter.Y, RobotInfo.PlcRobotParameter.CommandParameter.Z, RobotInfo.PlcRobotParameter.CommandParameter.U, RobotInfo.PlcRobotParameter.CommandParameter.Limz, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, }; List values = new List() { position.X, position.Y, position.Z, position.U, limz, false, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { if (Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true)) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); return false; } } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } } public async Task JumpAsync(RPoint position, double? LimZ) { if (Plc == null || !Plc.IsConnected) { return false; } return await Task.Run(() => { try { CanExecute = false; double limz = LimZ == null ? 0 : (double)LimZ; List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.X, RobotInfo.PlcRobotParameter.CommandParameter.Y, RobotInfo.PlcRobotParameter.CommandParameter.Z, RobotInfo.PlcRobotParameter.CommandParameter.U, RobotInfo.PlcRobotParameter.CommandParameter.Limz, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, }; List values = new List() { position.X, position.Y, position.Z, position.U, limz, false, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { if (Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true)) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); return false; } } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } }); } public bool Move(RPoint position) { if (Plc == null || !Plc.IsConnected) { return false; } try { CanExecute = false; List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.X, RobotInfo.PlcRobotParameter.CommandParameter.Y, RobotInfo.PlcRobotParameter.CommandParameter.Z, RobotInfo.PlcRobotParameter.CommandParameter.U, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, }; List values = new List() { position.X, position.Y, position.Z, position.U, false, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { if (Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true)) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); return false; } } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } } public async Task MoveAsync(RPoint position) { if (Plc == null || !Plc.IsConnected) { return false; } return await Task.Run(() => { try { CanExecute = false; List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.X, RobotInfo.PlcRobotParameter.CommandParameter.Y, RobotInfo.PlcRobotParameter.CommandParameter.Z, RobotInfo.PlcRobotParameter.CommandParameter.U, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, }; List values = new List() { position.X, position.Y, position.Z, position.U, false, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { if (Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true)) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); return false; } } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } }); } public bool SFree() { return false; } public async Task SFreeAsync() { return false; } public bool SLock() { return false; } public async Task SLockAsync() { return false; } public bool CalibMotion(RPoint position, double? LimZ) { if (Plc == null || !Plc.IsConnected) { return false; } try { CanExecute = false; double limz = LimZ == null ? 0 : (double)LimZ; List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.X, RobotInfo.PlcRobotParameter.CommandParameter.Y, RobotInfo.PlcRobotParameter.CommandParameter.Z, RobotInfo.PlcRobotParameter.CommandParameter.U, RobotInfo.PlcRobotParameter.CommandParameter.Limz, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, }; List values = new List() { position.X, position.Y, position.Z, position.U, limz, false, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { if (Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true)) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); return false; } } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } } public async Task CalibMotionAsync(RPoint position, double? LimZ) { if (Plc == null || !Plc.IsConnected) { return false; } return await Task.Run(() => { try { CanExecute = false; float limz = LimZ == null ? 0f : (float)LimZ; List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.X, RobotInfo.PlcRobotParameter.CommandParameter.Y, RobotInfo.PlcRobotParameter.CommandParameter.Z, RobotInfo.PlcRobotParameter.CommandParameter.U, RobotInfo.PlcRobotParameter.CommandParameter.Limz, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, }; List values = new List() { position.X, position.Y, position.Z, position.U, position.U, false, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { if (Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true)) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); return false; } } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } }); } public bool CalibOutIO(bool state) { if (Plc == null || !Plc.IsConnected) { return false; } try { for (int i = 0; i < 3; i++) { string node = state ? RobotInfo.PlcRobotParameter.CommandParameter.VacuumOn : RobotInfo.PlcRobotParameter.CommandParameter.VacuumOff; if (Plc.OpcUaClient.WriteNode(node, true)) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { string node1 = state ? RobotInfo.PlcRobotParameter.StateParameter.VacuumOn : RobotInfo.PlcRobotParameter.StateParameter.VacuumOff; var data = Plc.OpcUaClient.ReadNode(node1); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); Plc.OpcUaClient.WriteNode(node, false); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); Plc.OpcUaClient.WriteNode(node, false); return false; } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); throw ex; } } public async Task CalibOutIOAsync(bool state) { if (Plc == null || !Plc.IsConnected) { return false; } return await Task.Run(() => { try { for (int i = 0; i < 3; i++) { string node = state ? RobotInfo.PlcRobotParameter.CommandParameter.VacuumOn : RobotInfo.PlcRobotParameter.CommandParameter.VacuumOff; if (Plc.OpcUaClient.WriteNode(node, true)) { Stopwatch sw = new Stopwatch(); sw.Start(); while (true) { string node1 = state ? RobotInfo.PlcRobotParameter.StateParameter.VacuumOn : RobotInfo.PlcRobotParameter.StateParameter.VacuumOff; var data = Plc.OpcUaClient.ReadNode(node1); if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value) { sw.Stop(); Plc.OpcUaClient.WriteNode(node, false); return true; } if (sw.Elapsed.TotalMilliseconds > Timeout) { sw.Stop(); Plc.OpcUaClient.WriteNode(node, false); return false; } } } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); throw ex; } }); } public bool CalibParame(PickInfo Pick, int Speed, int Accel, bool Power, int WaitSuction, int WaitBlow) { if (Plc == null || !Plc.IsConnected) { return false; } try { List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.Speed, RobotInfo.PlcRobotParameter.CommandParameter.Accel, RobotInfo.PlcRobotParameter.CommandParameter.WaitVacuumOn, RobotInfo.PlcRobotParameter.CommandParameter.WaitVacuumOff, }; List values = new List() { Speed, Accel, WaitSuction, WaitBlow, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { return true; } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); throw ex; } } public async Task CalibParameAsync(PickInfo Pick, int Speed, int Accel, bool Power, int WaitSuction, int WaitBlow) { if (Plc == null || !Plc.IsConnected) { return false; } return await Task.Run(() => { try { List nodes = new List() { RobotInfo.PlcRobotParameter.CommandParameter.Speed, RobotInfo.PlcRobotParameter.CommandParameter.Accel, RobotInfo.PlcRobotParameter.CommandParameter.WaitVacuumOn, RobotInfo.PlcRobotParameter.CommandParameter.WaitVacuumOff, }; List values = new List() { Speed, Accel, WaitSuction, WaitBlow, }; for (int i = 0; i < 3; i++) { if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray())) { return true; } } return false; } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); throw ex; } }); } public List GetNodesValue() { if (Plc == null || !Plc.IsConnected) { return new List(); } try { CanExecute = false; List nodes = new List() { RobotInfo.PlcRobotParameter.StateParameter.X, RobotInfo.PlcRobotParameter.StateParameter.Y, RobotInfo.PlcRobotParameter.StateParameter.Z, RobotInfo.PlcRobotParameter.StateParameter.U, RobotInfo.PlcRobotParameter.StateParameter.MoveFinish, RobotInfo.PlcRobotParameter.StateParameter.Error, RobotInfo.PlcRobotParameter.StateParameter.VacuumOn, RobotInfo.PlcRobotParameter.StateParameter.VacuumOff }; List Results = new List(); for (int i = 0; i < 3; i++) { Results = Plc.OpcUaClient.ReadNodes(nodes.ToArray()); if (Results.Count != 0) { return Results; } } return new List(); } catch (Exception ex) { LogHelper.WriteLogError("执行机器人命令时出错!", ex); CanExecute = true; throw ex; } finally { CanExecute = true; } } /// /// 设置工具坐标 /// /// /// /// /// public bool SetTool(int index, double x, double y) { return true; } /// /// 设置工具坐标 /// /// /// /// /// public async Task SetToolAsync(int index, double x, double y) { return true; } /// /// 选择工具坐标 /// /// /// public bool SelectTool(int index) { return true; } /// /// 选择工具坐标 /// /// /// public async Task SelectToolAsync(int index) { return true; } #endregion #region plc触发信号监控 private bool isTrigger = false; public bool IsTrigger { get => isTrigger; set => isTrigger = value; } public Thread CreateThreadToTrigger(NodeId[]Keys, List outputadr) { Thread TriggerTherad = new Thread(() => { List Results = new List(); List ResultsT = new List(); while (IsTrigger) { try { if (Plc.IsConnected) { Results = Plc.OpcUaClient.ReadNodes(Keys); if (ResultsT.Count != Results.Count) { ResultsT.Clear(); for (int i = 0; i < Results.Count; i++) { ResultsT.Add(new DataValue() { Value = Results[i].Value }); if (Results[i].StatusCode.Code != 2150891520) { if ((bool)Results[i].Value) { TrggerChangeEvent?.Invoke(Id, Keys[i], outputadr); } } } } else { for (int i = 0; i < Results.Count; i++) { if (Results[i].StatusCode.Code != 2150891520) { if ((bool)Results[i].Value && (bool)Results[i].Value != (bool)ResultsT[i].Value) { TrggerChangeEvent?.Invoke(Id, Keys[i], outputadr); if (TrggerChangeEvent != null) { Delegate[] aaa = TrggerChangeEvent.GetInvocationList(); } } ResultsT[i].Value = Results[i].Value; } } } } } catch (Exception ex) { Thread.Sleep(5000); } Thread.Sleep(50); } }); TriggerTherad.IsBackground = true; TriggerTherad.Name = Plc.Name + "触发检测" + Keys[0]; TriggerTherad.Start(); return TriggerTherad; } #endregion #region 收发数据 /// /// 发送并接收数据 /// /// /// public string SendAndReceive(string send) { return string.Empty; } /// /// 发送并接收数据 /// /// /// public Task SendAndReceiveAsync(string send) { return Task.FromResult(string.Empty); } /// /// 发送并接收数据 /// /// /// public string SendAndReceive(ITcpSessionClient client, string send) { return string.Empty; } /// /// 发送并接收数据 /// /// /// public Task SendAndReceiveAsync(ITcpSessionClient client, string send) { return Task.FromResult(string.Empty); } public void Send(string send) { } public Task SendAsync(string send) { return Task.CompletedTask; } public void Send(ITcpSessionClient client, string send) { } public Task SendAsync(ITcpSessionClient client, string send) { return Task.CompletedTask; } /// /// 获取编码 /// /// /// public Encoding GetEncoding() { DataEncoding dataEncoding = this.DataEncoding; Encoding encoding; if (dataEncoding == DataEncoding.Default) { encoding = Encoding.Default; } else if (dataEncoding == DataEncoding.ASCII) { encoding = Encoding.ASCII; } else if (dataEncoding == DataEncoding.UTF7) { encoding = Encoding.UTF7; } else if (dataEncoding == DataEncoding.UTF8) { encoding = Encoding.UTF8; } else if (dataEncoding == DataEncoding.UTF32) { encoding = Encoding.UTF32; } else if (dataEncoding == DataEncoding.Unicode) { encoding = Encoding.Unicode; } else if (dataEncoding == DataEncoding.BigEndianUnicode) { encoding = Encoding.BigEndianUnicode; } else if (dataEncoding == DataEncoding.GB2312) { encoding = Encoding.GetEncoding("gb2312"); } else { encoding = Encoding.Default; } return encoding; } #endregion #region 属性通知 /// /// Occurs when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; /// /// Checks if a property already matches a desired value. Sets the property and /// notifies listeners only when necessary. /// /// Type of the property. /// Reference to a property with both getter and setter. /// Desired value for the property. /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers that /// support CallerMemberName. /// True if the value was changed, false if the existing value matched the /// desired value. protected virtual bool SetProperty(ref T storage, T value, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(storage, value)) return false; storage = value; RaisePropertyChanged(propertyName); return true; } /// /// Checks if a property already matches a desired value. Sets the property and /// notifies listeners only when necessary. /// /// Type of the property. /// Reference to a property with both getter and setter. /// Desired value for the property. /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers that /// support CallerMemberName. /// Action that is called after the property value has been changed. /// True if the value was changed, false if the existing value matched the /// desired value. protected virtual bool SetProperty(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null) { if (EqualityComparer.Default.Equals(storage, value)) return false; storage = value; onChanged?.Invoke(); RaisePropertyChanged(propertyName); return true; } /// /// Raises this object's PropertyChanged event. /// /// Name of the property used to notify listeners. This /// value is optional and can be provided automatically when invoked from compilers /// that support . protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } /// /// Raises this object's PropertyChanged event. /// /// The PropertyChangedEventArgs protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { PropertyChanged?.Invoke(this, args); } #endregion } }