倍福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. IIS服务命令

    : iisreset /reboot 重启win2k计算机(但有提示系统将重启信息出现) iisreset /start或stop 启动(停止)所有Internet服务 iisreset /resta ...

  2. 基于Socket网络编程

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a2011480169/article/details/73602708 博客核心内容: 1.Sock ...

  3. git的使用笔记

    1.git下载:https://git-scm.com/downloads   安装git   2.在github.com网站上注册账号 网址:https://github.com/   3.使用gi ...

  4. MongoDB 进程控制系列二:结束进程

    1:如果某个进程产生了异常,可以考虑将其kill掉 db.killOp(10417) db.killOp(10417/*opid*/) 等同于: db.$cmd.sys.killop.findOne( ...

  5. SoapUI Pro Project Solution Collection-change the JDBC Request behavior

    change the jdbc request : 1.change the driver name,connection string,query string or assert. the obj ...

  6. Python 隔离沙箱 virtualenv

    我认为Python一个很大的优势就是官方网站给出的众多的软件包,几乎能帮助你实现你想要的任何功能,避免了重复开发的劳动,但是零零碎碎的包,以及每个包的各种各样的版本管理就成为了一个比较棘手的问题,因此 ...

  7. [Aaronyang紫色博客] 写给自己的WPF4.5-Blend5公开课系列 1

     我的文章一定要做到对读者负责,否则就是失败的文章  ---------   www.ayjs.net    aaronyang技术分享 欢迎大家支持我的力作<[Aaronyang] 写给自己的 ...

  8. SED单行脚本快速参考(Unix 流编辑器)(转)

    sed.sourceforge.net被封杀,特在此处贴上官方的sed 使用说明文档 SED单行脚本快速参考(Unix 流编辑器) 2005年12月29日 英文标题:USEFUL ONE-LINE S ...

  9. linux ssh

    SSH 是建立在应用层和传输层基础上的一种安全协议. SSH传输数据是加密的,可以有效防止传输过程被截取数据保障安全. SSH的数据是经过压缩的,所以可以加快传输的速度 1. 首先查看一下当前linu ...

  10. Atitit 常见每日流程日程日常工作.docx v4

    Atitit 常见每日流程日程日常工作.docx v4 ----早晨 签到 晨会,每天或者隔天 每日计划( )项目计划,日常计划等. mailbox读取检查 每日趋势 推库 -----下午 签退 每日 ...