600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 倍福TwinCAT3上位机与PLC通信测试(ADS通信) 包含C#和C++代码

倍福TwinCAT3上位机与PLC通信测试(ADS通信) 包含C#和C++代码

时间:2022-10-08 17:59:49

相关推荐

倍福TwinCAT3上位机与PLC通信测试(ADS通信) 包含C#和C++代码

倍福TwinCAT3上位机与PLC通信测试(ADS通信) 包含C#和C++代码

倍福TwinCAT3上位机与PLC通信测试(ADS通信) 包含C#和C++代码

本次测试需要环境:

VS,TwinCAT3(本人版本TC31-Full-Setup.3.1.4018.16)

代码:C#代码,PLC程序代码,C++代码(官方提供)

测试部分:

测试包含:bool类型,int类型,long类型,real类型,lreal类型,string类型,数组,以及结构体 部分测试(这里没有结构体嵌套)

PLC程序Main程序

基本类型部分

代码:

结构体程序部分

代码:

C#程序主界面

主界面代码包含按钮事件等

using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;//引用Twincat.ads和Sustem.IOusing TwinCAT.Ads;using System.IO;namespace writeread{public partial class Form1 : Form{public Form1(){InitializeComponent();}//定义所需变量private bool writebool =false;private bool readbool=false ;private short writeint = 0;private short readint = 0;private int writelong = 0;private int readlong = 0;private float writereal = 0;private float readreal = 0;private double writelreal = 0;private double readlreal = 0;private string writestring = "";private string readstring = "";private int stringlen = 0;//定义结构体类型public struct structtype{public bool s1;public bool dummy1;public bool dummy2;public bool dummy3;public short s2 ;public short dummy4;public int s3 ;public float s4;public double s5 ;}//实例化结构体private structtype structtest =new structtype ();//定义数组,含有五个元素private short[] arraytest = new short[5];//定义句柄变量private int hvar = new int();//通讯数据定义private TcAdsClient tcclient;//定义通讯协议private void Form1_Load(object sender, EventArgs e){//通讯协议tcclient = new TcAdsClient();//tcclient.Connect("控制器NetID",851)tcclient.Connect(851);}//writeboolprivate void button1_Click(object sender, EventArgs e){if (writebool == true){writebool = false;}else{writebool = true;}try{hvar = tcclient.CreateVariableHandle("MAIN.BoolTest");tcclient.WriteAny(hvar ,writebool );tcclient.DeleteVariableHandle(hvar);}catch (Exception err){}}//readboolprivate void button2_Click(object sender, EventArgs e){try{hvar = tcclient.CreateVariableHandle("MAIN.BoolTest");readbool = (bool)(tcclient.ReadAny(hvar, typeof(bool)));tcclient.DeleteVariableHandle(hvar);label1.Text = readbool.ToString();}catch (Exception err){}}//writeINt plc int 对应c# shortprivate void button4_Click(object sender, EventArgs e){writeint =short.Parse (textBox2 .Text );try{hvar = tcclient.CreateVariableHandle("MAIN.IntTest");tcclient.WriteAny(hvar, writeint);tcclient.DeleteVariableHandle(hvar);}catch (Exception err){}}//readintprivate void button3_Click(object sender, EventArgs e){try{hvar = tcclient.CreateVariableHandle("MAIN.IntTest");readint = (short)(tcclient.ReadAny(hvar, typeof(short)));tcclient.DeleteVariableHandle(hvar);label2.Text = readint.ToString();}catch (Exception err){}}//writelongprivate void button6_Click(object sender, EventArgs e){writelong = int.Parse(textBox3.Text);try{hvar = tcclient.CreateVariableHandle("MAIN.LongTest");tcclient.WriteAny(hvar, writelong);tcclient.DeleteVariableHandle(hvar);}catch (Exception err){}}//readlongprivate void button5_Click(object sender, EventArgs e){try{hvar = tcclient.CreateVariableHandle("MAIN.LongTest");readlong = (Int32)(tcclient.ReadAny(hvar , typeof(Int32)));tcclient.DeleteVariableHandle(hvar);label3.Text = readlong.ToString();}catch (Exception err){}}//writerealprivate void button8_Click(object sender, EventArgs e){writereal = Single.Parse(textBox4.Text);try{hvar = tcclient.CreateVariableHandle("MAIN.SingleTest");tcclient.WriteAny(hvar , writereal);tcclient.DeleteVariableHandle(hvar);}catch (Exception err){}}//readrealprivate void button7_Click(object sender, EventArgs e){try{hvar = tcclient.CreateVariableHandle("MAIN.SingleTest");readreal = (float)(tcclient.ReadAny(hvar , typeof(float)));tcclient.DeleteVariableHandle(hvar);label4.Text = readreal.ToString();}catch (Exception err){}}//writelrealprivate void button10_Click(object sender, EventArgs e){writelreal = double.Parse(textBox5.Text);try{hvar = tcclient.CreateVariableHandle("MAIN.DoubleTest");tcclient.WriteAny(hvar, writelreal);tcclient.DeleteVariableHandle(hvar);}catch (Exception err){}}//readlrealprivate void button9_Click(object sender, EventArgs e){try{hvar = tcclient.CreateVariableHandle("MAIN.DoubleTest");readlreal = (double)(tcclient.ReadAny(hvar , typeof(double)));tcclient.DeleteVariableHandle(hvar);label5.Text = readlreal.ToString();}catch (Exception err){}}//writestring 按照ASCII码进行读写private void button12_Click(object sender, EventArgs e){writestring = textBox6.Text;stringlen = writestring.Length;try{hvar = tcclient.CreateVariableHandle("MAIN.StringTest");tcclient.WriteAny(hvar, writestring, new int[] { 80 });tcclient.DeleteVariableHandle(hvar);}catch (Exception err){}}//readstringprivate void button11_Click(object sender, EventArgs e){try{hvar = tcclient.CreateVariableHandle("MAIN.StringTest");readstring = tcclient.ReadAny(hvar, typeof(string), new int[] { 80 }).ToString();tcclient.DeleteVariableHandle(hvar);label6.Text = readstring;}catch (Exception err){}}//writestructprivate void button13_Click(object sender, EventArgs e){if (structtest.s1 == true){structtest.s1 = false;}else{structtest.s1 = true;}// structtest.s1 = bool.Parse(textBox7.Text);structtest.s2 = short.Parse(textBox8.Text);structtest.s3 = int.Parse(textBox9.Text);structtest.s4 = float.Parse(textBox10.Text);structtest.s5 = double.Parse(textBox11.Text);try{hvar = tcclient.CreateVariableHandle("MAIN.plcstruc");}catch (Exception err){MessageBox.Show("get hvar error");}AdsStream datastream = new AdsStream(24); //4+4+4+4+8=24BinaryWriter binwrite = new BinaryWriter(datastream);datastream.Position = 0;try{binwrite.Write(structtest.s1);binwrite.Write(structtest.dummy1 );binwrite.Write(structtest.dummy2);binwrite.Write(structtest.dummy3);binwrite.Write(structtest.s2);binwrite.Write(structtest.dummy4);binwrite.Write(structtest.s3);binwrite.Write(structtest.s4);binwrite.Write(structtest.s5);tcclient.Write(hvar, datastream);structtest.s1 = false;structtest.s2 = 0;structtest.s3 = 0;structtest.s4 = 0;structtest.s5 = 0;}catch (Exception err){MessageBox.Show("write value error");}try{tcclient.DeleteVariableHandle(hvar);}catch (Exception err){MessageBox.Show(" write delect hvar error");}}//readstructprivate void button14_Click(object sender, EventArgs e){try{hvar = tcclient.CreateVariableHandle("MAIN.plcstruc");}catch (Exception err){MessageBox.Show("get hvar error");}AdsStream datastream = new AdsStream(24);//4+4+4+4+8=24BinaryReader binread = new BinaryReader(datastream);datastream.Position = 0;try{tcclient.Read(hvar, datastream);structtest.s1 = binread.ReadBoolean();structtest.dummy1 = binread.ReadBoolean();structtest.dummy2 = binread.ReadBoolean();structtest.dummy3 = binread.ReadBoolean();structtest.s2 = binread.ReadInt16();structtest.dummy4 = binread.ReadInt16();structtest.s3 = binread.ReadInt32();structtest.s4 = binread.ReadSingle();structtest.s5 = binread.ReadDouble();label7.Text = structtest.s1.ToString();label8.Text = structtest.s2.ToString();label9.Text = structtest.s3.ToString();label10.Text = structtest.s4.ToString();label11.Text = structtest.s5.ToString();}catch (Exception err){MessageBox.Show("read value error");}try{tcclient.DeleteVariableHandle(hvar);}catch (Exception err){MessageBox.Show("read delect hvar error");}}//writearrayprivate void button15_Click(object sender, EventArgs e){arraytest[0] = short.Parse(textBox12.Text);arraytest[1] = short.Parse(textBox13.Text);arraytest[2] = short.Parse(textBox14.Text);arraytest[3] = short.Parse(textBox15.Text);arraytest[4] = short.Parse(textBox16.Text);try{hvar = tcclient.CreateVariableHandle("MAIN.plcarraytest");tcclient.WriteAny(hvar , arraytest);tcclient.DeleteVariableHandle(hvar);for (int i = 0; i < 5; i++){arraytest[i] = 0;}}catch (Exception err){}}//readarrayprivate void button16_Click(object sender, EventArgs e){try{hvar = tcclient.CreateVariableHandle("MAIN.plcarraytest");arraytest = (short[])(tcclient.ReadAny(hvar , typeof(short[]), new int[] { 5 }));tcclient.DeleteVariableHandle(hvar);label12.Text = arraytest[0].ToString();label13.Text = arraytest[1].ToString();label14.Text = arraytest[2].ToString();label15.Text = arraytest[3].ToString();label16.Text = arraytest[4].ToString();}catch (Exception err){}}private void Form1_FormClosing(object sender, FormClosingEventArgs e){if(tcclient !=null)tcclient.Dispose();}}}

C++程序代码项目(注意红色框里面的路径是TwinCAT的安装路径)

测试结果:

上位机:

PLC程序:

PLC程序运行动图:

测试完毕!

工程源文件下载地址

小伙伴们直接去这里下载:

点击下载

/files/JiYF/%E5%80%8D%E7%A6%8FTwinCAT3%E4%B8%8A%E4%BD%8D%E6%9C%BA%E4%B8%8EPLC%E9%80%9A%E4%BF%A1%E6%B5%8B%E8%AF%95(ADS%E9%80%9A%E4%BF%A1)%E4%BE%8B%E5%AD%90%E4%BB%A3%E7%A0%81.rar

注意事项:

string类型如果没有限定大小,默认是80个字符,在C#这里需要加一个字符为结束符也就是限定为81个字符

例如:

介绍:

分类:08PLC相关技术

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。