Files
LM-Middleware/HexcalMC/MainFrom.cs
T
2024-02-22 09:45:52 +08:00

446 lines
13 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using ACS.SPiiPlusNET;
using HexcalMC.Base;
using HexcalMC.Form;
using HexcalMC.Hexcal;
using HexcalMC.Properties;
using Telerik.WinControls.UI;
namespace HexcalMC
{
public partial class MainFrom : RadRibbonForm
{
public MainFrom()
{
InitializeComponent();
}
private void MainFrom_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.FixedSingle; // 设置窗体边框样式为固定大小
MaximizeBox = false; // 禁用窗体的最大化按钮
DebugDfn.textBox_Msg = TextBoxMsg;
double[] dataX = { 1, 2, 3, 4, 5 };
double[] dataY = { 1, 4, 9, 16, 25 };
formsPlot1.Plot.AddScatter(dataX, dataY);
formsPlot1.Refresh();
//启动界面刷新
timer_RefreshUI.Start();
}
private void MainFrom_Shown(object sender, EventArgs e)
{
//启动服务端,用于接收hexcal传来的指令
StartServer();
}
private void MainFrom_FormClosed(object sender, FormClosedEventArgs e)
{
MyBase.TraceWriteLine("关闭程序");
DebugDfn._strEndTime = DateTime.Now.ToString("yyyy.MM.dd HH-mm-ss");
timer_RefreshUI.Stop();
string CopyFileName = DebugDfn.StrDebugSavePath + "Debug(" + DebugDfn._strStartTime + " To " +
DebugDfn._strEndTime + ")" + ".txt";
if (File.Exists(DebugDfn.StrDebugSavePath))
File.Copy(DebugDfn.StrDebugSavePath, CopyFileName);
if (Errors.ErrorWrite != null)
Errors.ErrorWrite.Close();
if (Errors.OtherWrite != null)
Errors.OtherWrite.Close();
if (Errors.StatusWrite != null)
Errors.StatusWrite.Close();
}
#region hexcal变量区
private TcpIpServer _mTcpIpServer; //创建tcpserver,用于接收hexcal传来的指令,并解析传递平台
private readonly bool _mBHexcalConnected = false;
#endregion hexcal变量区
#region
private Api _acs;
private const int MaxUiLimitCnt = 8;
private const int MaxUiIoCnt = 8;
private readonly int _mNTotalAxis = 0;
private int _mNTotalBuffer = 0;
private Axis[] _mArrAxisList = null;
private readonly bool _mBConnected = false;
// For update values
private MotorStates _mNMotorState; //运动状态
private ProgramStates _mNProgramState; //程序状态
private object _mObjReadVar;
private Array _mArrReadVector;
private double _mLfRPos, _mLfFPos, _mLfPe, _mLfFvel; //参考位置,反馈位置 位置误差 反馈速度 double类型
private int _mNValues, _mNOutputState;
private Label[] _mLblLeftLimit; //左限位
private Label[] _mLblRightLimit; //右限位
#endregion
#region hexcal软件相关
private void StartServer()
{
//启动服务器,并获取数据,解析
_mTcpIpServer = new TcpIpServer("100.0.0.1", Convert.ToString(1234));
try
{
//启动监听
if (_mTcpIpServer.StartListen())
{
//绑定两个事件 OnRaisedStatus 和OnRaisedMessage
_mTcpIpServer.OnRaisedMessage += ReceiveMessage;
_mTcpIpServer.OnRaisedStatus += ReceiveStatus;
DebugDfn.AddLogText("TCP服务端启动成功 ");
}
else
{
MessageBox.Show("TCP服务端启动失败,请检查网络连接,重新打开软件", "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
DebugDfn.AddLogText("启动TCP服务端异常" + ex);
}
}
private void ReceiveMessage(string clientIp, string msg) //接收的内容
{
//打印ClientIP 和 Msg
DebugDfn.AddLogText("接收到" + clientIp + ": " + msg);
//根据源地址的不同,执行不同处理
//如果 ClientIP 来自L2系统的话,执行解析
switch (clientIp)
{
case "172.19.153.80": //L2系统
//解析处理数据
ParseHexcalPacket(msg);
break;
case "127.0.0.1": //模拟长宽系统
break;
}
}
private void ParseHexcalPacket(string msg) //编写一个Hexcal协议解析函数
{
//根据不同的指令进行解析,3个层次,不合法,故障,最后是正常
//判断Msg的长度,如果不是指定长度,直接返回
if (msg.Length != 8) return;
//判断是否含有故障ERROR字样
if (msg.Contains("ERROR")) return;
//指令解析
string[] tokens = msg.Split(',');
foreach (string token in tokens)
{
Console.WriteLine(token);
}
//可能涉及到 将指令提取并单独处理的问题
if (string.Equals("CMMTYP", msg))
{
SendMsgToHexcal("CMMTYP MA 19617, FDC V15.00, 6 6 2 , 0");
}
else if (string.Equals("VERSION", msg)) //版本号
{
SendMsgToHexcal("00-000-000-00000 FDC V51.04.0000 DATE: 12/21/22 TIME: 12:50:55");
}
else if (string.Equals("^B", msg)) //查询状态
{
//todo 启动指令
}
else if (string.Equals("SHOW MAXSTROKESW", msg)) //最大行程
{
SendMsgToHexcal("MAXSTROKESW 233.200000,346.500000,15.100000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("00000003", msg)) //最小行程
{
SendMsgToHexcal("MINSTROKESW -68.800000,-55.500000,-286.900000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("SHOW MAXVEL", msg)) //最大速度
{
SendMsgToHexcal("MAXVEL 300.000000,300.000000,300.000000,0.000000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("SHOW MAXACC", msg)) //最大加速度
{
SendMsgToHexcal(
"MAXACC 300.000000,300.000000,300.000000,0.000000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("SHOW TEMPCOMPTYPE", msg)) //温度补偿,温度补偿 >1 表示支持温度补偿
{
SendMsgToHexcal("TEMPCOMPTYPE 1");
}
else if (string.Equals("READTP", msg))
{
SendMsgToHexcal("READTP 0.000000");
}
else if (string.Equals("SHOW SENSWKP", msg))
{
SendMsgToHexcal("X_ SENSWKP 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
}
else if (string.Equals("SHOW X_SENSAXIS", msg))
{
SendMsgToHexcal("X_SENSAXIS 6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
}
else if (string.Equals("SHOW Y_SENSAXIS", msg))
{
SendMsgToHexcal("Y_SENSAXIS 2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
}
else if (string.Equals("SHOW Z_SENSAXIS", msg))
{
SendMsgToHexcal("Z_SENSAXIS 7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0");
}
else if (string.Equals("SHOW MOVPAR", msg)) //获取速度
{
SendMsgToHexcal(
"MOVPAR 300.000000,300.000000,300.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.00000 0,0.000000");
}
else if (string.Equals("MOVPAR 300.0,300.0,300.0,0.0,0.0,0.0", msg)) //设置速度 xyz 轴的速度
{
SendMsgToHexcal("%");
}
else if (string.Equals("SHOW MAXVEL", msg))
{
SendMsgToHexcal("MAXVEL 300.000000,300.000000,300.000000,0.000000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("SHOW ACCEL", msg)) //获取加速
{
SendMsgToHexcal(
"ACCEL 1000.000000,1000.000000,1000.000000,0.000000,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("ACCEL 1000.0,1000.0,1000.0", msg)) //设置加速度
{
SendMsgToHexcal("%");
}
else if (string.Equals(
"PRBPIN 0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000, 0.000000",
msg))
{
SendMsgToHexcal("%");
}
else if (string.Equals("ENABLE TEMP", msg))
{
SendMsgToHexcal("");
}
else if (string.Equals("WKPPAR 20,0.0000115,20", msg))
{
SendMsgToHexcal("");
}
else if (string.Equals("SCLTMP", msg))
{
SendMsgToHexcal("");
}
else if (string.Equals("MOVABS 167.553848,-55.400002,-208.548203,0.000000", msg)) //移动指令
{
SendMsgToHexcal("%");
}
else if (string.Equals("GETPOS", msg)) //获取位置
{
SendMsgToHexcal("POS 167.553898,-55.400421,-208.548678,0.000000,0.000000,0.000000,0.000000");
}
else if (string.Equals("SHOW ESTOP", msg))
{
SendMsgToHexcal("ESTOP FALSE");
}
else if (string.Equals("", msg))
{
}
//todo 未知指令
}
private void ReceiveStatus(TcpIpServer.EnumTcpIpServer iType, string msg)
{
//记录到日志
DebugDfn.AddLogText(iType + " : " + msg);
}
private void SendMsgToHexcal(string msg)
{
//发送数据
_mTcpIpServer.SendMessageToAllClients(msg);
}
#endregion hexcal软件相关
#region ACS平台相关
private void BtnEnable_Click(object sender, EventArgs e) //使能所有轴
{
Axis[] axisList =
{
Axis.ACSC_AXIS_0, Axis.ACSC_AXIS_1, Axis.ACSC_AXIS_4, Axis.ACSC_NONE
}; //!!!! Important !! Must insert '-1' at the last
_acs.EnableM(axisList);
}
private void BtnDisable_Click(object sender, EventArgs e) //轴取消
{
// Disable all of axes
_acs.DisableAll();
}
private void TmrMonitor_Tick(object sender, EventArgs e) //用于刷新状态
{
int iAxisNo = cboAxisNo.SelectedIndex;
if (_mBConnected)
{
try
{
// Instruction 1. Using library functions - acsc_GetFPosition,
// acsc_GetRPosition, .... Instruction 2. Read ACS variable - Already defined
// almost things (FPOS, RPOS, ...) Motion parameters and state is array (Max
// length is total number of axes)
//
// * Library function can read only 1 axis information, so if you want to read
// several axes, you have to call the function many times. (This may cause
// communication delay.) Recommand (if you want to read many axes) : read/write
// variable using ReadVariable, ReadVariableScalar, ReadVariableVector, ReadVariableMatrix
// Get Motor State ACSPL+ Variable : MST (integer)
_mNMotorState = _acs.GetMotorState((Axis)iAxisNo);
// Returned value is integer, you need to use bitmaks
if ((_mNMotorState & MotorStates.ACSC_MST_MOVE) != 0) lblMoving.Image = Resources.On;
else lblMoving.Image = Resources.Off; // 运动中
if ((_mNMotorState & MotorStates.ACSC_MST_INPOS) != 0) lblInPos.Image = Resources.On;
else lblInPos.Image = Resources.Off; // 就位
if ((_mNMotorState & MotorStates.ACSC_MST_ACC) != 0) lblAcc.Image = Resources.On;
else lblAcc.Image = Resources.Off; // 加速
if ((_mNMotorState & MotorStates.ACSC_MST_ENABLE) != 0) lblEnable.Image = Resources.On;
else lblEnable.Image = Resources.Off; // 使能
// Reference position ACSPL+ Variable : RPOS (real)
_mLfRPos = _acs.GetRPosition((Axis)iAxisNo); // 参考位置
// Feedback position (Encoder value) ACSPL+ Variable : FPO (real)
_mLfFPos = _acs.GetFPosition((Axis)iAxisNo); //反馈位置
// PE (Position Error) There is no function in library. We need to use
// ReadVariable function
_mLfPe = (double)_acs.ReadVariable("PE", ProgramBuffer.ACSC_NONE, iAxisNo, iAxisNo); //位置误差
// Feedback Velocity
_mLfFvel = (double)_acs.ReadVariable("FVEL", ProgramBuffer.ACSC_NONE, iAxisNo, iAxisNo); //实际速度
// txtRPOS.Text = String.Format("{0:0.0000}", m_lfRPos); //参考位置
rtb_xPos.Text = string.Format("{0:0.0000}", _mLfFPos); //反馈位置
// txtPE.Text =
// String.Format("{0:0.0000}",
// m_lfPE); //实际速度
// txtFVEL.Text =
// String.Format("{0:0.0000}", m_lfFVEL);//位置误差
// Read left/right hardware limits state ACSPL+ Variable : FAULT (integer)
_mObjReadVar =
_acs.ReadVariableAsVector("FAULT", ProgramBuffer.ACSC_NONE, 0, _mNTotalAxis - 1);
if (_mObjReadVar != null)
{
_mArrReadVector = _mObjReadVar as Array;
if (_mArrReadVector != null)
{
for (int i = 0; i < _mNTotalAxis; i++)
{
UpdateLimitState(i, (int)_mArrReadVector.GetValue(i));
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
Debug.WriteLine(ex.Message);
}
}
}
private void Btn_ACSStart_Click(object sender, EventArgs e) //连接
{
btn_ACSStart.Enabled = false;
btn_ACSStop.Enabled = true;
// Set updating timer
tmrMonitor.Interval = 50;
tmrMonitor.Start();
}
private void Btn_ACSStop_Click(object sender, EventArgs e) //断开连接
{
if (_mBConnected) _acs.CloseComm();
tmrMonitor.Stop();
btn_ACSStart.Enabled = true;
btn_ACSStop.Enabled = false;
}
private void Btn_HexcalStart_Click(object sender, EventArgs e)
{
}
private void UpdateLimitState(int axisNo, int fault) //刷新限位
{
if (axisNo < MaxUiLimitCnt)
{
if ((fault & (int)SafetyControlMasks.ACSC_SAFETY_LL) != 0)
_mLblLeftLimit[axisNo].Image = Resources.Error;
else _mLblLeftLimit[axisNo].Image = Resources.Off;
if ((fault & (int)SafetyControlMasks.ACSC_SAFETY_RL) != 0)
_mLblRightLimit[axisNo].Image = Resources.Error;
else _mLblRightLimit[axisNo].Image = Resources.Off;
}
}
#endregion ACS平台相关
#region
private void Rtb_motion_Click(object sender, EventArgs e) //ACS调试页面
{
Motion motion = new Motion();
motion.Show();
}
private void Rtb_about_Click(object sender, EventArgs e) //关于界面
{
AboutBox mAboutBox = new AboutBox();
mAboutBox.StartPosition = FormStartPosition.CenterScreen;
mAboutBox.Show();
}
private void Timer_RefreshUI_Tick(object sender, EventArgs e) //UI刷新
{
//状态灯刷新
lamp_acs.State = _mBConnected ? LampColor.Green : LampColor.Silver;
lamp_hexcal.State = _mBHexcalConnected ? LampColor.Green : LampColor.Silver;
//时间栏
//获取当前时间,构造形如 精确到秒,例如 2023-10-08 16:01:23
rle_timer.Text = "当前时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
#endregion
}
}