倍福ads通讯软件_倍福TwinCAT3上位机与PLC通信测试
(ADS通信)包含C#和C++代码
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSy 引⽤Twincat.ads和Sustem.IO
usingTwinCAT.Ads;usingSystem.IO;namespacewriteread
{public partial classForm1 : Form
{publicForm1()
{
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 structstructtype
{public bools1;public booldummy1;public booldummy2;public booldummy3;public shorts2 ;public shortdummy4;public ints3
;public floats4;public doubles5 ;
}//实例化结构体
private structtype structtest =newstructtype ();//定义数组,含有五个元素
private short[] arraytest = new short[5];//定义句柄变量
private int hvar = new int();//通讯数据定义
private TcAdsClient tcclient;//定义通讯协议
private void Form1_Load(objectsender, EventArgs e)
{//通讯协议
tcclient = newTcAdsClient();//tcclient.Connect("控制器NetID",851)
tcclient.Connect(851);
}//writebool
private void button1_Click(objectsender, 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(objectsender, 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(objectsender, 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(objectsender, 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(objectsender, 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(objectsender, 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(objectsender, 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(objectsender, 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(objectsender, 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(objectsender, 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(objectsender, 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)
{
}error parse new
}//readstring
private void button11_Click(objectsender, 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)
{
}
}//writestruct
private void button13_Click(objectsender, 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=24 BinaryWriter binwrite = newBinaryWriter(datastream);
datastream.Position= 0;try{

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。