整理快捷脚本

This commit is contained in:
tfarcraw 2026-07-05 22:39:50 +08:00
parent 74ab157f06
commit 7d94a8ecb7
14 changed files with 361 additions and 239 deletions

View File

@ -1,4 +1,4 @@
namespace PLCSharp.Core.Common
namespace PLCSharp.Core.Common
{
/// <summary>
/// 流程模型
@ -75,5 +75,7 @@ namespace PLCSharp.Core.Common
return FlowStarTime.AddSeconds(second) < DateTime.Now;
}
public Task<bool> TaskBool { get; set; }
}
}

View File

@ -1,4 +1,4 @@
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Newtonsoft.Json;
using PLCSharp.VVMs.Connects.ModbusTcp;
using PLCSharp.VVMs.Connects.Socket;
@ -70,12 +70,10 @@ namespace PLCSharp.VVMs.Connects
private ProtocolType _Type;
/// <summary>
/// 类型
/// </summary>
[JsonIgnore]
/// <summary>
/// 协议类型
/// </summary>
[JsonIgnore]
public ProtocolType Type
{
get { return _Type; }
@ -111,12 +109,10 @@ namespace PLCSharp.VVMs.Connects
private bool _LogSwitch;
/// <summary>
/// 日志Switch
/// </summary>
[NotMapped]
/// <summary>
/// 日志开关
/// </summary>
[NotMapped]
public bool LogSwitch
{
get { return _LogSwitch; }
@ -125,12 +121,10 @@ namespace PLCSharp.VVMs.Connects
private bool _Connected;
/// <summary>
/// Connected
/// </summary>
[NotMapped]
/// <summary>
/// 连接成功
/// </summary>
[NotMapped]
public bool Connected
{
get { return _Connected; }
@ -139,12 +133,10 @@ namespace PLCSharp.VVMs.Connects
private bool _Successful;
/// <summary>
/// Successful
/// </summary>
[NotMapped]
/// <summary>
/// 读写成功
/// </summary>
[NotMapped]
public bool Successful
{
get { return _Successful; }
@ -154,12 +146,10 @@ namespace PLCSharp.VVMs.Connects
private bool _Online;
/// <summary>
/// Online
/// </summary>
[NotMapped]
/// <summary>
/// 在线
/// </summary>
[NotMapped]
public bool Online
{
get { return _Online; }
@ -167,9 +157,7 @@ namespace PLCSharp.VVMs.Connects
}
private string _ReceiveInfo;
/// <summary>
/// 接收信息
/// </summary>
[NotMapped]
/// <summary>
/// 收到的信息
@ -182,9 +170,7 @@ namespace PLCSharp.VVMs.Connects
private byte[] _ReceiveData;
/// <summary>
/// 接收数据
/// </summary>
[NotMapped]
/// <summary>
/// 收到的数据
@ -195,9 +181,7 @@ namespace PLCSharp.VVMs.Connects
set { SetProperty(ref _ReceiveData, value); }
}
private string _SendInfo;
/// <summary>
/// 发送信息
/// </summary>
[NotMapped]
/// <summary>
/// 要发送的信息
@ -207,6 +191,11 @@ namespace PLCSharp.VVMs.Connects
get { return _SendInfo; }
set { SetProperty(ref _SendInfo, value); }
}
/// <summary>
/// 告警信息
/// </summary>
[NotMapped]
public string ErrInfo { get; set; } = "无异常";
/// <summary>
/// 序列化Params
@ -336,7 +325,7 @@ namespace PLCSharp.VVMs.Connects
if (LogQueue.Count > 1000)
LogQueue.TryDequeue(out _);
RaisePropertyChanged("LogQueue");
RaisePropertyChanged(nameof(LogQueue));
}
}
#endregion
@ -395,11 +384,11 @@ namespace PLCSharp.VVMs.Connects
/// </summary>
/// <param name="msg">msg</param>
/// <returns>返回结果</returns>
public virtual async Task SendAsync(string msg)
public virtual async Task<bool> SendAsync(string msg)
{
if (Params.DataType == CommunicationDataType.String)
{
await SendMsgAsync(msg);
return await SendMsgAsync(msg);
}
else
@ -407,11 +396,14 @@ namespace PLCSharp.VVMs.Connects
if (HexStringToBytes(msg, out List<byte> bytes, out string format))
{
SendInfo = format;
await SendDataAsync([.. bytes]);
return await SendDataAsync([.. bytes]);
}
else
{
Log("请输入正确的十六进制字符串");
ErrInfo = "请输入正确的十六进制字符串";
Log(ErrInfo);
return false;
}
}
@ -423,14 +415,14 @@ namespace PLCSharp.VVMs.Connects
/// <param name="msg">msg</param>
/// <param name="clientName">客户端名称</param>
/// <returns>返回结果</returns>
public virtual async Task SendAsync(string msg, string clientName)
public virtual async Task<bool> SendAsync(string msg, string clientName)
{
if (this is SocketServer server)
{
if (Params.DataType == CommunicationDataType.String)
{
await server.SendMsgAsync(msg, clientName);
return await server.SendMsgAsync(msg, clientName);
}
else
@ -439,20 +431,29 @@ namespace PLCSharp.VVMs.Connects
if (HexStringToBytes(msg, out List<byte> bytes, out string format))
{
SendInfo = format;
await server.SendDataAsync([.. bytes], clientName);
return await server.SendDataAsync([.. bytes], clientName);
}
else
{
Log("请输入正确的十六进制字符串");
ErrInfo = "请输入正确的十六进制字符串";
Log(ErrInfo);
return false;
}
}
}
}
else
{
ErrInfo = "此连接不是服务器";
Log(ErrInfo);
return false;
}
}
/// <summary>
/// 发送HexMsgAsync
/// </summary>
@ -478,6 +479,7 @@ namespace PLCSharp.VVMs.Connects
{
if (string.IsNullOrEmpty(msg))
{
ErrInfo = "消息为空,未发送";
Log($"{DateTime.Now:HH:mm:ss:fff} 消息为空,未发送");
return false;
}

View File

@ -278,33 +278,30 @@ public class EpsonRobot : Robot
}
private bool SendCommand(RobotPoint point)
private void SendCommand(RobotPoint point)
{
try
if (!ClientCtrl.Connected)
{
ClientCommand.ReceiveInfo = "";
if (!point.Safe)
{
RcvShowInfo = "不安全,请检查点位";
return false;
}
Status[12] = false;
if (!ClientCommand.Connected) return false;
var speedPercent = (int)(point.Rate * Speed / 100);
var sendMsg =
$"@{point.Command} {speedPercent} {point.X} {point.Y} {point.Z} {point.U} {point.V} {point.W},{point.Hand}\r\n";
ClientCommand.SendMsg(sendMsg);
return true;
throw new Exception($"机器人未连接");
}
catch (Exception ex)
if (!ClientCommand.Connected)
{
System.Diagnostics.Debug.WriteLine($"SendCommand 失败: {ex.Message}");
return false;
throw new Exception($"机器人未启动");
}
ClientCommand.ReceiveInfo = "";
if (!point.Safe)
{
throw new Exception($"机器人点位{point.Name}不安全");
}
Status[12] = false;
var speedPercent = (int)(point.Rate * Speed / 100);
var sendMsg =
$"@{point.Command} {speedPercent} {point.X} {point.Y} {point.Z} {point.U} {point.V} {point.W},{point.Hand}\r\n";
ClientCommand.SendMsg(sendMsg);
}
/// <summary>
@ -312,17 +309,13 @@ public class EpsonRobot : Robot
/// </summary>
/// <param name="point">点位</param>
/// <returns>返回布尔值</returns>
public override bool RunPoint(string pointName)
public override void RunPoint(string pointName)
{
PointDone = false;
var point = Points.FirstOrDefault(p => p.Name == pointName);
if (point == null)
{
return false;
}
var point = Points.FirstOrDefault(p => p.Name == pointName) ?? throw new Exception($"机器人点位{pointName}不存在");
point.Command = "RunPoint";
PointCommand = point.DeepCopy();
return SendCommand(PointCommand);
SendCommand(PointCommand);
}
/// <summary>
@ -333,15 +326,11 @@ public class EpsonRobot : Robot
/// <param name="dist"></param>
/// <param name="rate"></param>
/// <returns></returns>
public override bool Jog(string pointName, string cmd, double dist, double rate = 0)
public override void Jog(string pointName, string cmd, double dist, double rate = 0)
{
PointDone = false;
var cmdDone = false;
var point = Points.FirstOrDefault(p => p.Name == pointName);
if (point == null)
{
return false;
}
var point = Points.FirstOrDefault(p => p.Name == pointName) ?? throw new Exception($"机器人点位{pointName}不存在");
PointCommand = CurrPoint.DeepCopy();
@ -353,29 +342,29 @@ public class EpsonRobot : Robot
case "X-":
PointCommand.Command = cmd;
PointCommand.X = dist;
PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand);
PointCommand.Rate = rate > 0 ? rate : point.Rate;
SendCommand(PointCommand);
break;
case "Y+":
case "Y-":
PointCommand.Command = cmd;
PointCommand.Y = dist;
PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand);
SendCommand(PointCommand);
break;
case "Z+":
case "Z-":
PointCommand.Command = cmd;
PointCommand.Z = dist;
PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand);
SendCommand(PointCommand);
break;
case "U+":
case "U-":
PointCommand.Command = cmd;
PointCommand.U = dist;
PointCommand.Rate = rate > 0 ? rate : point.Rate;
cmdDone = SendCommand(PointCommand);
SendCommand(PointCommand);
break;
}
}
@ -386,24 +375,21 @@ public class EpsonRobot : Robot
var tool = Tools.FirstOrDefault(w => w.Key == point.ToolNum);
if (tool.Value == null) return false;
if (tool.Value == null) throw new Exception("工具坐标配置");
var pointTool = ToolFrame4Axis.WorldToTool(PointCommand, tool.Value);
switch (cmd)
{
case "X+":
pointTool.X += dist;
break;
case "X-":
pointTool.X -= dist;
break;
case "Y+":
pointTool.Y += dist;
break;
case "Y-":
pointTool.Y -= dist;
break;
case "Z+":
@ -422,10 +408,9 @@ public class EpsonRobot : Robot
}
var pointWord = ToolFrame4Axis.ToolToWorld(pointTool, tool.Value);
cmdDone = SendCommand(pointWord);
SendCommand(pointWord);
}
return cmdDone;
}
/// <summary>

