using System;
using System.Collections;
using System.IO;
using TwinCAT.Ads;
namespace MYLA_SJJS
{
internal class Twincat_ads
{
region PLC基本对象定义
/// <summary>
/// 创建Ads对象
/// </summary>
public TwinCAT.Ads.TcAdsClient _adsClient = new TcAdsClient();
///// <summary>
///// 定义连接PLC的AMSNetID
///// </summary>
public string string_AmsNetID = "";
///// <summary>
///// 定义连接PLC的端口号
///// TwinCAT2,端口号801;TwinCAT3,端口号为851
///// </summary>
public int iPort = 801;
////public int iPort = 851;
/// <summary>
/// PLC连接成功的标志
/// </summary>
public bool bConnectToBeckhoffOK = false;
public ArrayList notificationHandles = new ArrayList();
#endregion PLC基本对象定义
#region 函数定义
public void ADS()
{
_adsClient = new TcAdsClient();
}
/// <summary>
/// 连接到PLC
/// </summary>
public void ConnectPLC()
{
try
{
_adsClient.Connect(string_AmsNetID, iPort);
//_adsClient.Connect(iPort);
bConnectToBeckhoffOK = true;
}
catch
{
bConnectToBeckhoffOK = false;
}
}
/// <summary>
/// 读取int类型的变量,非实时刷新
/// </summary>
/// <param name="PlcVariableName"></param>
/// <returns></returns>
public int ReadInt(string PlcVariableName)
{
int dresult = 0;
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
dresult = (int)(_adsClient.ReadAny(iHandleTemp, typeof(int)));
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { return 0; }
return dresult;
}
/// <summary>
/// 读取string类型的变量,非实时刷新
/// </summary>
/// <param name="PlcVariableName"></param>
/// <returns></returns>
public char[] Readchar(string PlcVariableName)
{
char[] dresult = { };
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
dresult = (char[])(_adsClient.ReadAny(iHandleTemp, typeof(char[])));
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { }
return dresult;
}
/// <summary>
/// 读取string类型的变量,非实时刷新
/// </summary>
/// <param name="PlcVariableName"></param>
/// <returns></returns>
public string Readstring(string PlcVariableName)
{
string dresult = "";
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
dresult = _adsClient.ReadAny(iHandleTemp, typeof(string), new int[] { 80 }).ToString();
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { }
return dresult;
}
/// <summary>
/// 读取float类型的变量,非实时刷新
/// </summary>
/// <param name="PlcVariableName"></param>
/// <returns></returns>
public float Readfloat(string PlcVariableName)
{
float dresult;
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
dresult = (float)(_adsClient.ReadAny(iHandleTemp, typeof(float)));
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { return 0; }
return dresult;
}
/// <summary>
/// 读取short类型的变量,非实时刷新
/// </summary>
/// <param name="PlcVariableName"></param>
/// <returns></returns>
public short Readshort(string PlcVariableName)
{
short dresult;
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
dresult = (short)(_adsClient.ReadAny(iHandleTemp, typeof(short)));
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { return 0; }
return dresult;
}
/// <summary>
/// 读取bool类型的变量,非实时刷新
/// </summary>
/// <param name="PlcVariableName"></param>
/// <returns></returns>
public bool Readbool(string PlcVariableName)
{
bool dresult;
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
dresult = (bool)(_adsClient.ReadAny(iHandleTemp, typeof(bool)));
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { return false; }
return dresult;
}
/// <summary>
/// 根据PLC变量名,写PLC变量
/// </summary>
/// <param name="PlcVariableName"></param>
/// <param name="dresult"></param>
public void WriteVariable(string PlcVariableName, Double dresult)
{
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
_adsClient.WriteAny(iHandleTemp, dresult);
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { }
}
/// <summary>
/// 根据PLC变量名,写PLC变量
/// </summary>
/// <param name="PlcVariableName"></param>
/// <param name="dresult"></param>
public void WriteVariable(string PlcVariableName, short dresult)
{
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
_adsClient.WriteAny(iHandleTemp, dresult);
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { }
}
/// <summary>
/// 根据PLC变量名,写PLC变量
/// </summary>
/// <param name="PlcVariableName"></param>
/// <param name="dresult"></param>
public void WriteVariable(string PlcVariableName, bool dresult)
{
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
_adsClient.WriteAny(iHandleTemp, dresult);
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { }
}
/// <summary>
/// 根据PLC变量名,写PLC变量
/// </summary>
/// <param name="PlcVariableName"></param>
/// <param name="dresult"></param>
public void WriteVariable(string PlcVariableName, float dresult)
{
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
_adsClient.WriteAny(iHandleTemp, dresult);
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { }
}
/// <summary>
/// 根据PLC变量名,写PLC变量
/// </summary>
/// <param name="PlcVariableName"></param>
/// <param name="dresult"></param>
public void WriteVariable(string PlcVariableName, int dresult)
{
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
_adsClient.WriteAny(iHandleTemp, dresult);
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { }
}
/// <summary>
/// 根据PLC变量名,写PLC变量
/// </summary>
/// <param name="PlcVariableName"></param>
/// <param name="dresult"></param>
public void WriteVariable(string PlcVariableName, byte dresult)
{
try
{
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
_adsClient.WriteAny(iHandleTemp, dresult);
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch { }
}
/// <summary>
/// 根据PLC变量名,写PLC变量
/// </summary>
/// <param name="PlcVariableName"></param>
/// <param name="dresult"></param>
public void WriteVariable(string PlcVariableName, string dresult)
{
int stringlen = dresult.Length;
AdsStream datastream = new AdsStream(stringlen + 1);
BinaryWriter binwrite = new BinaryWriter(datastream, System.Text.Encoding.ASCII);
datastream.Position = 0;
try
{
binwrite.Write(dresult.ToCharArray());
binwrite.Write('\0');
int iHandleTemp = _adsClient.CreateVariableHandle(PlcVariableName);
_adsClient.Write(iHandleTemp, datastream);
_adsClient.DeleteVariableHandle(iHandleTemp);
}
catch (Exception err)
{
}
}
#endregion 函数定义
}
}
本内容主要包含对倍福PLC的各种变量读写方法,实现工控机和PLC之间数据传输
主要方法有两部:
1.建立连接,前提是工控机需要安装Twincat2或者Twincat3,并实现PLC添加工控机路由,然后在程序中通过调用连接方法进行连接,代码:
Twincat_ads twincat_Ads = new Twincat_ads(); //初始化ADS通讯类
twincat_Ads.string_AmsNetID = “192.168.1.100.1.1”; 、、
twincat_Ads.iPort = 801;
twincat_Ads.ConnectPLC(); //开始连接PLC
2.直接调用读写方法,(这里需要注意数据类型的转换)
int NewStatus = Convert.ToInt32(twincat_Ads.Readshort(".NewStatus ")); //获取int类型数据
float Number1 = Convert.ToInt16(twincat_Ads.Readshort(".Number1 ")); //获取short类型数据
float Number2 = Convert.ToInt16(twincat_Ads.Readshort(".Number2 ")); //获取short类型数据
twincat_Ads.WriteVariable(".Number3", 23.5); //写入float类型数据
twincat_Ads.WriteVariable("Number4", 50); //写入int类型数据
twincat_Ads.WriteVariable(".string1", “正常”); //写入string类型数据
twincat_Ads.WriteVariable(".bool1", true); //写入bool类型数据
|