【CNC——第3篇】基于固高链接库用VS写一个点位运动窗体应用 (vs C# Winform编程学习)

    xiaoxiao2022-07-15  193

    资料链接:Microsoft Windows 窗体

    Windows 窗体,是一种基于 Windows 的应用程序,说白了就是给企业和最终用户使用的客户端。

    窗体的设计

    1 新建 windows窗体运用

    2 复制GTS800.cfg和gts.dll文件到/bin/debug文件夹下;复制gts.cs文件到和 Program.cs文件同级的文件夹内。 这些文件其实就是动态链接库和函数声明文件等文件,他们包含了该款控制卡配套的厂家开发的函数,用户开发就是调用这些函数。

    3 然后再在工具箱中选择 label,textbox,timer 控件放到 form1 中,并打开属性页面,将其 text 属 性改成图中所示名称,如图:

    4 依次双击所有按钮button1-6以及 timer1,就会在Form1.cs文件中自动生成其响应函数,而下一步就是自己在响应函数里编程。

    Form1.cs 文件代码

    编程时可以看编程手册,由于刚入门,现在只是验证性地看一下编程手册,知道一下为什么是这么写,在哪里可以看到这个函数,它的函数作用以及函数原型是什么,码代码Ctrl+J快捷键可以打开自动提示,能够提醒函数原型,非常防脱发。

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 点位运动1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //定义全局变量 bool flag = false; short rtn; int pos, vel; uint clk; double prfpos, prfvel, encpos, encvel; short AXIS = 1;//定义当前轴为1轴 //初始化 private void button1_Click(object sender, EventArgs e) { rtn = gts.mc.GT_Open(0, 1);//打开控制卡 rtn = gts.mc.GT_Reset();//控制卡复位 rtn = gts.mc.GT_LoadConfig("GTS800.cfg");//加载配置文件 rtn = gts.mc.GT_ClrSts(1, 8);//清除状态,从1轴开始,最多8个轴 timer1.Enabled = true;//开启定时器 } //清除状态 private void button2_Click(object sender, EventArgs e) { rtn = gts.mc.GT_ClrSts(1, 8);//清除状态 } //伺服使能 private void button3_Click(object sender, EventArgs e) { if(flag==false)//判断伺服标志位 { gts.mc.GT_AxisOn(AXIS);//开伺服 button3.Text = "伺服开启"; flag = !flag; } else if(flag==true) { gts.mc.GT_AxisOff(AXIS);//关伺服 button3.Text = "伺服关闭"; flag = !flag; } } //位置清零 private void button4_Click(object sender, EventArgs e) { gts.mc.GT_ZeroPos(1, 8);//位置清零,从1轴开始,最多8个轴 } private void button5_Click(object sender, EventArgs e) { timer1.Start();//开启定时器显示 rtn = gts.mc.GT_PrfTrap(AXIS);//设置当前轴为点位运动模式 gts.mc.TTrapPrm trapprm; //定义为点位运动结构体变量 gts.mc.GT_GetTrapPrm(AXIS, out trapprm);//读取点位模式运动参数 trapprm.acc = 0.1;//设置加速度 trapprm.dec = 0.1;//设置减速度 trapprm.smoothTime = 1;//设置平滑时间 gts.mc.GT_SetTrapPrm(AXIS, ref trapprm);//设置点位运动参数 pos += Convert.ToInt32(textBox1.Text);//获取输入框内数字 vel = Convert.ToInt32(textBox2.Text);//转换为int32位 rtn = gts.mc.GT_SetPos(AXIS, pos);//设置目标位置 rtn = gts.mc.GT_SetVel(AXIS, vel);//设置目标速度 rtn = gts.mc.GT_Update(AXIS); } //停止运动 private void button6_Click(object sender, EventArgs e) { timer1.Stop();//关闭定时器 rtn = gts.mc.GT_Stop(AXIS, 0);//停止运动 } //使用定时器显示状态 private void timer1_Tick(object sender, EventArgs e) { gts.mc.GT_GetPrfPos(AXIS, out prfpos, 1, out clk);// 读取规划位置 gts.mc.GT_GetPrfPos(AXIS, out encpos, 1, out clk);//读取实际位置 textBox3.Text = prfpos.ToString(); textBox5.Text = encpos.ToString(); gts.mc.GT_GetPrfVel(AXIS, out prfvel, 1, out clk);//读取规划速度 gts.mc.GT_GetPrfVel(AXIS, out encvel, 1, out clk);//读取实际速度 textBox4.Text = prfvel.ToString(); textBox6.Text = encvel.ToString(); } } }

    启动运行或者F5,运行效果

    多窗体

    一般来说,一个应用由一个主页面,然后按照功能进行分组,所以有种方法是运行进入主窗体,通过菜单选择功能子窗体。

    1 添加窗体,即cs文件,然后在工具栏中添加menuskrip控件,添加Picturebox导入图片,命名如下:

    2 同样双击menuskrip中的子选项就可以创建点击响应函数,我们添加代码:

    Form1 dianwei = new Form1();// 打开窗体1 dianwei.Show(); //this.Close();// 关闭此窗体

    Form0.cs文件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 点位运动1 { public partial class Form0 : Form { public Form0() { InitializeComponent(); } private void 点位运动ToolStripMenuItem_Click(object sender, EventArgs e) { Form1 dianwei = new Form1(); dianwei.Show(); //this.Close(); } } }

    改写program. cs中Application.Run(new Form0());//打开窗体0,让运行开启主窗体。

    static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form0());//打开窗体0 } }

    运行效果:

    这样子其实会无限打开,所以应该需要加个检测该窗体是否打开的判断。 如果是用Form1 f1 = new Form1(); f1.ShowDialog();,是切换到1窗体,鼠标是不能操控主窗体了;

    最新回复(0)