测试环境:vs2015 + TC31-Full-Setup.3.1.4022.30.exe

首先需要安装TC31-Full-Setup.3.1.4022.30.exe

本例子是用本机作测试,如果使用远程plc控制器作测试,改成实际的IP和PORT即可

添加C#窗体程序,下述是读取的结果

添加plc程序

启动plc:第一步 Active Configuration,第二步Restract TwinCat System(绿色按钮),第三步登陆

C#代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TwinCAT.Ads; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private TcAdsClient tcclient;//定义通讯协议 //定义所需变量 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;//4
public short s2;
public short dummy4;//4+2+2=8 public int s3;//8+4=12
public float s4;//12+4=16
public double s5;//16+8=24
}
private structtype structtest = new structtype(); //定义数组,含有五个元素
private short[] arraytest = new short[5]; //定义句柄变量
private int hvar = new int(); private void btnConnect_Click(object sender, EventArgs e)
{
if (tcclient==null)
{
tcclient = new TcAdsClient();
//tcclient.Connect("控制器NetID",851)
}
tcclient.Connect(851);
MessageBox.Show("连接成功!");
} private void btnDisConnect_Click(object sender, EventArgs e)
{
if (tcclient!=null)
{
tcclient.Disconnect();
}
MessageBox.Show("设备已断开!");
} public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{ } private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (tcclient!=null)
{
tcclient.Dispose();
}
} private void btnWriteBool_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)
{
MessageBox.Show(err.Message);
}
} private void btnReadBool_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)
{
MessageBox.Show(err.Message);
}
} private void btnWriteInt_Click(object sender, EventArgs e)
{
try
{
writeint = short.Parse(txtInt.Text);
hvar = tcclient.CreateVariableHandle("MAIN.IntTest");
tcclient.WriteAny(hvar, writeint);
tcclient.DeleteVariableHandle(hvar);//释放句柄
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
} private void btnReadInt_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)
{
MessageBox.Show(err.Message);
}
} private void btnWriteLong_Click(object sender, EventArgs e)
{
writelong = int.Parse(txtLong.Text); try
{
hvar = tcclient.CreateVariableHandle("MAIN.LongTest");
tcclient.WriteAny(hvar, writelong);
tcclient.DeleteVariableHandle(hvar);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
} private void btnReadLong_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)
{
MessageBox.Show(err.Message);
}
} private void btnWriteReal_Click(object sender, EventArgs e)
{ try
{
writereal = Single.Parse(txtlReal.Text);
hvar = tcclient.CreateVariableHandle("MAIN.SingleTest");
tcclient.WriteAny(hvar, writereal);
tcclient.DeleteVariableHandle(hvar);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
} private void btnReadReal_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)
{
MessageBox.Show(err.Message);
}
} private void btnWritelReal_Click(object sender, EventArgs e)
{
writelreal = double.Parse(txtlReal.Text);
try
{
hvar = tcclient.CreateVariableHandle("MAIN.DoubleTest");
tcclient.WriteAny(hvar, writelreal);
tcclient.DeleteVariableHandle(hvar);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
} private void btnReadlReal_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)
{
MessageBox.Show(err.Message);
}
} private void btnWriteString_Click(object sender, EventArgs e)
{
writestring = txtString.Text;
stringlen = writestring.Length; try
{
hvar = tcclient.CreateVariableHandle("MAIN.StringTest");
tcclient.WriteAny(hvar, writestring, new int[] { 80 });
tcclient.DeleteVariableHandle(hvar);
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
} private void btnReadString_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)
{
MessageBox.Show(err.Message);
}
} private void btnWriteStruct_Click(object sender, EventArgs e)
{
if (structtest.s1==true)
{
structtest.s1 = false;
}
else
{
structtest.s1 = true;
} try
{
//structtest.s1 = bool.Parse(txtStructBool.Text);
structtest.s2 = short.Parse(txtStructShort.Text);
structtest.s3 = int.Parse(txtStructInt.Text);
structtest.s4 = float.Parse(txtStructFloat.Text);
structtest.s5 = double.Parse(txtStructDouble.Text);
hvar = tcclient.CreateVariableHandle("MAIN.plcstruc");
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
AdsStream datastream = new AdsStream(24); //4+4+4+4+8=24
BinaryWriter binwrite = new BinaryWriter(datastream);
datastream.Position = 0;
try
{
//bool
binwrite.Write(structtest.s1);
binwrite.Write(structtest.dummy1);
binwrite.Write(structtest.dummy2);
binwrite.Write(structtest.dummy3);
//short
binwrite.Write(structtest.s2);
binwrite.Write(structtest.dummy4);
//int
binwrite.Write(structtest.s3);
//float
binwrite.Write(structtest.s4);
//double
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");
}
} private void btnReadStruct_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=24
BinaryReader 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");
}
} private void btnWriteArray_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)
{
MessageBox.Show(err.Message);
}
} private void btnReadArray_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)
{
MessageBox.Show(err.Message);
} } }
}