View File

@ -415,9 +415,9 @@ namespace PLCSharp.VVMs.Robots
/// </summary>
/// <param name="robotPoint">机器人点位</param>
/// <returns>返回布尔值</returns>
public virtual bool RunPoint(string pointName)
public virtual void RunPoint(string pointName)
{
return false;
}
/// <summary>
@ -428,11 +428,9 @@ namespace PLCSharp.VVMs.Robots
/// <param name="dist">dist</param>
/// <param name="rate">rate</param>
/// <returns>返回布尔值</returns>
public virtual bool Jog(string pointName, string cmd, double dist, double rate = 0)
public virtual void Jog(string pointName, string cmd, double dist, double rate = 0)
{
return false;
}
/// <summary>

View File

@ -296,40 +296,48 @@ namespace PLCSharp.VVMs.Robots
void ExecuteRobotPointCommand(object cmd)
{
var cmdStr = cmd as string;
if (SelectedRobot.SelectedPoint == null)
try
{
SendInfoDialog("请选择点位");
return;
}
SelectedRobot.SelectedPoint.Safe = true;
switch (cmdStr)
var cmdStr = cmd as string;
if (SelectedRobot.SelectedPoint == null)
{
SendInfoDialog("请选择点位");
return;
}
SelectedRobot.SelectedPoint.Safe = true;
switch (cmdStr)
{
case "RunPoint":
SelectedRobot.RunPoint(SelectedRobot.SelectedPoint.Name);
break;
case "X+":
case "X-":
case "Y+":
case "Y-":
case "Z+":
case "Z-":
case "U+":
case "U-":
SelectedRobot.Jog(SelectedRobot.SelectedPoint.Name, cmdStr, RelativeDistance);
break;
}
}
catch (Exception ex)
{
case "RunPoint":
SelectedRobot.RunPoint(SelectedRobot.SelectedPoint.Name);
break;
case "X+":
case "X-":
case "Y+":
case "Y-":
case "Z+":
case "Z-":
case "U+":
case "U-":
SelectedRobot.Jog(SelectedRobot.SelectedPoint.Name, cmdStr, RelativeDistance);
break;
SendErrDialog(ex.Message);
}
}

