开发流程:
1、打开Visual Studio,新建项目,选择Windows窗体应用。
2、点击下一步,设定项目名称和项目存放位置。
3、点击下一步,选择框架,然后点击创建,新项目就创建成功了。
界面和具体代码如下
using System.Text;
using static cszmcaux.zmcaux;
namespace JOG
{
public partial class Form1 : Form
{
private IntPtr handle;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
/// <summary>
/// 初始化控制器
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
//获取规划轴号
var axis = GetPrfAxis();
//设置正限位映射的IO口
ZAux_Direct_SetFwdIn(handle, axis, 2);
//设置负限位映射的IO口
ZAux_Direct_SetRevIn(handle, axis, 0);
//设置原点映射的IO口
ZAux_Direct_SetDatumIn(handle, axis, 1);
}
/// <summary>
/// 获取单轴运动规划轴号
/// </summary>
/// <returns></returns>
private int GetPrfAxis()
{
int result = -1;
if (rad_X.Checked)
{
result = 0;
}
if (rad_Y.Checked)
{
result = 1;
}
if (rad_Z.Checked)
{
result = 2;
}
if (rad_U.Checked)
{
result = 3;
}
return result;
}
/// <summary>
/// 通过扫描获取当前网段的所有IP,并添加到下拉列表框中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Btn_ScanIP_Click(object sender, EventArgs e)
{
//定义存储扫描的IP组成字符串缓存对象
var buffer = new StringBuilder();
//搜索当前网段下控制器的IP
int res = ZAux_SearchEthlist(buffer, 1024, 1000);
if (res != 0)
{
MessageBox.Show("扫描IP地址失败");
return;
}
//根据空格分隔出IP地址组成的数组
var ips = buffer.ToString().Split(" ");
//添加仿真环境的IP地址到下拉列表框中
cmblps.Items.Add("127.0.0.1");
//循环把获取的IP列表添加到下拉列表框中
for (int i = 0; i < ips.Length - 1; i++)
{
cmblps.Items.Add(ips[i]);
}
//默认选择下拉列表框中第一个项
cmblps.SelectedIndex = 0;
}
/// <summary>
/// 连接与断开控制器的功能
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
//获取按钮的名称
var btnContent = Btn_connect.Text;
//判断是断开还是连接
if (object.Equals(btnContent, "连接"))
{
//获取连接的IP地址
var ip = cmblps.Text;
//调用方法连接控制器
int res = ZAux_OpenEth(ip, out handle);
//判断是否连接成功
if (res != 0)
{
MessageBox.Show("连接失败,请检查IP地址是否正确");
return;
}
//修改按钮的显示字符串
Btn_connect.Text = "断开";
//修改LED灯的颜色
LED.BackColor = Color.LimeGreen;
}
else //执行断开连接的操作
{
//判断连接的句柄是否为空
if (handle != IntPtr.Zero)
{
//关闭控制器连接
int res = ZAux_Close(handle);
if (res == 0)
{
//修改按钮的显示字符串
Btn_connect.Text = "连接";
//修改LED灯的颜色
LED.BackColor = Color.Gray;
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Btn_Right_MouseDown(object sender, MouseEventArgs e)
{
//获取规划的轴号
var axis = GetPrfAxis();
//设置轴类型
ZAux_Direct_SetAtype(handle, axis, 1);
//设置运动曲线,起始速度 运行速度 加速度 减速度 S曲线 脉冲当量
ZAux_Direct_SetLspeed(handle, axis, Convert.ToSingle(txt_Lspeed.Text));
ZAux_Direct_SetSpeed(handle, axis, Convert.ToSingle(txtSpeed.Text));
ZAux_Direct_SetAccel(handle, axis, Convert.ToSingle(txtAcc.Text));
ZAux_Direct_SetDecel(handle, axis, Convert.ToSingle(txtDeccl.Text));
ZAux_Direct_SetSramp(handle, axis, Convert.ToSingle(txt_Sramp.Text));
ZAux_Direct_SetUnits(handle, axis, Convert.ToSingle(txtUnits.Text));
//启动连续启动
ZAux_Direct_Single_Vmove(handle, axis, 1);
}
private void Btn_Right_MouseUp(object sender, MouseEventArgs e)
{
}
private void Btn_Left_MouseDown(object sender, MouseEventArgs e)
{
//获取规划的轴号
var axis = GetPrfAxis();
//设置轴类型
ZAux_Direct_SetAtype(handle, axis, 1);
//设置运动曲线,起始速度 运行速度 加速度 减速度 S曲线 脉冲当量
ZAux_Direct_SetLspeed(handle,axis,Convert.ToSingle(txt_Lspeed.Text));
ZAux_Direct_SetSpeed(handle, axis, Convert.ToSingle(txtSpeed.Text));
ZAux_Direct_SetAccel(handle, axis, Convert.ToSingle(txtAcc.Text));
ZAux_Direct_SetDecel(handle, axis, Convert.ToSingle(txtDeccl.Text));
ZAux_Direct_SetSramp(handle, axis, Convert.ToSingle(txt_Sramp.Text));
ZAux_Direct_SetUnits(handle, axis, Convert.ToSingle(txtUnits.Text));
//启动连续启动
ZAux_Direct_Single_Vmove(handle,axis,-1);
}
private void Btn_Left_MouseUp(object sender, MouseEventArgs e)
{
//获取规划的轴号
var axis=GetPrfAxis();
//停止规划轴对应运动
if (handle!=IntPtr.Zero)
{
int res=ZAux_Direct_Single_Cancel(handle,axis,2);
if (res == 0)
{
MessageBox.Show($"{axis}轴的运动停止成功");
}
}
}
}
}