2065 lines
67 KiB
C#
2065 lines
67 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Model;
|
||
using System.IO;
|
||
using System.Windows;
|
||
using Views;
|
||
using Community;
|
||
using System.Threading;
|
||
using System.Net.Sockets;
|
||
using System.Windows.Controls;
|
||
using System.Xml;
|
||
using System.Xml.Linq;
|
||
using System.Xml.Serialization;
|
||
using System.Text.RegularExpressions;
|
||
|
||
namespace ViewModel.Robots
|
||
{
|
||
/// <summary>
|
||
/// 图灵机械手
|
||
/// </summary>
|
||
public class TurinRobotVM : FeiYuRobotVM
|
||
{
|
||
/// <summary>
|
||
/// 上报异常的委托事件
|
||
/// </summary>
|
||
public override event delegeateExcep eventExcepReport;
|
||
/// <summary>
|
||
/// 刷新这列点位
|
||
/// </summary>
|
||
public override event ShuaxinPoint ShuaxinPointing;
|
||
|
||
|
||
|
||
#region 机器人控制器返回信息
|
||
private string connectionStatus;
|
||
/// <summary>
|
||
/// 机器人控制器返回信息
|
||
/// </summary>
|
||
public string ConnectionStatus
|
||
{
|
||
get { return connectionStatus; }
|
||
set
|
||
{
|
||
connectionStatus = value;
|
||
this.RaisePropertyChanged("ConnectionStatus");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 机器人连接
|
||
private bool robotConn;
|
||
/// <summary>
|
||
/// 机器人连接
|
||
/// </summary>
|
||
public bool RobotConn
|
||
{
|
||
get { return robotConn; }
|
||
set
|
||
{
|
||
robotConn = value;
|
||
|
||
if (value)
|
||
{
|
||
SendControl("$Login\r\n");
|
||
|
||
}
|
||
else
|
||
{
|
||
if (RobotStart)
|
||
{
|
||
ServoOn = false;
|
||
RobotStart = false;
|
||
}
|
||
SendControl("$Logout\r\n");
|
||
}
|
||
this.RaisePropertyChanged("RobotConn");
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 启动机器人开关
|
||
private bool robotStart;
|
||
/// <summary>
|
||
/// 启动机器人开关
|
||
/// </summary>
|
||
public bool RobotStart
|
||
{
|
||
get { return robotStart; }
|
||
set
|
||
{
|
||
robotStart = value;
|
||
if (MyThread != null)
|
||
{
|
||
if (value)
|
||
{
|
||
SendControl("$Start,0\r\n");
|
||
}
|
||
else
|
||
{
|
||
SendControl("$Stop\r\n");
|
||
}
|
||
}
|
||
|
||
this.RaisePropertyChanged("RobotStart");
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 使能开关
|
||
private bool servoOn;
|
||
/// <summary>
|
||
/// 使能开关
|
||
/// </summary>
|
||
public bool ServoOn
|
||
{
|
||
get { return servoOn; }
|
||
set
|
||
{
|
||
servoOn = value;
|
||
this.RaisePropertyChanged("ServoOn");
|
||
if (RobotConn)
|
||
{
|
||
if (value)
|
||
{
|
||
SendControl("$SetMotorsOn,0\r\n");
|
||
ConnectionStatus = "开启工作模式";
|
||
}
|
||
else
|
||
{
|
||
SendControl("$SetMotorsOff,0\r\n");
|
||
ConnectionStatus = "关闭工作模式";
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 机器人重置
|
||
/// <summary>
|
||
/// 机器人重置
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public override void Reset(object obj)
|
||
{
|
||
//SendControl("$Reset\r\n");
|
||
ClearRobotError();//清楚报警
|
||
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region 构造函数
|
||
/// <summary>
|
||
/// 构造函数
|
||
/// </summary>
|
||
public TurinRobotVM(MachineModel machine)
|
||
{
|
||
MyWindow = new TurinRobotWind();
|
||
MyWindow.DataContext = this;
|
||
MachineObj = machine;
|
||
ResetCommand = new DelegateCommand();
|
||
ResetCommand.ExecuteAction = new Action<object>(Reset);
|
||
|
||
RunSelectedRobotPointCommand = new DelegateCommand();
|
||
RunSelectedRobotPointCommand.ExecuteAction = new Action<object>(RunSelectedRobotPointPoint);
|
||
|
||
EditorRobotPointCommand = new DelegateCommand();
|
||
EditorRobotPointCommand.ExecuteAction = new Action<object>(EditorRobotPoint);
|
||
|
||
SaveRobotPointCommand = new DelegateCommand();
|
||
SaveRobotPointCommand.ExecuteAction = new Action<object>(SaveRobotPoint);
|
||
|
||
UpToolCoordinateCommand = new DelegateCommand();
|
||
UpToolCoordinateCommand.ExecuteAction = new Action<object>(UpToolCoordinate);
|
||
|
||
RobotPGMoveCommand = new DelegateCommand();
|
||
RobotPGMoveCommand.ExecuteAction = new Action<object>(RobotPGMove);
|
||
|
||
GoHomeCommand = new DelegateCommand();
|
||
GoHomeCommand.ExecuteAction = new Action<object>(GoHome);
|
||
|
||
SaveLaserCommand = new DelegateCommand();
|
||
SaveLaserCommand.ExecuteAction = new Action<object>(SaveLaser);
|
||
|
||
RobotPer = dal.GetRobot()[0];
|
||
SpeedS = RobotPer.SpeedS;
|
||
ControllerConn = new RobotSocket(RobotPer.IpStr, RobotPer.Port);
|
||
CommandInteraction = new RobotSocket(RobotPer.IpStr, RobotPer.Port1);
|
||
|
||
RobotConn = false;
|
||
ServoOn = false;
|
||
RobotStart = false;
|
||
|
||
EditingEnabled = false;
|
||
|
||
TeachShow = new ShowInfo();
|
||
AutoShow = new ShowInfo();
|
||
WarningShow = new ShowInfo();
|
||
SErrorShow = new ShowInfo();
|
||
SafeDoorShow = new ShowInfo();
|
||
EStopShow = new ShowInfo();
|
||
ErrorShow = new ShowInfo();
|
||
PausedShow = new ShowInfo();
|
||
RunningShow = new ShowInfo();
|
||
ReadyShow = new ShowInfo();
|
||
|
||
|
||
//初始化命令参数
|
||
CommandModel = new RobotPoint();
|
||
|
||
|
||
MyThread = new Thread(ReceivesRunState);
|
||
MyThread.IsBackground = true;
|
||
MyThread.Start();
|
||
|
||
MyThread1 = new Thread(ReceivesRemoteState);
|
||
MyThread1.IsBackground = true;
|
||
MyThread1.Start();
|
||
|
||
|
||
HomeStep = GengralProcessEnum.Start;
|
||
CommonList = new List<CommonClass>() {
|
||
new CommonClass() { ID=1,CommonName="NG料盘2"},
|
||
new CommonClass() { ID=2,CommonName="料盘2"},
|
||
new CommonClass() { ID=3,CommonName="料盘1"},
|
||
new CommonClass() { ID=4,CommonName="NG料盘1"},
|
||
new CommonClass() { ID=5,CommonName="NG料盘4"},
|
||
new CommonClass() { ID=6,CommonName="料盘4"},
|
||
new CommonClass() { ID=7,CommonName="料盘3"},
|
||
new CommonClass() { ID=8,CommonName="NG料盘3"},
|
||
new CommonClass() { ID=9,CommonName="测试站"},
|
||
new CommonClass() { ID=0,CommonName="公共点位"},
|
||
|
||
};
|
||
|
||
|
||
SelectStationName = CommonList.Where(w => w.ID == 0).FirstOrDefault().CommonName;
|
||
|
||
}
|
||
|
||
|
||
public override void SaveLaser(object obj)
|
||
{
|
||
if (dal.UpdateRobotPointSpareField(SelectedRobotPoint) > 0)
|
||
{
|
||
MyMessageBox.Show("保存成功");
|
||
}
|
||
else
|
||
{
|
||
MyMessageBox.Show("保存失败");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 机器人控制器
|
||
|
||
#region 连接控制器并接收控制器状态信息
|
||
bool jiaohuan { get; set; }
|
||
/// <summary>
|
||
/// 连接控制器并接收控制器状态信息
|
||
/// </summary>
|
||
private void ReceivesRunState()
|
||
{
|
||
while (true)
|
||
{
|
||
try
|
||
{
|
||
ControllerConn.StartConn(); //开启控制器连接
|
||
}
|
||
catch (Exception)
|
||
{
|
||
ControllerConn.ConnStatu.StatusBackground = ColorStr.Red;
|
||
ControllerConn.ConnStatu.StatuInfo = "机器人连接失败";
|
||
ControllerConn.ConnStatu.StatusForeground = ColorStr.White;
|
||
ControllerConn.ShutDown(); //关闭连接
|
||
|
||
continue; //跳出本次循环
|
||
}
|
||
ControllerConn.ConnStatu.StatusBackground = ColorStr.Green;
|
||
ControllerConn.ConnStatu.StatuInfo = "连接正常";
|
||
ControllerConn.ConnStatu.StatusForeground = ColorStr.White;
|
||
|
||
while (true)
|
||
{
|
||
|
||
try
|
||
{
|
||
if (jiaohuan)
|
||
{
|
||
GetRobotState(); //询问控制器状态
|
||
jiaohuan = false;
|
||
}
|
||
else
|
||
{
|
||
GetCurrAllPos();//获取坐标
|
||
jiaohuan = true;
|
||
|
||
}
|
||
|
||
StateAnalytical(ControllerConn.ReceivesMessageB());
|
||
|
||
}
|
||
catch (SocketException)
|
||
{
|
||
//eventExcepReport("机器人控制连接异常:" + ex.Message);//抛出异常
|
||
|
||
ControllerConn.ConnStatu.StatuInfo = "连接异常";
|
||
ControllerConn.ConnStatu.StatusBackground = ColorStr.Yellow;
|
||
ControllerConn.ConnStatu.StatusForeground = ColorStr.Black;
|
||
|
||
ControllerConn.ShutDown(); //关闭连接
|
||
|
||
break; //跳出当前循环
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
eventExcepReport("机器人控制连接异常:" + ex.Message);//抛出异常
|
||
ControllerConn.ConnStatu.StatuInfo = "机器人控制连接异常";
|
||
ControllerConn.ConnStatu.StatusBackground = ColorStr.Yellow;
|
||
ControllerConn.ConnStatu.StatusForeground = ColorStr.Black;
|
||
ControllerConn.ShutDown(); //关闭连接
|
||
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 机器人控制信息分析
|
||
/// <summary>
|
||
/// 接收到的控制状态
|
||
/// </summary>
|
||
TurinCommandModel ControlCmd { get; set; }
|
||
/// <summary>
|
||
/// 机器人控制信息分析
|
||
/// </summary>
|
||
public void StateAnalytical(string RunStateMsg)
|
||
{
|
||
if (string.IsNullOrEmpty(RunStateMsg))
|
||
{
|
||
return;
|
||
}
|
||
ControlCmd = ControlReadXml(RunStateMsg);
|
||
|
||
if (ControlCmd.Data == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
switch (ControlCmd.Name)
|
||
{
|
||
case "GetCurrAllPos":
|
||
RobotCurPos = GetPoint(ControlCmd.Data);
|
||
// GetCurrAllPos();//获取机械手点位
|
||
break;
|
||
case "GetSystemRunningStatus": //获取运行状态
|
||
ConnectionStatus = JiexiState(ControlCmd.Data);
|
||
break;
|
||
case "ClearRobotError"://重置报警
|
||
if (ControlCmd.Data.Contains("OK"))
|
||
{
|
||
ConnectionStatus = "异常清除成功";
|
||
}
|
||
else
|
||
{
|
||
ConnectionStatus = "异常清除失败" + ControlCmd.Data;
|
||
}
|
||
break;
|
||
default:
|
||
ConnectionStatus = ControlCmd.Data;//RcvStr[0].ToLower().ToString();
|
||
break;
|
||
}
|
||
|
||
RunStateMsg = null;
|
||
}
|
||
#endregion
|
||
#endregion
|
||
|
||
#region 指令交互
|
||
#region 接收远程控制状态信息
|
||
/// <summary>
|
||
/// 接收机器人状态信息
|
||
/// </summary>
|
||
private void ReceivesRemoteState()
|
||
{
|
||
|
||
while (true)
|
||
{
|
||
try
|
||
{
|
||
CommandInteraction.StartConn(); //开启连接
|
||
LoginRobot();//登录
|
||
}
|
||
catch (Exception)
|
||
{
|
||
CommandInteraction.ConnStatu.StatusBackground = ColorStr.Red;
|
||
CommandInteraction.ConnStatu.StatuInfo = "未连接";
|
||
CommandInteraction.ConnStatu.StatusForeground = ColorStr.White;
|
||
CommandInteraction.ShutDown();//关闭连接
|
||
continue;
|
||
}
|
||
CommandInteraction.ConnStatu.StatusBackground = ColorStr.Green;
|
||
CommandInteraction.ConnStatu.StatuInfo = "连接正常";
|
||
CommandInteraction.ConnStatu.StatusForeground = ColorStr.White;
|
||
|
||
|
||
while (true)
|
||
{
|
||
try
|
||
{
|
||
RunStateAnalytical(CommandInteraction.ReceivesMessageB());
|
||
}
|
||
catch (SocketException)
|
||
{
|
||
CommandInteraction.ConnStatu.StatuInfo = "连接异常";
|
||
CommandInteraction.ConnStatu.StatusBackground = ColorStr.Red;
|
||
CommandInteraction.ConnStatu.StatusForeground = ColorStr.White;
|
||
CommandInteraction.ShutDown();//关闭连接
|
||
|
||
break;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
eventExcepReport("机器人控制连接异常" + ex.Message);//抛出异常
|
||
CommandInteraction.ConnStatu.StatuInfo = "远程控制消息接收失败";
|
||
CommandInteraction.ConnStatu.StatusBackground = ColorStr.Red;
|
||
CommandInteraction.ConnStatu.StatusForeground = ColorStr.White;
|
||
CommandInteraction.ShutDown();//关闭连接
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 机器人运行状态解析
|
||
TurinCommandModel cmd;
|
||
string RcvStr { get; set; }
|
||
/// <summary>
|
||
/// 机器人运行状态解析
|
||
/// </summary>
|
||
public void RunStateAnalytical(string RemoteStateMsg)
|
||
{
|
||
if (string.IsNullOrEmpty(RemoteStateMsg))
|
||
{
|
||
return;
|
||
}
|
||
RcvStr = RemoteStateMsg;
|
||
cmd = ReadXml(RemoteStateMsg);
|
||
|
||
if (cmd.Data == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
switch (cmd.Name)
|
||
{
|
||
case "Login":
|
||
RcvShowInfo = "登录成功";
|
||
break;
|
||
case "movetohome":
|
||
RcvShowInfo = "回原点成功";
|
||
break;
|
||
case "GetCurrAllPos":
|
||
RobotCurPos = GetPoint(cmd.Data);
|
||
// GetCurrAllPos();//获取机械手点位
|
||
break;
|
||
case "MotionStart": //单轴运行OK
|
||
RcvShowInfo = cmd.Name;
|
||
break;
|
||
default:
|
||
RcvShowInfo = cmd.Data;//RcvStr[0].ToLower().ToString();
|
||
|
||
break;
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
#endregion
|
||
#endregion
|
||
|
||
#region 发送控制命令
|
||
/// <summary>
|
||
/// 发送控制命令给机器人
|
||
/// </summary>
|
||
public void SendControl(string strMgs)
|
||
{
|
||
if (ControllerConn != null)
|
||
{
|
||
ControllerConn.Send(strMgs);
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 发送运行命令
|
||
/// <summary>
|
||
/// 发送运行点位
|
||
/// </summary>
|
||
/// <param name="RP"></param>
|
||
/// <param name="Model">运行模式 1:关节 2:直线</param>
|
||
/// <returns></returns>
|
||
public bool SendCommand(RobotPoint RP, int Model = 1)
|
||
{
|
||
try
|
||
{
|
||
string RunModel = "";
|
||
switch (Model)
|
||
{
|
||
case 1:
|
||
RunModel = "MoveJ";
|
||
MotionStart(RP, RunModel);
|
||
break;
|
||
case 2:
|
||
RunModel = "MoveL";
|
||
MotionStart(RP, RunModel);
|
||
break;
|
||
}
|
||
//记录移动日志
|
||
// RecordLog("(PC→Robot)" + RP.PointName + "【" + sendMsg + "】");
|
||
|
||
return true;
|
||
}
|
||
catch
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送交互命令
|
||
/// </summary>
|
||
/// <param name="sendstr"></param>
|
||
/// <returns></returns>
|
||
public void SendCommand(string sendstr)
|
||
{
|
||
lock (this)
|
||
{
|
||
try
|
||
{
|
||
if (!string.IsNullOrEmpty(sendstr))
|
||
{
|
||
RcvStr = "";
|
||
RcvShowInfo = "";
|
||
CommandInteraction.Send(sendstr);
|
||
|
||
RecordLog("(PC→Robot)" + sendstr);
|
||
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
RcvShowInfo = "发送命令失败" + ex.Message;
|
||
}
|
||
|
||
|
||
}
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region 机器人点位编辑相关
|
||
|
||
|
||
#region 机器人PG移动方法
|
||
/// <summary>
|
||
/// 机器人PG移动方法
|
||
/// </summary>
|
||
public override void RobotMove(string direction)
|
||
{
|
||
CommandModel = new RobotPoint();
|
||
switch (direction)
|
||
{
|
||
case "+X":
|
||
PGRun("X", RobotTuningDis);
|
||
break;
|
||
case "-X":
|
||
PGRun("X", -RobotTuningDis);
|
||
break;
|
||
case "+Y":
|
||
PGRun("Y", RobotTuningDis);
|
||
break;
|
||
case "-Y":
|
||
PGRun("Y", -RobotTuningDis);
|
||
break;
|
||
case "+Z":
|
||
PGRun("Z", RobotTuningDis);
|
||
break;
|
||
case "-Z":
|
||
PGRun("Z", -RobotTuningDis);
|
||
break;
|
||
case "+U":
|
||
PGRun("U", RobotTuningDis);
|
||
break;
|
||
case "-U":
|
||
PGRun("U", -RobotTuningDis);
|
||
break;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 鼠标点击PG移动
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public override void RobotPGMove(object obj)
|
||
{
|
||
try
|
||
{
|
||
if (RunningShow.StatusBackground != ColorStr.Green)
|
||
{
|
||
Button btn = obj as Button;
|
||
RobotTuningDis = Convert.ToDouble(btn.ToolTip);
|
||
RobotMove(btn.Tag.ToString());
|
||
}
|
||
else
|
||
{
|
||
//MyMessageBox.Show("机器运行中,无法寸动");
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
}
|
||
}
|
||
|
||
|
||
#region 编辑点位
|
||
/// <summary>
|
||
/// 编辑点位
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public override void EditorRobotPoint(object obj)
|
||
{
|
||
if (SelectedRobotPoint != null && SelectedRobotPoint.ID > 0)
|
||
{
|
||
EditingEnabled = true;
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("请选择要编辑的点位");
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 保存机器人点位
|
||
/// <summary>
|
||
/// 保存机器人点位
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public override void SaveRobotPoint(object obj)
|
||
{
|
||
RobotCurPos = null;
|
||
//GetCurrAllPos();
|
||
if (MessageBox.Show("请确定要保存当前点位吗?", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
|
||
{
|
||
if (RobotCurPos != null)
|
||
{
|
||
try
|
||
{
|
||
SelectedRobotPoint.PointX = RobotCurPos.PointX;
|
||
SelectedRobotPoint.PointY = RobotCurPos.PointY;
|
||
SelectedRobotPoint.PointZ = RobotCurPos.PointZ;
|
||
SelectedRobotPoint.PointU = RobotCurPos.PointU;
|
||
SelectedRobotPoint.PointV = RobotCurPos.PointV;
|
||
SelectedRobotPoint.PointW = RobotCurPos.PointW;
|
||
|
||
SelectedRobotPoint.J1 = RobotCurPos.J1;
|
||
SelectedRobotPoint.J2 = RobotCurPos.J2;
|
||
SelectedRobotPoint.J3 = RobotCurPos.J3;
|
||
SelectedRobotPoint.J4 = RobotCurPos.J4;
|
||
SelectedRobotPoint.J5 = RobotCurPos.J5;
|
||
SelectedRobotPoint.J6 = RobotCurPos.J6;
|
||
SelectedRobotPoint.J7 = RobotCurPos.J7;
|
||
SelectedRobotPoint.J8 = RobotCurPos.J8;
|
||
SelectedRobotPoint.J9 = RobotCurPos.J9;
|
||
|
||
ShuaxinPointing(SelectedRobotPoint.PointClass);//刷新矩阵点位
|
||
|
||
if (dal.UpdateRobotPoint(SelectedRobotPoint) > 0)
|
||
{
|
||
MessageBox.Show("保存成功");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("保存失败");
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
MessageBox.Show("保存失败\r\n" + ex.Message);
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("机器人点位保存失败,未能获取机器人的当前点位信息,请重试");
|
||
}
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 运行到选中点位
|
||
/// <summary>
|
||
/// 当前调试运行点位
|
||
/// </summary>
|
||
RobotPoint CurrPoint { get; set; }
|
||
/// <summary>
|
||
/// 运行到选中点位
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public override void RunSelectedRobotPointPoint(object obj)
|
||
{
|
||
if (SelectedRobotPoint != null && SelectedRobotPoint.ID > 0 && RunningShow.StatusBackground != ColorStr.Green)
|
||
{
|
||
|
||
if (XizuiLabelPaperList[0].QiGangUp.LightUp && XizuiLabelPaperList[1].QiGangUp.LightUp)
|
||
{
|
||
CurrPoint = Coper(SelectedRobotPoint);
|
||
List<RobotPoint> ls = PointFenxi(CurrPoint);
|
||
MotionStart(ls);
|
||
|
||
}
|
||
else
|
||
{
|
||
MyMessageBox.Show("请把吸嘴气缸回到原位");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//MessageBox.Show("请选择要运行的点位");
|
||
}
|
||
}
|
||
#endregion
|
||
#endregion
|
||
|
||
#region 手动回原点
|
||
/// <summary>
|
||
/// 手动回原点
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public override void GoHome(object obj)
|
||
{
|
||
if (XizuiLabelPaperList[0].QiGangUp.LightUp && XizuiLabelPaperList[1].QiGangUp.LightUp)
|
||
{
|
||
RobotPoint rrp = RobotPointList.Where(w => w.PointType == 1 && w.PointClass == 0).FirstOrDefault();
|
||
List<RobotPoint> ls = PointFenxi(rrp);
|
||
MotionStart(ls);
|
||
}
|
||
else
|
||
{
|
||
MyMessageBox.Show("请把吸嘴气缸回到原位");
|
||
}
|
||
|
||
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 机器人回原点
|
||
|
||
/// <summary>
|
||
/// 机器人回原点
|
||
/// </summary>
|
||
public override void RobotOrgin()
|
||
{
|
||
switch (HomeStep)
|
||
{
|
||
case GengralProcessEnum.Start:
|
||
if (ControllerConn != null && ControllerConn.ConnStatu.StatuInfo == "连接正常")
|
||
{
|
||
|
||
HomeTime = DateTime.Now;
|
||
HomeStep++;
|
||
}
|
||
break;
|
||
case GengralProcessEnum.Step1:
|
||
if (CommandInteraction != null && CommandInteraction.ConnStatu.StatuInfo == "连接正常")
|
||
{
|
||
CurrPoint = RobotPointList.Where(w => w.PointType == 1 && w.PointClass == 0).FirstOrDefault();
|
||
List<RobotPoint> ls = PointFenxi(CurrPoint);
|
||
MotionStart(ls);
|
||
HomeTime = DateTime.Now;
|
||
HomeStep++;
|
||
}
|
||
break;
|
||
case GengralProcessEnum.Step2:
|
||
if (Bijiao(CurrPoint))
|
||
{
|
||
HomeStep = GengralProcessEnum.End;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 更新工具坐标系
|
||
/// <summary>
|
||
/// 更新工具坐标系
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
public override void UpToolCoordinate(object obj)
|
||
{
|
||
if (MessageBox.Show("错误的设置可能带来风险,你确定要更新工具坐标系吗?", "警告", MessageBoxButton.OKCancel, MessageBoxImage.Exclamation) != MessageBoxResult.OK)
|
||
{
|
||
return;
|
||
}
|
||
|
||
RobotPoint p1 = RobotPointList.Where(r => r.PointType == 100).FirstOrDefault();
|
||
RobotPoint p2 = RobotPointList.Where(r => r.PointType == 101).FirstOrDefault();
|
||
|
||
double d = Math.Abs((Math.Abs(p1.PointU) - Math.Abs(p2.PointU)));
|
||
if (d < 178)
|
||
{
|
||
MessageBox.Show("工具坐标点位的角度必须相差180度,请重新调整工具坐标点位");
|
||
return;
|
||
}
|
||
|
||
RobotPoint p = CalcTool(p1, p2);
|
||
p.Command = "UpTool";
|
||
p.ToolNum = 1;
|
||
SendCommand(p);
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 工具坐标系的创建算法
|
||
/// <summary>
|
||
/// 获取工具坐标系参数
|
||
/// </summary>
|
||
/// <param name="p1">第一个坐标点</param>
|
||
/// <param name="p2">旋转180度后的第二个坐标点</param>
|
||
/// <param name="toolNum">工具坐标序号</param>
|
||
/// <returns></returns>
|
||
public RobotPoint CalcTool(RobotPoint p1, RobotPoint p2)
|
||
{
|
||
double xTool = (p1.PointX - p2.PointX) / 2;
|
||
double yTool = (p1.PointY - p2.PointY) / 2;
|
||
double theta = Math.Atan2(yTool, xTool); //Math.Atan2(xTool, yTool);
|
||
theta = theta - (p2.PointU * Math.PI / 180);//角度转弧度
|
||
double rTool = Math.Sqrt(xTool * xTool + yTool * yTool);
|
||
xTool = Math.Cos(theta) * rTool;
|
||
yTool = Math.Sin(theta) * rTool;
|
||
RobotPoint toolPoint = new RobotPoint() { PointX = xTool, PointY = yTool };
|
||
|
||
Tool_X = xTool;
|
||
Tool_Y = yTool;
|
||
return toolPoint;
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 世界坐标转工具坐标
|
||
/// </summary>
|
||
/// <param name="wp"></param>
|
||
/// <param name="xtool"></param>
|
||
/// <param name="ytool"></param>
|
||
/// <returns></returns>
|
||
private RobotPoint WorldToTool(RobotPoint wp, double xtool, double ytool)
|
||
{
|
||
RobotPoint rp = new RobotPoint();
|
||
rp.PointU = wp.PointU;
|
||
rp.PointZ = wp.PointZ;
|
||
rp.PointX = wp.PointX + (xtool * Math.Cos(wp.PointU) - ytool * Math.Sin(wp.PointU));
|
||
rp.PointY = wp.PointY + (xtool * Math.Sin(wp.PointU) + ytool * Math.Cos(wp.PointU));
|
||
return rp;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 工具坐标系 X系数
|
||
/// </summary>
|
||
public double Tool_X { get; set; }
|
||
/// <summary>
|
||
/// 工具坐标系 Y系数
|
||
/// </summary>
|
||
public double Tool_Y { get; set; }
|
||
|
||
|
||
public RobotPoint wordPoint = new RobotPoint() { PointX = -145.011, PointY = 423.095, PointZ = -10, PointU = 179.972 };
|
||
public RobotPoint toolPoint = new RobotPoint() { PointX = -144.166, PointY = 510.445, PointZ = -10, PointU = 179.972 };
|
||
|
||
/// <summary>
|
||
/// 工具坐标与世界坐标的转换
|
||
/// </summary>
|
||
/// <param name="Rp"></param>
|
||
/// <param name="WordToTool"></param>
|
||
/// <returns></returns>
|
||
public RobotPoint Pointzhuanghuan(RobotPoint Rp, bool WordToTool)
|
||
{
|
||
Tool_X = -0.802;
|
||
Tool_Y = -87.350;
|
||
|
||
//Rp.PointU =Math.Round( Rp.PointU-180,3);
|
||
Rp.PointU = (-1 * Math.PI / 180);
|
||
|
||
RobotPoint rp = new RobotPoint();
|
||
rp.PointU = Rp.PointU;
|
||
rp.PointZ = Rp.PointZ;
|
||
if (WordToTool) //世界坐标转工具坐标
|
||
{
|
||
double xcos = Math.Round(Rp.PointX * Math.Cos(Rp.PointU), 3);
|
||
double xsin = Math.Round(Rp.PointX * Math.Sin(Rp.PointU), 3);
|
||
double ysin = Math.Round(Rp.PointY * Math.Sin(Rp.PointU), 3);
|
||
double ycos = Math.Round(Rp.PointY * Math.Cos(Rp.PointU), 3);
|
||
rp.PointX = (Rp.PointX * Math.Cos(Rp.PointU) - Rp.PointY * Math.Sin(Rp.PointU)) - Tool_X;
|
||
rp.PointY = (Rp.PointX * Math.Sin(Rp.PointU) + Rp.PointY * Math.Cos(Rp.PointU)) - Tool_Y;
|
||
}
|
||
else //否则 工具坐标转世界坐标
|
||
{
|
||
rp.PointX = Rp.PointX - (Tool_X * Math.Cos(Rp.PointU) + Tool_Y * Math.Sin(Rp.PointU));
|
||
rp.PointY = Rp.PointY - (Tool_X * Math.Sin(Rp.PointU) - Tool_Y * Math.Cos(Rp.PointU));
|
||
}
|
||
return rp;
|
||
}
|
||
|
||
|
||
|
||
#endregion
|
||
|
||
#region 运行机械手点位
|
||
/// <summary>
|
||
/// 运行机械手点位
|
||
/// </summary>
|
||
/// <param name="point"></param>
|
||
/// <returns></returns>
|
||
public override bool RunRobotPoint(RobotPoint point, bool runType = false)
|
||
{
|
||
try
|
||
{
|
||
List<RobotPoint> ls = PointFenxi(point);
|
||
MotionStart(ls);
|
||
return true;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Z轴安全高度
|
||
/// </summary>
|
||
public int limtZ = 400;
|
||
|
||
|
||
/// <summary>
|
||
/// 临时点位
|
||
/// </summary>
|
||
RobotPoint TempPoint { get; set; }
|
||
|
||
/// <summary>
|
||
/// 当前机械手所在区域
|
||
/// </summary>
|
||
int CurrQ = 0;
|
||
/// <summary>
|
||
/// 目标点所在区域
|
||
/// </summary>
|
||
int MubiaoQ = 0;
|
||
|
||
RobotPoint Guodu1 { get; set; }
|
||
RobotPoint Guodu2 { get; set; }
|
||
RobotPoint Guodu3 { get; set; }
|
||
|
||
/// <summary>
|
||
/// 获取机械手所在区域
|
||
/// </summary>
|
||
/// <param name="P"></param>
|
||
/// <returns></returns>
|
||
public int GetQuYu(RobotPoint P)
|
||
{
|
||
int a = 0;
|
||
if (P.PointY > 0 && P.PointX < 0)
|
||
{
|
||
a = 1;
|
||
}
|
||
else if (P.PointY > 0 && P.PointX > 0)
|
||
{
|
||
a = 2;
|
||
}
|
||
else if (P.PointY < 0 && P.PointX < 0)
|
||
{
|
||
a = 3;
|
||
}
|
||
else if (P.PointY < 0 && P.PointX > 0)
|
||
{
|
||
a = 4;
|
||
}
|
||
|
||
return a;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 路径规划的当前点位
|
||
/// </summary>
|
||
RobotPoint CurrP { get; set; }
|
||
/// <summary>
|
||
/// 点位路径规划
|
||
/// </summary>
|
||
/// <param name="P"></param>
|
||
public List<RobotPoint> PointFenxi(RobotPoint P)
|
||
{
|
||
List<RobotPoint> poinList = new List<RobotPoint>();
|
||
if (RobotCurPos != null && RobotCurPos.PointX != 0 && RobotCurPos.PointY != 0 && RobotCurPos.PointZ != 0)
|
||
{
|
||
CurrQ = GetQuYu(RobotCurPos);
|
||
MubiaoQ = GetQuYu(P);
|
||
//如果当前区域小于等于0 或者如果处于当前位置 则无需移动
|
||
if ((CurrQ <= 0 && MubiaoQ <= 0) || Bijiao(P))
|
||
{
|
||
return poinList;
|
||
}
|
||
|
||
|
||
|
||
//获取过度点
|
||
Guodu1 = RobotPointList.Where(w => w.PointClass == 0 && w.PointType == 11).FirstOrDefault();
|
||
Guodu2 = RobotPointList.Where(w => w.PointClass == 0 && w.PointType == 12).FirstOrDefault();
|
||
Guodu3 = RobotPointList.Where(w => w.PointClass == 0 && w.PointType == 13).FirstOrDefault();
|
||
|
||
CurrP = Coper(RobotCurPos);//确定当前点
|
||
|
||
#region 移动过度点
|
||
switch (CurrQ)
|
||
{
|
||
case 1:
|
||
switch (MubiaoQ)
|
||
{
|
||
case 1:
|
||
//执行相同区域的方法
|
||
//RunStep++;
|
||
break;
|
||
case 2:
|
||
//移动到过度点1
|
||
poinList.AddRange(MoveGuoduPoint(Guodu1));
|
||
break;
|
||
case 3:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu1));
|
||
poinList.AddRange(MoveGuoduPoint(Guodu3));
|
||
CurrP = Coper(Guodu3);//改变当前点
|
||
break;
|
||
case 4:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu1));
|
||
poinList.AddRange(MoveGuoduPoint(Guodu2));
|
||
break;
|
||
}
|
||
break;
|
||
case 2:
|
||
switch (MubiaoQ)
|
||
{
|
||
case 1:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu1));
|
||
break;
|
||
case 2:
|
||
//执行相同区域的方法
|
||
//RunStep++;
|
||
break;
|
||
case 3:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu2));
|
||
poinList.AddRange(MoveGuoduPoint(Guodu3));
|
||
break;
|
||
case 4:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu2));
|
||
break;
|
||
}
|
||
break;
|
||
case 3:
|
||
switch (MubiaoQ)
|
||
{
|
||
case 1:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu3));
|
||
poinList.AddRange(MoveGuoduPoint(Guodu1));
|
||
break;
|
||
case 2:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu3));
|
||
poinList.AddRange(MoveGuoduPoint(Guodu2));
|
||
break;
|
||
case 3:
|
||
//执行相同区域的方法
|
||
// RunStep++;
|
||
break;
|
||
case 4:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu3));
|
||
break;
|
||
}
|
||
break;
|
||
case 4:
|
||
switch (MubiaoQ)
|
||
{
|
||
case 1:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu2));
|
||
poinList.AddRange(MoveGuoduPoint(Guodu1));
|
||
break;
|
||
case 2:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu2));
|
||
break;
|
||
case 3:
|
||
poinList.AddRange(MoveGuoduPoint(Guodu3));
|
||
break;
|
||
case 4:
|
||
//执行相同区域的方法
|
||
//RunStep++;
|
||
break;
|
||
}
|
||
break;
|
||
}
|
||
#endregion
|
||
|
||
//目标点低于当前点位 则已当前高度移动到目标点上空
|
||
if (CurrP.PointZ > P.PointZ)
|
||
{
|
||
RobotPoint tmp = Coper(P);
|
||
tmp.PointZ = CurrP.PointZ;
|
||
tmp.PointRunModel = 2;
|
||
poinList.Add(tmp);
|
||
}
|
||
else //否则先抬升到目标点的高度
|
||
{
|
||
RobotPoint tmp = Coper(CurrP);
|
||
tmp.PointZ = P.PointZ;
|
||
tmp.PointRunModel = 2;
|
||
poinList.Add(tmp);
|
||
}
|
||
|
||
RobotPoint pp = Coper(P);//复制目标点
|
||
pp.PointRunModel = 2;
|
||
poinList.Add(pp);
|
||
|
||
|
||
}
|
||
|
||
return poinList;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 移动到过度点
|
||
/// </summary>
|
||
/// <param name="P"></param>
|
||
/// <returns></returns>
|
||
public List<RobotPoint> MoveGuoduPoint(RobotPoint P)
|
||
{
|
||
List<RobotPoint> list = new List<RobotPoint>();
|
||
if (CurrP != null)
|
||
{
|
||
//如果当前位置低于安全位置 则先抬升到安全点
|
||
if (CurrP.PointZ < limtZ)
|
||
{
|
||
RobotPoint tmp = Coper(CurrP);
|
||
tmp.PointZ = limtZ;
|
||
tmp.PointRunModel = 2;//直线运行
|
||
list.Add(tmp);
|
||
}
|
||
//直接关节运行到过度点
|
||
RobotPoint guodu = Coper(P);
|
||
guodu.PointRunModel = 1;//关节运行
|
||
list.Add(guodu);
|
||
CurrP = Coper(P);//改变当前位置
|
||
}
|
||
|
||
return list;
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region 机器人移动日志
|
||
byte[] data { get; set; }
|
||
|
||
/// <summary>
|
||
/// 文件夹路径
|
||
/// </summary>
|
||
string path { get; set; }
|
||
/// <summary>
|
||
/// 文件路径
|
||
/// </summary>
|
||
string paths { get; set; }
|
||
|
||
/// <summary>
|
||
/// 打开的文件流对象
|
||
/// </summary>
|
||
FileStream FS { get; set; }
|
||
|
||
|
||
/// <summary>
|
||
/// 记录日志信息到文件
|
||
/// </summary>
|
||
/// <param name="info"></param>
|
||
private void RecordLog(string showMag)
|
||
{
|
||
try
|
||
{
|
||
lock (this)
|
||
{
|
||
path = @"D:\\LAGLog\RobotLog"; //文件夹路径
|
||
|
||
paths = @"D:\\LAGLog\RobotLog\" + DateTime.Now.ToString("D") + ".txt"; //文件路径 以年月日为名称
|
||
|
||
//创建文件夹
|
||
if (!Directory.Exists(path))
|
||
{
|
||
Directory.CreateDirectory(path);
|
||
}
|
||
|
||
//打开或创建文件
|
||
FS = File.Open(paths, FileMode.Append);
|
||
|
||
//获得字节数组
|
||
|
||
data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff") + showMag + "\r\n");
|
||
//开始写入
|
||
FS.Write(data, 0, data.Length);
|
||
//清空缓冲区、关闭流
|
||
FS.Flush();
|
||
FS.Close();
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 比较坐标是否到位
|
||
/// </summary>
|
||
/// <param name="point"></param>
|
||
/// <returns></returns>
|
||
public override bool Bijiao(RobotPoint point)
|
||
{
|
||
lock (this)
|
||
{
|
||
try
|
||
{
|
||
if (RobotCurPos == null || point == null)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
double x = Math.Abs(point.PointX - RobotCurPos.PointX);
|
||
double y = Math.Abs(point.PointY - RobotCurPos.PointY);
|
||
double z = Math.Abs(point.PointZ - RobotCurPos.PointZ);
|
||
double u = Math.Abs(point.PointU - RobotCurPos.PointU);
|
||
double v = Math.Abs(point.PointV - RobotCurPos.PointV);
|
||
double w = Math.Abs(Math.Abs(point.PointW) - Math.Abs(RobotCurPos.PointW));
|
||
if (x <= 0.1 && y <= 0.1 && z <= 0.1 && u <= 0.1 && v <= 0.1 && w <= 0.1)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 工具坐标系
|
||
/// </summary>
|
||
int Tool = 1;
|
||
/// <summary>
|
||
/// 用户坐标系
|
||
/// </summary>
|
||
int UserCs = 1;
|
||
|
||
|
||
string UserNameStr = "administrator";
|
||
string PasswordStr = "12345678";
|
||
|
||
|
||
|
||
|
||
#region 登录机械手
|
||
/// <summary>
|
||
/// 登录机械手
|
||
/// </summary>
|
||
/// <param name="user"></param>
|
||
/// <param name="password"></param>
|
||
private void LoginRobot()
|
||
{
|
||
//创建xml文档
|
||
XmlDocument xmldoc = new XmlDocument();
|
||
|
||
//加入XML声明
|
||
XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
|
||
xmldoc.AppendChild(xmldecl);
|
||
|
||
|
||
//加入一个根元素
|
||
XmlElement Bodys = xmldoc.CreateElement("Bodys");
|
||
xmldoc.AppendChild(Bodys);
|
||
|
||
|
||
XmlElement Cmd = xmldoc.CreateElement("Cmd");//创建一个子节点
|
||
Cmd.SetAttribute("Name", "Login");//设置该节点属性
|
||
Cmd.SetAttribute("CmdCont", "0");//设置该节点属性
|
||
Cmd.SetAttribute("Status", "Send");//设置该节点属性
|
||
|
||
Bodys.AppendChild(Cmd);
|
||
|
||
XmlElement Param = xmldoc.CreateElement("Param");//创建一个子节点
|
||
Param.SetAttribute("UserName", UserNameStr);//设置该节点属性
|
||
Param.SetAttribute("Password", PasswordStr);//设置该节点属性
|
||
|
||
Cmd.AppendChild(Param);
|
||
|
||
//string str = xmldoc.InnerXml.Replace("1.0", "Turin.Robot.V2.0");
|
||
string str = xmldoc.InnerXml;
|
||
SendCommand(str);
|
||
}
|
||
#endregion
|
||
|
||
#region 获取当前坐标
|
||
/// <summary>
|
||
/// 获取当前坐标
|
||
/// </summary>
|
||
public void GetCurrAllPos()
|
||
{
|
||
// RobotCurPos = null;//清空当前坐标
|
||
//创建xml文档
|
||
XmlDocument xmldoc = new XmlDocument();
|
||
|
||
//加入XML声明
|
||
XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
|
||
xmldoc.AppendChild(xmldecl);
|
||
|
||
|
||
//加入一个根元素
|
||
XmlElement Bodys = xmldoc.CreateElement("Bodys");
|
||
xmldoc.AppendChild(Bodys);
|
||
|
||
|
||
XmlElement Cmd = xmldoc.CreateElement("Cmd");//创建一个子节点
|
||
Cmd.SetAttribute("Name", "GetCurrAllPos");//设置该节点属性
|
||
Cmd.SetAttribute("CmdCont", "0");//设置该节点属性
|
||
Cmd.SetAttribute("Status", "Send");//设置该节点属性
|
||
|
||
Bodys.AppendChild(Cmd);
|
||
|
||
XmlElement Param = xmldoc.CreateElement("Param");//创建一个子节点
|
||
Param.SetAttribute("Tool", Tool.ToString());//设置该节点属性
|
||
Param.SetAttribute("User", UserCs.ToString());//设置该节点属性
|
||
|
||
Cmd.AppendChild(Param);
|
||
string str = xmldoc.InnerXml;
|
||
//SendCommand(str);
|
||
SendControl(str);
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 机械手PG运行
|
||
/// <summary>
|
||
/// 机械手PG运行
|
||
/// </summary>
|
||
/// <param name="axis">轴号</param>
|
||
/// <param name="dis">移动距离</param>
|
||
private void PGRun(string axis, double dis)
|
||
{
|
||
|
||
DateTime time = DateTime.Now;
|
||
while (RobotCurPos == null)
|
||
{
|
||
if (time.AddSeconds(1) < DateTime.Now)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
switch (axis)
|
||
{
|
||
case "X":
|
||
RobotCurPos.PointX += dis;
|
||
MotionStart(RobotCurPos, "MoveL");
|
||
break;
|
||
case "Y":
|
||
RobotCurPos.PointY += dis;
|
||
MotionStart(RobotCurPos, "MoveL");
|
||
break;
|
||
case "Z":
|
||
RobotCurPos.PointZ += dis;
|
||
MotionStart(RobotCurPos, "MoveL");
|
||
break;
|
||
case "U":
|
||
RobotCurPos.PointU += dis;
|
||
MotionStart(RobotCurPos, "MoveL");
|
||
break;
|
||
}
|
||
|
||
|
||
|
||
|
||
//dis = Math.Abs(dis);
|
||
|
||
////创建xml文档
|
||
//XmlDocument xmldoc = new XmlDocument();
|
||
|
||
////加入XML声明
|
||
//XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
|
||
//xmldoc.AppendChild(xmldecl);
|
||
|
||
|
||
////加入一个根元素
|
||
//XmlElement Bodys = xmldoc.CreateElement("Bodys");
|
||
//xmldoc.AppendChild(Bodys);
|
||
|
||
|
||
//XmlElement Cmd = xmldoc.CreateElement("Cmd");//创建一个子节点
|
||
//Cmd.SetAttribute("Name", "Jog");//设置该节点属性
|
||
//Cmd.SetAttribute("CmdCont", "0");//设置该节点属性
|
||
//Cmd.SetAttribute("Status", "Send");//设置该节点属性
|
||
|
||
//Bodys.AppendChild(Cmd);
|
||
|
||
//XmlElement Param = xmldoc.CreateElement("Param");//创建参数
|
||
//Param.SetAttribute("Mode", "2");//点动模式
|
||
//Param.SetAttribute("Motion", "2");//坐标系 Motion 为1 时,Axis 取值范围是1 至9,代表1 至9 轴。Motion 为2/ 3 / 4 时,Axis 取值范围是1 至6,依次代表X、Y、Z、RX、RY、RZ 等6 个轴。Motion 为5 时,Axis 取值范围是1 至6,1 至3 表示非协同点动变位机1 至3 轴,4 至6 表示协同点动变位机1 至3 轴。
|
||
//Param.SetAttribute("Operate", "Start");//启停 start 和stop
|
||
//Param.SetAttribute("Axis", axis.ToString());//轴号
|
||
//Param.SetAttribute("Speed", "20");//百分比
|
||
//Param.SetAttribute("Offset", "true");//可选值为:true 或false(一般使用false)
|
||
//Param.SetAttribute("TarPos", dis.ToString());//位置
|
||
|
||
//Cmd.AppendChild(Param);
|
||
|
||
//string str = xmldoc.InnerXml;
|
||
//SendCommand(str);
|
||
|
||
|
||
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 清除报警
|
||
/// <summary>
|
||
/// 清除报警
|
||
/// </summary>
|
||
public void ClearRobotError()
|
||
{
|
||
//创建xml文档
|
||
XmlDocument xmldoc = new XmlDocument();
|
||
|
||
//加入XML声明
|
||
XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
|
||
xmldoc.AppendChild(xmldecl);
|
||
|
||
|
||
//加入一个根元素
|
||
XmlElement Bodys = xmldoc.CreateElement("Bodys");
|
||
xmldoc.AppendChild(Bodys);
|
||
|
||
|
||
XmlElement Cmd = xmldoc.CreateElement("Cmd");//创建一个子节点
|
||
Cmd.SetAttribute("Name", "ClearRobotError");//设置该节点属性
|
||
Cmd.SetAttribute("CmdCont", "0");//设置该节点属性
|
||
Cmd.SetAttribute("Status", "Send");//设置该节点属性
|
||
|
||
Bodys.AppendChild(Cmd);
|
||
|
||
string str = xmldoc.InnerXml;
|
||
//SendCommand(str);
|
||
SendControl(str);
|
||
}
|
||
#endregion
|
||
|
||
#region 启动脚本 运行点位
|
||
/// <summary>
|
||
/// 启动脚本 运行点位
|
||
/// </summary>
|
||
/// <param name="p"></param>
|
||
/// <param name="RunModel">运行模式</param>
|
||
private void MotionStart(RobotPoint p, string RunModel)
|
||
{
|
||
// <? xml version = "Turin.Robot.V2.0" encoding = "UTF-8" ?>< Bodys >< Cmd Name = "MotionStart" CmdCont = "0" Status = "Send" >
|
||
//< Param UseInThread = "是否" IsGcode = "是否" MainFileName = "主程序" StartFileName = "子程序" StartLine = "启动行" ExeLines = "执行行" ExeLoops = "循环次数" />
|
||
//< Data > 脚本内容 </ Data ></ Cmd >\n" </Bodys>
|
||
|
||
|
||
|
||
MotionStop(); //启动脚本前 先停止之前所有脚本
|
||
|
||
//创建xml文档
|
||
XmlDocument xmldoc = new XmlDocument();
|
||
|
||
//加入XML声明
|
||
XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
|
||
xmldoc.AppendChild(xmldecl);
|
||
|
||
|
||
//加入一个根元素
|
||
XmlElement Bodys = xmldoc.CreateElement("Bodys");
|
||
xmldoc.AppendChild(Bodys);
|
||
|
||
|
||
XmlElement Cmd = xmldoc.CreateElement("Cmd");//创建一个子节点
|
||
Cmd.SetAttribute("Name", "MotionStart");//设置该节点属性
|
||
Cmd.SetAttribute("CmdCont", "0");//设置该节点属性
|
||
Cmd.SetAttribute("Status", "Send");//设置该节点属性
|
||
|
||
Bodys.AppendChild(Cmd);
|
||
|
||
XmlElement Param = xmldoc.CreateElement("Param");//创建参数
|
||
Param.SetAttribute("UseInThread", "false");//是否
|
||
Param.SetAttribute("IsGcode", "false");//是否
|
||
Param.SetAttribute("MainFileName", "Start");//主程序
|
||
Param.SetAttribute("StartFileName", "");//轴号
|
||
Param.SetAttribute("StartLine", "0");//启动行
|
||
Param.SetAttribute("ExeLines", "0");//执行行
|
||
Param.SetAttribute("ExeLoops", "1");//循环次数
|
||
Cmd.AppendChild(Param);
|
||
|
||
XmlElement Data = xmldoc.CreateElement("Data");//创建一个子节点
|
||
Data.InnerText = PointToStr(p, RunModel);//设置运行脚本
|
||
|
||
Cmd.AppendChild(Data);
|
||
string str = xmldoc.InnerXml;
|
||
SendCommand(str);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 停止脚本
|
||
/// </summary>
|
||
public void MotionStop()
|
||
{
|
||
//创建xml文档
|
||
XmlDocument xmldoc = new XmlDocument();
|
||
|
||
//加入XML声明
|
||
XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
|
||
xmldoc.AppendChild(xmldecl);
|
||
|
||
|
||
//加入一个根元素
|
||
XmlElement Bodys = xmldoc.CreateElement("Bodys");
|
||
xmldoc.AppendChild(Bodys);
|
||
|
||
|
||
XmlElement Cmd = xmldoc.CreateElement("Cmd");//创建一个子节点
|
||
Cmd.SetAttribute("Name", "MotionStop");//设置该节点属性
|
||
Cmd.SetAttribute("CmdCont", "0");//设置该节点属性
|
||
Cmd.SetAttribute("Status", "Send");//设置该节点属性
|
||
|
||
Bodys.AppendChild(Cmd);
|
||
string str = xmldoc.InnerXml;
|
||
SendCommand(str);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 坐标转字符串
|
||
/// </summary>
|
||
/// <param name="p"></param>
|
||
/// <param name="RunModel">运行模式</param>
|
||
/// <returns></returns>
|
||
private string PointToStr(RobotPoint p, string RunModel)
|
||
{
|
||
int sd = 100;
|
||
|
||
if (p.SpeedGear <= 0)
|
||
{
|
||
if (CurrPoint != null)
|
||
{
|
||
sd = (int)CurrPoint.SpeedGear * SpeedS / 100;
|
||
}
|
||
else
|
||
{
|
||
sd = (int)SelectedRobotPoint.SpeedGear * SpeedS / 100;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
sd = (int)p.SpeedGear * SpeedS / 100;
|
||
}
|
||
|
||
|
||
StringBuilder sb = new StringBuilder();
|
||
//sb.Append("MoveJ");
|
||
//sb.Append("MoveL");
|
||
sb.Append(RunModel);
|
||
sb.AppendFormat("(");
|
||
sb.AppendFormat("{0},{1},{2},{3},{4},{5},{6},{7},{8},", p.PointX, p.PointY, p.PointZ, p.PointV, p.PointW, p.PointU, 0, 0, 0);//参数 1-9:在用户坐标系下的当前位置,即 X,Y,Z,RX,RY,RZ,J7,J8,J9(可忽略);
|
||
sb.AppendFormat("{0},{1},{2},{3},{4},{5},{6},{7},{8},", p.J1, p.J2, p.J3, p.J4, p.J5, p.J6, p.J7, p.J8, p.J9); //参数 10 - 18:在关节坐标下的当前位置,即 J1, J2, J3, J4, J5, J6, J7, J8, J9,单位°;
|
||
sb.AppendFormat("{0},{1},{2},", sd, 10, 1);//速度 加速度 速度单位 1为% 2为mm
|
||
sb.AppendFormat("{0},{1},{2}", 50, Tool, UserCs);//平滑系数(0-100) 工具坐标 用户坐标
|
||
sb.AppendFormat(")");
|
||
string sbStr = sb.ToString();
|
||
return sbStr;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 启动脚本 运行多个点位
|
||
/// </summary>
|
||
/// <param name="p"></param>
|
||
/// <param name="RunModel">运行模式</param>
|
||
private void MotionStart(List<RobotPoint> Pointlist)
|
||
{
|
||
// <? xml version = "Turin.Robot.V2.0" encoding = "UTF-8" ?>< Bodys >< Cmd Name = "MotionStart" CmdCont = "0" Status = "Send" >
|
||
//< Param UseInThread = "是否" IsGcode = "是否" MainFileName = "主程序" StartFileName = "子程序" StartLine = "启动行" ExeLines = "执行行" ExeLoops = "循环次数" />
|
||
//< Data > 脚本内容 </ Data ></ Cmd >\n" </Bodys>
|
||
|
||
if (Pointlist == null || Pointlist.Count <= 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
|
||
MotionStop(); //启动脚本前 先停止之前所有脚本
|
||
|
||
//创建xml文档
|
||
XmlDocument xmldoc = new XmlDocument();
|
||
|
||
//加入XML声明
|
||
XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
|
||
xmldoc.AppendChild(xmldecl);
|
||
|
||
|
||
//加入一个根元素
|
||
XmlElement Bodys = xmldoc.CreateElement("Bodys");
|
||
xmldoc.AppendChild(Bodys);
|
||
|
||
|
||
XmlElement Cmd = xmldoc.CreateElement("Cmd");//创建一个子节点
|
||
Cmd.SetAttribute("Name", "MotionStart");//设置该节点属性
|
||
Cmd.SetAttribute("CmdCont", "0");//设置该节点属性
|
||
Cmd.SetAttribute("Status", "Send");//设置该节点属性
|
||
|
||
Bodys.AppendChild(Cmd);
|
||
|
||
XmlElement Param = xmldoc.CreateElement("Param");//创建参数
|
||
Param.SetAttribute("UseInThread", "false");//是否
|
||
Param.SetAttribute("IsGcode", "false");//是否
|
||
Param.SetAttribute("MainFileName", "Start");//主程序
|
||
Param.SetAttribute("StartFileName", "");//轴号
|
||
Param.SetAttribute("StartLine", "0");//启动行
|
||
Param.SetAttribute("ExeLines", "0");//执行行
|
||
Param.SetAttribute("ExeLoops", "1");//循环次数
|
||
Cmd.AppendChild(Param);
|
||
|
||
XmlElement Data = xmldoc.CreateElement("Data");//创建一个子节点
|
||
// Data.InnerText = PointToStr(p, RunModel);//设置运行脚本
|
||
string PL = "";
|
||
foreach (var item in Pointlist)
|
||
{
|
||
PL += PointToStr(item) + "\n";
|
||
}
|
||
Data.InnerText = PL;
|
||
Cmd.AppendChild(Data);
|
||
string str = xmldoc.InnerXml;
|
||
SendCommand(str);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 坐标转字符串
|
||
/// </summary>
|
||
/// <param name="p"></param>
|
||
/// <param name="RunModel">运行模式</param>
|
||
/// <returns></returns>
|
||
private string PointToStr(RobotPoint p)
|
||
{
|
||
string RunModel = "MoveJ";
|
||
switch (p.PointRunModel)
|
||
{
|
||
case 1:
|
||
RunModel = "MoveJ";
|
||
break;
|
||
case 2:
|
||
RunModel = "MoveL";
|
||
break;
|
||
}
|
||
|
||
int sd = 100;
|
||
|
||
if (p.SpeedGear <= 0)
|
||
{
|
||
if (CurrPoint != null)
|
||
{
|
||
sd = (int)CurrPoint.SpeedGear * SpeedS / 100;
|
||
}
|
||
else
|
||
{
|
||
sd = (int)SelectedRobotPoint.SpeedGear * SpeedS / 100;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
sd = (int)p.SpeedGear * SpeedS / 100;
|
||
}
|
||
|
||
|
||
StringBuilder sb = new StringBuilder();
|
||
//sb.Append("MoveJ");
|
||
//sb.Append("MoveL");
|
||
sb.Append(RunModel);
|
||
sb.AppendFormat("(");
|
||
sb.AppendFormat("{0},{1},{2},{3},{4},{5},{6},{7},{8},", p.PointX, p.PointY, p.PointZ, p.PointV, p.PointW, p.PointU, 0, 0, 0);//参数 1-9:在用户坐标系下的当前位置,即 X,Y,Z,RX,RY,RZ,J7,J8,J9(可忽略);
|
||
sb.AppendFormat("{0},{1},{2},{3},{4},{5},{6},{7},{8},", p.J1, p.J2, p.J3, p.J4, p.J5, p.J6, p.J7, p.J8, p.J9); //参数 10 - 18:在关节坐标下的当前位置,即 J1, J2, J3, J4, J5, J6, J7, J8, J9,单位°;
|
||
sb.AppendFormat("{0},{1},{2},", sd, 10, 1);//速度 加速度 速度单位 1为% 2为mm
|
||
sb.AppendFormat("{0},{1},{2}", 100, Tool, UserCs);//平滑系数(0-100) 工具坐标 用户坐标
|
||
sb.AppendFormat(")");
|
||
string sbStr = sb.ToString();
|
||
return sbStr;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 获取机器人状态
|
||
private void GetRobotState()
|
||
{
|
||
//创建xml文档
|
||
XmlDocument xmldoc = new XmlDocument();
|
||
|
||
//加入XML声明
|
||
XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
|
||
xmldoc.AppendChild(xmldecl);
|
||
|
||
|
||
//加入一个根元素
|
||
XmlElement Bodys = xmldoc.CreateElement("Bodys");
|
||
xmldoc.AppendChild(Bodys);
|
||
|
||
|
||
XmlElement Cmd = xmldoc.CreateElement("Cmd");//创建一个子节点
|
||
Cmd.SetAttribute("Name", "GetSystemRunningStatus");//设置该节点属性
|
||
Cmd.SetAttribute("CmdCont", "0");//设置该节点属性
|
||
Cmd.SetAttribute("Status", "Send");//设置该节点属性
|
||
|
||
Bodys.AppendChild(Cmd);
|
||
|
||
|
||
string str = xmldoc.InnerXml;
|
||
// SendCommand(str);
|
||
SendControl(str);
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 控制连接返回字符串解析
|
||
/// </summary>
|
||
/// <param name="Xml"></param>
|
||
/// <returns></returns>
|
||
public TurinCommandModel ControlReadXml(string Xml)
|
||
{
|
||
try
|
||
{
|
||
Xml = Xml.Replace("Turin.Robot.V2.0", "1.0").Replace('\0', ' ');
|
||
string ss = "<?xml version='1.0' encoding='UTF-8'?>";
|
||
string[] XmlList = Xml.Split(new string[] { ss }, StringSplitOptions.RemoveEmptyEntries);
|
||
// 解析XML
|
||
XElement root = XElement.Parse(ss + XmlList[0]);
|
||
|
||
TurinCommandModel cmd = new TurinCommandModel();
|
||
// 提取Cmd元素
|
||
XElement cmdElement = root.Element("Cmd");
|
||
if (cmdElement != null)
|
||
{
|
||
// 提取属性
|
||
cmd.Name = cmdElement.Attribute("Name")?.Value;
|
||
cmd.CmdCont = cmdElement.Attribute("CmdCont")?.Value;
|
||
cmd.Status = cmdElement.Attribute("Status")?.Value;
|
||
|
||
// 提取Param元素的属性
|
||
XElement paramElement = cmdElement.Element("Param");
|
||
|
||
if (paramElement != null)
|
||
{
|
||
foreach (var item in paramElement.Attributes())
|
||
{
|
||
cmd.Param.Add(item.Name.ToString(), item.Value);
|
||
}
|
||
}
|
||
|
||
|
||
// 提取Data元素内容
|
||
XElement dataElement = cmdElement.Element("Data");
|
||
if (dataElement != null)
|
||
{
|
||
cmd.Data = dataElement.Value;
|
||
}
|
||
|
||
}
|
||
return cmd;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return new TurinCommandModel();
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 解析机器人状态
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
private string JiexiState(string data)
|
||
{
|
||
lock (this)
|
||
{
|
||
try
|
||
{
|
||
//提取坐标
|
||
string[] da = data.Split('\n');
|
||
|
||
Dictionary<string, string> dalong = new Dictionary<string, string>();
|
||
foreach (var item in da)
|
||
{
|
||
if (!string.IsNullOrEmpty(item))
|
||
{
|
||
string[] st = item.Split(':');
|
||
dalong.Add(st[0], st[1]);
|
||
}
|
||
}
|
||
|
||
|
||
string sa = dalong.Where(w => w.Key == "MotionStatus").FirstOrDefault().Value.Trim();
|
||
switch (sa)
|
||
{
|
||
case "running":
|
||
RunningShow.StatusBackground = ColorStr.Green;
|
||
break;
|
||
case "stopped":
|
||
RunningShow.StatusBackground = ColorStr.Gray;
|
||
break;
|
||
}
|
||
|
||
return sa;
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return ex.Message;
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
|
||
|
||
#region 机械手返回字符串解析
|
||
/// <summary>
|
||
/// 机械手返回字符串解析
|
||
/// </summary>
|
||
/// <param name="Xml"></param>
|
||
/// <returns></returns>
|
||
public TurinCommandModel ReadXml(string Xml)
|
||
{
|
||
try
|
||
{
|
||
Xml = Xml.Replace("Turin.Robot.V2.0", "1.0").Replace('\0', ' ');
|
||
string ss = "<?xml version='1.0' encoding='UTF-8'?>";
|
||
string[] XmlList = Xml.Split(new string[] { ss }, StringSplitOptions.RemoveEmptyEntries);
|
||
// 解析XML
|
||
XElement root = XElement.Parse(ss + XmlList[0]);
|
||
|
||
TurinCommandModel cmd = new TurinCommandModel();
|
||
// 提取Cmd元素
|
||
XElement cmdElement = root.Element("Cmd");
|
||
if (cmdElement != null)
|
||
{
|
||
// 提取属性
|
||
cmd.Name = cmdElement.Attribute("Name")?.Value;
|
||
cmd.CmdCont = cmdElement.Attribute("CmdCont")?.Value;
|
||
cmd.Status = cmdElement.Attribute("Status")?.Value;
|
||
|
||
// 提取Param元素的属性
|
||
XElement paramElement = cmdElement.Element("Param");
|
||
|
||
if (paramElement != null)
|
||
{
|
||
foreach (var item in paramElement.Attributes())
|
||
{
|
||
cmd.Param.Add(item.Name.ToString(), item.Value);
|
||
}
|
||
}
|
||
|
||
|
||
// 提取Data元素内容
|
||
XElement dataElement = cmdElement.Element("Data");
|
||
if (dataElement != null)
|
||
{
|
||
cmd.Data = dataElement.Value;
|
||
}
|
||
|
||
}
|
||
return cmd;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return new TurinCommandModel();
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 提取机械手坐标
|
||
/// <summary>
|
||
/// 提取机械手坐标
|
||
/// </summary>
|
||
/// <param name="data"></param>
|
||
/// <returns></returns>
|
||
private RobotPoint GetPoint(string data)
|
||
{
|
||
try
|
||
{
|
||
//提取坐标
|
||
string[] da = data.Split('\n');
|
||
|
||
Dictionary<string, string> dalong = new Dictionary<string, string>();
|
||
foreach (var item in da)
|
||
{
|
||
string[] st = item.Split(':');
|
||
dalong.Add(st[0], st[1]);
|
||
}
|
||
|
||
|
||
string[] point = dalong.Where(w => w.Key == "Tool").FirstOrDefault().Value.Split(',');
|
||
string[] JP = dalong.Where(w => w.Key == "Joint").FirstOrDefault().Value.Split(',');
|
||
RobotPoint PP = new RobotPoint();
|
||
if (point.Length >= 6)
|
||
{
|
||
PP.PointX = Convert.ToDouble(point[0]);
|
||
PP.PointY = Convert.ToDouble(point[1]);
|
||
PP.PointZ = Convert.ToDouble(point[2]);
|
||
PP.PointU = Convert.ToDouble(point[5]);
|
||
PP.PointV = Convert.ToDouble(point[3]);
|
||
PP.PointW = Convert.ToDouble(point[4]);
|
||
|
||
}
|
||
|
||
if (JP.Length >= 6)
|
||
{
|
||
PP.J1 = Convert.ToDouble(JP[0]);
|
||
PP.J2 = Convert.ToDouble(JP[1]);
|
||
PP.J3 = Convert.ToDouble(JP[2]);
|
||
PP.J4 = Convert.ToDouble(JP[3]);
|
||
PP.J5 = Convert.ToDouble(JP[4]);
|
||
PP.J6 = Convert.ToDouble(JP[5]);
|
||
PP.J7 = Convert.ToDouble(JP[6]);
|
||
PP.J8 = Convert.ToDouble(JP[7]);
|
||
PP.J9 = Convert.ToDouble(JP[8]);
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
return PP;
|
||
}
|
||
catch (Exception)
|
||
{
|
||
return new RobotPoint();
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
public class TurinCommandModel
|
||
{
|
||
public string Name { get; set; }
|
||
public string CmdCont { get; set; }
|
||
public string Status { get; set; }
|
||
|
||
public Dictionary<string, string> Param { get; set; }
|
||
public string Data { get; set; }
|
||
|
||
public TurinCommandModel()
|
||
{
|
||
Name = "";
|
||
CmdCont = "";
|
||
Param = new Dictionary<string, string>();
|
||
|
||
}
|
||
}
|
||
|
||
public class StringCleaner
|
||
{
|
||
// 使用正则表达式来定义无效字符
|
||
private static readonly Regex _invalidCharactersRegex = new Regex("[\\\\/:*?\"<>|]");
|
||
|
||
// 清理字符串中的无效字符
|
||
public static string CleanString(string input)
|
||
{
|
||
return _invalidCharactersRegex.Replace(input, "");
|
||
}
|
||
|
||
|
||
|
||
//过滤方法
|
||
public static string FilterXmlErrorCode(string xml)
|
||
{
|
||
foreach (char c in xml)
|
||
{
|
||
if (!IsLegalXmlChar(c))
|
||
{
|
||
xml = xml.Replace(c.ToString(), "");
|
||
}
|
||
}
|
||
|
||
return xml;
|
||
}
|
||
|
||
//xml 无效字符检查
|
||
private static bool IsLegalXmlChar(int character)
|
||
{
|
||
return
|
||
(
|
||
character == 0x9 /* == '/t' == 9 */ ||
|
||
character == 0xA /* == '/n' == 10 */ ||
|
||
character == 0xD /* == '/r' == 13 */ ||
|
||
(character >= 0x20 && character <= 0xD7FF) ||
|
||
(character >= 0xE000 && character <= 0xFFFD) ||
|
||
(character >= 0x10000 && character <= 0x10FFFF)
|
||
);
|
||
}
|
||
|
||
|
||
}
|
||
}
|