View File

@ -31,16 +31,9 @@ namespace PLCSharp.VVMs.Workflows
//视觉功能
case 0:
CurrVF = globalModel.GetVisionFunction("视觉功能名");
if (CurrVF != null)
{
CurrVF.Flow.Reset();
flow.Step++;
}
else
{
CurrVF.Flow.Reset();
flow.Step++;
throw new Exception("视觉功能名称错误!");
}
break;
case 1:
@ -49,71 +42,43 @@ namespace PLCSharp.VVMs.Workflows
flow.Step++;
}
else if (flow.CheckStepTime(3))
{
flow.Step--;
throw new Exception("视觉功能超时!");
}
break;
//全局变量
case 2:
CurrVariable = globalModel.GetVariable("变量名");
if (CurrVariable != null)
{
CurrVariable.Value = 100; // 设置变量值
flow.Step++;
}
else
{
throw new Exception("机器人名称错误!");
}
CurrVariable.Value = 100; // 设置变量值
flow.Step++;
break;
//机器人绝对运动
case 3:
CurrRobot = globalModel.GetRobot("机器人名称");
if (CurrRobot != null)
{
flow.Step++;
}
else
{
throw new Exception("机器人名称错误!");
}
flow.Step++;
break;
case 4:
if (CurrRobot.RunPoint("点位名称"))
{
flow.Step++;
}
else
{
throw new Exception("机器人点位运行失败!");
}
CurrRobot.RunPoint("点位名称");
flow.Step++;
break;
case 5:
if (CurrRobot.PointDone) // 等待运动完成
{
flow.Step++;
}
else if (flow.CheckStepTime(3))
else if (flow.CheckStepTime(3)) // 超时处理
{
flow.Step--;
throw new Exception("机器人点位运动超时!");
}
break;
//机器人相对运动
case 6:
if (CurrRobot.Jog("点位名称", "X+", 10))
{
flow.Step++;
}
else
{
throw new Exception("机器人点位运行失败!");
}
CurrRobot.Jog("点位名称", "X+", 10);
flow.Step++;
break;
@ -123,7 +88,10 @@ namespace PLCSharp.VVMs.Workflows
flow.Step++;
}
else if (flow.CheckStepTime(3))
{
flow.Step--;
throw new Exception("机器人点位运动超时!");
}
break;
case 8:
//显示状态
@ -133,10 +101,87 @@ namespace PLCSharp.VVMs.Workflows
CurrControl.Params.CellInfos[0].State = CellState.完成;
flow.Step++;
}
else {
else
{
throw new Exception("控件名称错误!");
}
break;
//网络通信 SocketServer广播 /SocketClient / FreeSerialProtocol
case 9:
CurrConnect = globalModel.GetConnect("网络连接名");
flow.TaskBool = CurrConnect.SendAsync("要发送的消息");
flow.Step++;
break;
case 10:
if (flow.TaskBool.IsCompleted)
{
if (flow.TaskBool.Result)
{
if (string.IsNullOrEmpty(CurrConnect.ReceiveInfo))
{
//这里处理 接收到的消息 CurrConnect.ReceiveInfo
flow.Step++;
}
else if (flow.CheckStepTime(3)) // 如果不需要判断超时,可以不写这个判断
{
flow.Step--;
throw new Exception("回复超时!");
}
}
else
{
flow.Step--;
throw new Exception($"发送失败!{CurrConnect.ErrInfo}");
}
}
else if (flow.CheckStepTime(3))
{
flow.Step--;
throw new Exception("发送超时!");
}
break;
//网络通信 SocketServer 向指定客户端发送消息
case 11:
CurrConnect = globalModel.GetConnect("网络连接名");
flow.TaskBool = CurrConnect.SendAsync("要发送的消息", "客户端名称");
flow.Step++;
break;
case 12:
if (flow.TaskBool.IsCompleted)
{
if (flow.TaskBool.Result)
{
if (string.IsNullOrEmpty(CurrConnect.ReceiveInfo))
{
//这里处理 接收到的消息 CurrConnect.ReceiveInfo
flow.Step++;
}
else if (flow.CheckStepTime(3)) // 如果不需要判断超时,可以不写这个判断
{
flow.Step--;
throw new Exception("回复超时!");
}
}
else
{
flow.Step--;
throw new Exception($"发送失败!{CurrConnect.ErrInfo}");
}
}
else if (flow.CheckStepTime(3))
{
flow.Step--;
throw new Exception("发送超时!");
}
break;
}
}