添加plc程序:新建TwinCAT Project工程,取名为PLCPro,然后在文件夹PLC下新添加一个取名为Test1的standard PLCProject,然后在DUTS中添加一个DUT结构体plcstructure,并添加如下内容:

TYPE plcstructure :
STRUCT
plcs1:BOOL;
dummy1:BOOL;
dummy2:BOOL;
dummy3:BOOL; plcs2:INT;
dummy4:INT; plcs3:DINT;
plcs4:REAL;
plcs5:LREAL;
END_STRUCT
END_TYPE

在POUS文件夹的Main(Pro)下面添加:

PROGRAM MAIN
VAR
BoolTest :BOOL;
IntTest:INT;
LongTest:DINT;
SingleTest:REAL;
DoubleTest:LREAL;
StringTest:STRING;
readstringlen:INT;
plcstruc :plcstructure;
plcarraytest:ARRAY[1..5] OF INT;
END_VAR

启动plc

启动后,运行C#窗体。

附源代码:链接: https://pan.baidu.com/s/1cvCcENgZ8kTURE5jmfWS0g提取码: ez3a

倍福Ads协议通信测试的更多相关文章

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

    倍福TwinCAT3上位机与PLC通信测试(ADS通信) 包含C#和C++代码 本次测试需要环境: VS2013,TwinCAT3(本人版本TC31-Full-Setup.3.1.4018.16) 代 ...

  2. C# 读写倍福plc beckhoff , 使用ADS协议实现读取plc

    本文将使用库技术来读写倍福PLC数据,使用的是基于以太网的ADS实现,不需要额外的组件,读取操作只要放到后台线程就不会卡死线程,本组件支持超级方便的高性能读写操作 github地址:https://g ...

  3. ADS协议变量配置界面

    ADS协议是倍福产品的通信协议,在双击ADS协议图标后打开如下图的配置窗口. 变量配置表中各列的含义: 报警类型: 脚本: 当设置了报类型时,在运行时如果变量值引发报警时将执行一次脚本.例如 大于报警 ...

  4. 倍福TwinCAT(贝福Beckhoff)应用教程12.3 TwinCAT控制松下伺服 NC进阶

    在前面一节,我们简单介绍了通过PLC+HMI实现完整控制松下伺服的上使能-运动,采集位置,速度等功能,这里我们会大量简化用到的贝福功能块(为了更加实用).首先依然是对单个轴的封装,我们之前的做法,例如 ...

  5. 倍福CX5120嵌入式控制器使用教程

    1.新建工程 新建TwinCAT XAE Project 2.连接设备 点击SYSTEM,再点击"Change Target..." 在弹出的"choose Targt ...

  6. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何查询某个变量的ADS地址

    鼠标放到任意一个参数上停留一段时间,会自动弹出关于该条参数的ADS信息   TC3也是一样(如果不是轴的某个参数而是PLC程序绑定的某个变量也是一样,定位到这个变量既可以看到ADS信息)       ...

  7. 倍福TwinCAT(贝福Beckhoff)基础教程6.1 TwinCAT如何与高级语言通讯

    因为使用TwinCAT的人用途不同,重视点就不同.如果用来代替传统PLC+HMI做项目的,很少会需要用到跟高级语言通讯,但是如果是用来做运动控制平台如做机器人运动控制器的,就肯定会用到.不管是否用得上 ...

  8. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-TwinCAT自带的找原点功能块MC_Home怎么用

    对于相对编码器类型轴(包括虚拟轴),可以使用贝福提供的找原点功能块MC_Home.   HomingMode是指机器在往前跑的时候(30单位/s的默认速度),当碰到阻挡,则会有一个布尔值从FALSE改 ...

  9. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-电机实际运行距离跟给定距离不一致怎么办,如何设置Scaling Factor

    有时候,让电机从0度转到绝对的360度,有时候会出现电机实际转动更多或者更少的情况.   一般是电机的编码器的Scaling Factor Numerator数值不对导致的,数值越小,则同比转过角度越 ...

