倍福Ads协议通信测试

测试环境: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 

posted @   WellMandala  阅读(2272)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示