C#编写上位机驱动运动控制板卡
Posted 虎鲸不是鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#编写上位机驱动运动控制板卡相关的知识,希望对你有一定的参考价值。
C#编写上位机驱动运动控制板卡
项目概述
笔者曾自费1200软妹币购置了一款运动控制板卡,利用下班时间琢磨出上位机,用以取代传统的PLC控制系统。IO卡及运动卡均可走RJ45水晶头串联,可级联8块,最多4096IO点及256轴联动,延时2ms以内。某些型号具备脉冲控制、编码盘伺服控制、加加速度扭矩控制,性能及性价比都不逊色于PLC。笔者已完成了部分功能的编写。系虎彩全厂首次尝试不含PLC的运动控制系统。
控制原理
安装原厂驱动后,可以使用SDK包进行开发。笔者选用了RJ45直连的方式,较走交换机的TCP/IP模式延时更短,可靠性更好。经万用表测试为NPN型。
使用脉冲控制步进驱动器。伺服控制要复杂一些。
功能实现
上位机界面
使用VS2019,Win Form写出界面。
先用AI绘制个图放右下角。。。希望虎彩自动化水平越来越高。。。
主控界面用于调用其它功能模块。
先连接板卡,之后可以测试各种功能。使用完后断开。
通讯连接模块
此处需加以限定。
#region 下拉框有效性判断
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
//MessageBox.Show("xx");
if (this.comboBox1.SelectedIndex == 0)
comboBox2.Enabled = true;
comboBox3.Enabled = false;
comboBox4.Enabled = false;
comboBox5.Enabled = false;
comboBox6.Enabled = false;
comboBox7.Enabled = false;
comboBox8.Enabled = false;
comboBox9.Enabled = false;
else if (this.comboBox1.SelectedIndex == 1)
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = false;
comboBox5.Enabled = false;
comboBox6.Enabled = false;
comboBox7.Enabled = false;
comboBox8.Enabled = false;
comboBox9.Enabled = false;
else if (this.comboBox1.SelectedIndex == 2)
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
comboBox5.Enabled = false;
comboBox6.Enabled = false;
comboBox7.Enabled = false;
comboBox8.Enabled = false;
comboBox9.Enabled = false;
else if (this.comboBox1.SelectedIndex == 3)
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
comboBox5.Enabled = true;
comboBox6.Enabled = false;
comboBox7.Enabled = false;
comboBox8.Enabled = false;
comboBox9.Enabled = false;
else if (this.comboBox1.SelectedIndex == 4)
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
comboBox5.Enabled = true;
comboBox6.Enabled = true;
comboBox7.Enabled = false;
comboBox8.Enabled = false;
comboBox9.Enabled = false;
else if (this.comboBox1.SelectedIndex == 5)
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
comboBox5.Enabled = true;
comboBox6.Enabled = true;
comboBox7.Enabled = true;
comboBox8.Enabled = false;
comboBox9.Enabled = false;
else if (this.comboBox1.SelectedIndex == 6)
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
comboBox5.Enabled = true;
comboBox6.Enabled = true;
comboBox7.Enabled = true;
comboBox8.Enabled = true;
comboBox9.Enabled = false;
else if (this.comboBox1.SelectedIndex == 7)
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
comboBox5.Enabled = true;
comboBox6.Enabled = true;
comboBox7.Enabled = true;
comboBox8.Enabled = true;
comboBox9.Enabled = true;
#endregion
连接板卡。
private void button1_Click(object sender, EventArgs e)
short rtn = 0;
ushort Conenection_Number = 0;
ushort[] Station_Number = 0, 1, 2, 3, 4, 5, 6, 7 ;
ushort[] Station_Type = 0, 0, 0, 0, 0, 0, 0, 0 ;
Conenection_Number = (ushort)(this.comboBox1.SelectedIndex + 1);
ushort.TryParse(comboBox2.Text, out Station_Type[0]);
ushort.TryParse(comboBox3.Text, out Station_Type[1]);
ushort.TryParse(comboBox4.Text, out Station_Type[2]);
ushort.TryParse(comboBox5.Text, out Station_Type[3]);
ushort.TryParse(comboBox6.Text, out Station_Type[4]);
ushort.TryParse(comboBox7.Text, out Station_Type[5]);
ushort.TryParse(comboBox8.Text, out Station_Type[6]);
ushort.TryParse(comboBox9.Text, out Station_Type[7]);
rtn = CMCDLL_NET.MCF_Open_Net(Conenection_Number, ref Station_Number[0], ref Station_Type[0]);
if (rtn == 0)
this.Close();
MessageBox.Show("连接成功");
else
MessageBox.Show("打开卡失败!!! 检查硬件是否接好。");
断开连接模块
#region 断开运动控制卡
private void button2_Click(object sender, EventArgs e)
short rtn;
rtn = CMCDLL_NET.MCF_Close_Net();
MessageBox.Show("断开成功");
#endregion
刷新输入模块
public ChangeInput()
InitializeComponent();
Image Image1;
Image Image2;
this.comboBox1.SelectedIndex = 0;
//ushort StationNumber = 0;
timer1.Enabled = true;
#region 初始化图形显示
Image1 = new Bitmap(@"E:\\work\\技术攻关\\C#运动控制卡\\练习程序\\2020.11.15\\运动控制卡~练习\\Resources\\灯-绿.png");//绿色代表接通
Image2 = new Bitmap(@"E:\\work\\技术攻关\\C#运动控制卡\\练习程序\\2020.11.15\\运动控制卡~练习\\Resources\\灯-红.png");//红色代表断开
pictureBox1.Image = Image2;
pictureBox2.Image = Image2;
pictureBox3.Image = Image2;
pictureBox4.Image = Image2;
pictureBox5.Image = Image2;
pictureBox6.Image = Image2;
pictureBox7.Image = Image2;
pictureBox8.Image = Image2;
pictureBox9.Image = Image2;
pictureBox10.Image = Image2;
pictureBox11.Image = Image2;
pictureBox12.Image = Image2;
pictureBox13.Image = Image2;
pictureBox14.Image = Image2;
pictureBox15.Image = Image2;
pictureBox16.Image = Image2;
pictureBox17.Image = Image2;
pictureBox18.Image = Image2;
pictureBox19.Image = Image2;
pictureBox20.Image = Image2;
pictureBox21.Image = Image2;
pictureBox22.Image = Image2;
pictureBox23.Image = Image2;
pictureBox24.Image = Image2;
pictureBox25.Image = Image2;
pictureBox26.Image = Image2;
pictureBox27.Image = Image2;
pictureBox28.Image = Image2;
pictureBox29.Image = Image2;
pictureBox30.Image = Image2;
pictureBox31.Image = Image2;
pictureBox32.Image = Image2;
#endregion
插入图片。
private void timer1_Tick(object sender, EventArgs e)
short rtn;
ushort i = 0;
ushort[] Input = new ushort[16];
Image Image1;
Image Image2;
Image1 = new Bitmap(@"E:\\work\\技术攻关\\C#运动控制卡\\练习程序\\2020.11.15\\运动控制卡~练习\\Resources\\灯-绿.png");//绿色代表接通
Image2 = new Bitmap(@"E:\\work\\技术攻关\\C#运动控制卡\\练习程序\\2020.11.15\\运动控制卡~练习\\Resources\\灯-红.png");//红色代表断开
#region 刷新输入00~15共计16个点的状态显示
for (i = 0; i < 16; i++)
rtn = CMCDLL_NET.MCF_Get_Input_Bit_Net(i, ref Input[i], 0);
if (i == 0)
if (Input[i] == 0)
pictureBox1.Image = Image1;
else
pictureBox1.Image = Image2;
else if (i == 1)
if (Input[i] == 0)
pictureBox2.Image = Image1;
else
pictureBox2.Image = Image2;
else if (i == 2)
if (Input[i] == 0)
pictureBox3.Image = (Image)Image1;
else
pictureBox3.Image = (Image)Image2;
else if (i == 3)
if (Input[i] == 0)
pictureBox4.Image = (Image)Image1;
else
pictureBox4.Image = (Image)Image2;
else if (i == 4)
if (Input[i] == 0)
pictureBox8.Image = (Image)Image1;
else
pictureBox8.Image = (Image)Image2;
else if (i == 5)
if (Input[i] == 0)
pictureBox7.Image = (Image)Image1;
else
pictureBox7.Image = (Image)Image2;
else if (i == 6)
if (Input[i] == 0)
pictureBox6.Image = (Image)Image1;
else
pictureBox6.Image = (Image)Image2;
else if (i == 7)
if (Input[i] == 0)
pictureBox5.Image = (Image)Image1;
else
pictureBox5.Image = (Image)Image2;
else if (i == 8)
if (Input[i] == 0)
pictureBox12.Image = (Image)Image1;
else
pictureBox12.Image = (Image)Image2;
else if (i == 9)
if (Input[i] == 0)
pictureBox11.Image = (Image)Image1;
else
pictureBox11.Image = (Image)Image2;
else if (i == 10)
if (Input[i] == 0)
pictureBox10.Image = (Image)Image1;
else
pictureBox10.Image = (Image)Image2;
else if (i == 11)
if (Input[i] == 0)
pictureBox9.Image = (Image)Image1;
else
pictureBox9.Image = (Image)Image2;
else if (i == 12)
if (Input[i] == 0)
pictureBox16.Image = (Image)Image1;
else
pictureBox16.Image = (Image)Image2;
else if (i == 13)
if (Input[i] == 0)
pictureBox15.Image = (Image)Image1;
else
pictureBox15.Image = (Image)Image2;
else if (i == 14)
if (Input[i] == 0)
pictureBox14.Image = (Image)Image1;
以上是关于C#编写上位机驱动运动控制板卡的主要内容,如果未能解决你的问题,请参考以下文章
想用c#编写上位机,是否要学wpf的编程?有没有好的教材推荐?