随机推荐

  1. 如何将原生微信小程序页面改成原生VUE框架的H5页面

    项目背景: 公司为了实现小程序与H5页面共同覆盖,全面推广.特此想将已有的小程序进行快速改造上线(二周内),研发出H5版本.目前公司前端技术较为薄弱,现有的技术解决方案还停留在JSP. 问题: 如何将 ...

  2. gem5 使用记录, 基于理解来写个最简单的计数器程序

    学习GEM5其实是因为工作需要,主要是用来做数字电路的模型仿真的,之前用过 systemC,现在公司用的 gem5,其实本质上都是 C++只是套个不同的壳然后拿去仿真而已,SC本身就提供了时钟可以仿真 ...

  3. Linux命令之find、grep、echo、tar、whoami、uname

    1. whoami--查看当前登录的用户名 book@100ask:~/linux$ whoami book 2. echo--打印命令,配合'>'或者'>>'使用 echo 打印信 ...

  4. 论文解读(RvNN)《Rumor Detection on Twitter with Tree-structured Recursive Neural Networks》

    论文信息 论文标题:Rumor Detection on Twitter with Tree-structured Recursive Neural Networks论文作者:Jing Ma, Wei ...

  5. Rust变量用法与特征

    变量用法与特征 变量绑定 let a = "hello world"  为何不用赋值而用绑定呢(其实你也可以称之为赋值,但是绑定的含义更清晰准确)?这里就涉及 Rust 最核心的原 ...

  6. 常见Content-Type(MIME)列表

    Content-Type(MIME)用于标识发送或接收数据的类型,浏览器根据该参数来决定数据的打开方式.多用于指定一些客户端自定义的文件,以及一些媒体文件的打开方式. 文件扩展名 Content-Ty ...

  7. filebeat中增加其他参数用来区分不同的日志来源示例

    filebeat.yml配置文件参数 在source里加入了一个fields叫做"appach: true". filebeat.inputs: - type: log enabl ...

  8. 配置Pod的 /etc/hosts

    某些情况下,DNS 或者其他的域名解析方法可能不太适用,您需要配置 /etc/hosts 文件,在Linux下是比较容易做到的,在 Kubernetes 中,可以通过 Pod 定义中的 hostAli ...

  9. P2216 [HAOI2007]理想的正方形 方法记录

    [HAOI2007]理想的正方形 题目描述 有一个 \(a \times b\) 的整数组成的矩阵,现请你从中找出一个 \(n \times n\) 的正方形区域,使得该区域所有数中的最大值和最小值的 ...

  10. Spring Boot 源码学习之转载

    这次的学习,主要转载了 波波老师的笔记,后续会自己整理一份 1.Spring-Boot源码分析-源码编译:https://dpb-bobokaoya-sm.blog.csdn.net/article/ ...