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

本次测试需要环境:

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

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

 测试部分:

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

PLC程序Main程序

基本类型部分

代码:

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

结构体程序部分

代码:

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

C#程序主界面

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//引用Twincat.ads和Sustem.IO
using 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 = ;
private short readint = ; private int writelong = ;
private int readlong = ; private float writereal = ;
private float readreal = ; private double writelreal = ;
private double readlreal = ; private string writestring = "";
private string readstring = "";
private int stringlen = ; //定义结构体类型
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[]; //定义句柄变量
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();
}
//writebool
private 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)
{ } }
//readbool
private 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# short private 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)
{ }
}
//readint
private 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)
{ } }
//writelong
private 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)
{ }
}
//readlong
private 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)
{ } }
//writereal
private 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)
{ }
}
//readreal
private 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)
{ } }
//writelreal
private 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)
{ }
}
//readlreal
private 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[] { });
tcclient.DeleteVariableHandle(hvar);
}
catch (Exception err)
{ }
}
//readstring
private void button11_Click(object sender, EventArgs e)
{
try
{
hvar = tcclient.CreateVariableHandle("MAIN.StringTest");
readstring = tcclient.ReadAny(hvar, typeof(string), new int[] { }).ToString();
tcclient.DeleteVariableHandle(hvar);
label6.Text = readstring;
} catch (Exception err)
{ }
}
//writestruct
private 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(); //4+4+4+4+8=24
BinaryWriter binwrite = new BinaryWriter(datastream);
datastream.Position = ; 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 = ;
structtest.s3 = ;
structtest.s4 = ;
structtest.s5 = ; } catch (Exception err)
{
MessageBox.Show("write value error");
}
try
{
tcclient.DeleteVariableHandle(hvar);
}
catch (Exception err)
{
MessageBox.Show(" write delect hvar error");
}
}
//readstruct
private 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();//4+4+4+4+8=24
BinaryReader binread = new BinaryReader(datastream);
datastream.Position = ; 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");
} }
//writearray
private void button15_Click(object sender, EventArgs e)
{
arraytest[] = short.Parse(textBox12.Text);
arraytest[] = short.Parse(textBox13.Text);
arraytest[] = short.Parse(textBox14.Text);
arraytest[] = short.Parse(textBox15.Text);
arraytest[] = short.Parse(textBox16.Text); try
{
hvar = tcclient.CreateVariableHandle("MAIN.plcarraytest");
tcclient.WriteAny(hvar , arraytest);
tcclient.DeleteVariableHandle(hvar);
for (int i = ; i < ; i++)
{
arraytest[i] = ;
}
}
catch (Exception err)
{ } }
//readarray
private void button16_Click(object sender, EventArgs e)
{
try
{
hvar = tcclient.CreateVariableHandle("MAIN.plcarraytest");
arraytest = (short[])(tcclient.ReadAny(hvar , typeof(short[]), new int[] { }));
tcclient.DeleteVariableHandle(hvar); label12.Text = arraytest[].ToString();
label13.Text = arraytest[].ToString();
label14.Text = arraytest[].ToString();
label15.Text = arraytest[].ToString();
label16.Text = arraytest[].ToString();
}
catch (Exception err)
{ } } private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(tcclient !=null)
tcclient.Dispose();
}
}
}

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

测试结果:

上位机:

PLC程序:

PLC程序运行动图:

测试完毕!

工程源文件下载地址

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

点击下载

https://files.cnblogs.com/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个字符

例如:

介绍:

倍福TwinCAT3上位机与PLC通信测试(ADS通信) 包含C#和C++代码的更多相关文章

  1. C#上位机之—WinForm实现串口通信示例

    上位机开发常用到串口通信来控制设备,串口通信的主要参数:COM口,波特率(9600),停止位(One),数据位(8),校验位(None),括号中的是常用值,具体意思我也不太懂,会用能实现功能就行哈哈: ...

  2. 上位机开发之西门子PLC-S7通信实践

    写在前面: 就目前而言,在中国的工控市场上,西门子仍然占了很大的份额,因此对于上位机开发而言,经常会存在需要与西门子PLC进行通信的情况.然后对于西门子PLC来说,通信方式有很多,下面简单列举一下: ...

  3. 【精讲版】上位机C#/.NET与西门子PLC通信

    618来啦 亲们,腾讯课堂101机构打榜了,快来助力<新阁教育>,<免费赠送课程>! 1.手机QQ(微信请也来一遍)扫下方二维码↓,找到<新阁教育> 2.点击“支持 ...

  4. c#上位机与三菱PLC(FX3U)串口通讯

    项目中会经常用到上位机与PLC之间的串口通信,本文介绍一下C#如何编写上位机代码 与三菱FX3U进行通讯 1. 第一种方法是自己写代码实现,主要代码如下: //对PLC的Y7进行置1 byte[] Y ...

  5. 上位机开发之三菱FX3U以太网通信实践

    上次跟大家介绍了一下上位机与三菱Q系列PLC通信的案例,大家可以通过点击这篇文章:上位机开发之三菱Q系列PLC通信实践(←戳这里) 今天以三菱FX3U PLC为例,跟大家介绍一下,如何实现上位机与其之 ...

  6. C#上位机中ZedGraph控件的使用

    上位机程序控制PLC模拟量通道输出周期性正弦波信号,并采集所造成改变的模拟量输入信号,并绘制数据变化曲线. 界面如图: 最后测试效果如图: 代码: using System; using System ...

  7. 基于Arduino和python的串口通信和上位机控制

    引言 经常的时候我们要实现两个代码之间的通信,比如说两个不同不同人写的代码要对接,例如将python指令控制Arduino控件的开关,此处使用串口通信是非常方便的,下面笔者将结合自己踩过的坑来讲述下自 ...

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

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

  9. 上位机开发之三菱Q系列PLC通信实践

    经常关注我们公众号或者公开课的学员(如果还没有关注的话,左上角点击一波关注)应该知道,我们会经常使用西门子PLC,其实对于其他品牌的PLC,我们都会讲到,包括三菱.欧姆龙.基恩士.松下及国产台达.信捷 ...

随机推荐

  1. linux命令行下修改系统时间、时区

    date查看时间以及时区 图a是est时区,和HONGkong时间查了一个小时. # 保存设置$ sudo mv /etc/localtime /etc/localtime.old # 设置时区 $ ...

  2. 你真的会用Gson吗?Gson使用指南(4)

    原文出处: 怪盗kidou 注:此系列基于Gson 2.4. 本次文章的主要内容: TypeAdapter JsonSerializer与JsonDeserializer TypeAdapterFac ...

  3. OpenCV3 for python3 学习笔记2

    1.安装 如果你是第一次使用OpenCV Python开发包,想要安装OpenCV Python只要执行如下命令行即可:  pip install opencv-python   如果你还想使用Ope ...

  4. Android HandlerThread 源代码分析

    HandlerThread 简单介绍: 我们知道Thread线程是一次性消费品,当Thread线程运行完一个耗时的任务之后.线程就会被自己主动销毁了.假设此时我又有一 个耗时任务须要运行,我们不得不又 ...

  5. SublimeText SFTP连接Amazon EC2

    文章最初发表于szhshp的第三边境研究所 转载请注明 1. SublimeText SFTP连接Amazon EC2 Sublime Text 3 正式版发布了,全平台IDE果断都换了. 今天终于有 ...

  6. centos7安装配置mysql5.6

    1. 下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2. 安装mysql-co ...

  7. .Net学习资料

    1.博客系列文章 (1)设计模式 吕震宇 设计模式 张逸:晴窗笔记 Design & Pattern 梦幻Dot Net  .Net设计模式 李会军          .NET设计模式系列文章 ...

  8. C#中Post请求的两种方式发送参数链和Body的

    POST请求 有两种方式 一种是组装key=value这种参数对的方式 一种是直接把一个字符串发送过去 作为body的方式 我们在postman中可以看到 sfdsafd sdfsdfds publi ...

  9. 【C#】C#中的属性与字段

    目录结构: contents structure [+] 属性和字段的区别 无参属性 自动实现的属性 对象和集合初始化器 匿名类型 System.Tuple类型 有参属性 属性的可访问性 在这篇文章中 ...

  10. 11G新特性 -- RMAN Multisection Backups

    Oracle 11g支持以sections的方式来备份和还原数据文件.在section级别进行备份,被称作multisection backup(多段备份).section是一个数据文件中连续的块.m ...