View File

@ -0,0 +1,6 @@
//全局变量
case 2:
CurrVariable = globalModel.GetVariable("变量名");//如果前面已经获取,这里可以省略
CurrVariable.Value = 100; // 设置变量值
flow.Step++;
break;

View File

@ -1,7 +0,0 @@
 case 0:
{
CurrVariable = globalModel.GetVariable("变量名");
CurrVariable.Value = 100; // 设置变量值
flow.Step++;
}
break;

View File

@ -0,0 +1,24 @@
//机器人相对运动
case 4:
CurrRobot = globalModel.GetRobot("机器人名称"); //如果前面已经获取过机器人对象,这里可以省略
flow.Step++;
break;
case 6:
CurrRobot.Jog("点位名称", "X+", 10);
flow.Step++;
break;
case 7:
if (CurrRobot.PointDone) // 等待运动完成
{
flow.Step++;
}
else if (flow.CheckStepTime(3))
{
flow.Step--;
throw new Exception("机器人点位运动超时!");
}
break;

View File

@ -0,0 +1,21 @@
//机器人绝对运动
case 3:
CurrRobot = globalModel.GetRobot("机器人名称");//如果前面已经获取过机器人对象,这里可以省略
flow.Step++;
break;
case 4:
CurrRobot.RunPoint("点位名称");
flow.Step++;
break;
case 5:
if (CurrRobot.PointDone) // 等待运动完成
{
flow.Step++;
}
else if (flow.CheckStepTime(3)) // 超时处理
{
flow.Step--;
throw new Exception("机器人点位运动超时!");
}
break;

View File

@ -1,20 +0,0 @@
case 0:
{
var robot = globalModel.GetRobot("机器人名称");
if (robot != null)
{
var point = robot.Points.FirstOrDefault(p => p.Name == "点位名称");
if (point != null)
{
robot.RunPoint(point);
flow.Step++;
}
}
}
break;
case 1:
if (robot.) // 等待运动完成
{
flow.Step++;
}
break;

View File

@ -0,0 +1,37 @@
 //网络通信 SocketServer 向指定客户端发送消息
case 11:
CurrConnect = globalModel.GetConnect("网络连接名");
flow.TaskBool = CurrConnect.SendAsync("要发送的消息", "客户端名称");
flow.Step++;
break;
case 12:
if (flow.TaskBool.IsCompleted)
{
if (flow.TaskBool.Result)
{
if (string.IsNullOrEmpty(CurrConnect.ReceiveInfo))
{
//这里处理 接收到的消息 CurrConnect.ReceiveInfo
flow.Step++;
}
else if (flow.CheckStepTime(3)) // 如果不需要判断超时,可以不写这个判断
{
flow.Step--;
throw new Exception("回复超时!");
}
}
else
{
flow.Step--;
throw new Exception($"发送失败!{CurrConnect.ErrInfo}");
}
}
else if (flow.CheckStepTime(3))
{
flow.Step--;
throw new Exception("发送超时!");
}
break;

View File

@ -1,17 +1,38 @@
case 0:
CurrConnect = globalModel.GetConnect("网络连接名");
_ = CurrConnect.SendAsync("要发送的消息");
flow.Step++;
break;
case 1:
if (string.IsNullOrEmpty(CurrConnect.ReceiveInfo))
{
flow.Step++;
//网络通信 SocketServer广播 /SocketClient / FreeSerialProtocol
case 9:
}
break;
CurrConnect = globalModel.GetConnect("网络连接名");
flow.TaskBool = CurrConnect.SendAsync("要发送的消息");
flow.Step++;
break;
case 10:
if (flow.TaskBool.IsCompleted)
{
if (flow.TaskBool.Result)
{
if (string.IsNullOrEmpty(CurrConnect.ReceiveInfo))
{
//这里处理 接收到的消息 CurrConnect.ReceiveInfo
flow.Step++;
}
else if (flow.CheckStepTime(3)) // 如果不需要判断超时,可以不写这个判断
{
flow.Step--;
throw new Exception("回复超时!");
}
}
else
{
flow.Step--;
throw new Exception($"发送失败!{CurrConnect.ErrInfo}");
}
}
else if (flow.CheckStepTime(3))
{
flow.Step--;
throw new Exception("发送超时!");
}
break;

View File

@ -1,19 +1,19 @@
case 0:
CurrVF= globalModel.GetVisionFunction("视觉功能名");
CurrVF.Flow.Reset();
flow.Step++;
break;
case 1:
{
if(CurrVF.RunAll(CurrVF.Flow))
{
flow.Step++;
 //视觉功能
case 0:
CurrVF = globalModel.GetVisionFunction("视觉功能名");//如果前面已经获取,这里可以省略
CurrVF.Flow.Reset();
flow.Step++;
}else if (flow.CheckStepTime(3))
throw new Exception("视觉功能超时!");
}
}
break;
break;
case 1:
if (CurrVF.RunAll(CurrVF.Flow))
{
flow.Step++;
}
else if (flow.CheckStepTime(3))
{
flow.Step--;
throw new Exception("视觉功能超时!");
}
break;