文章目录
1、PLC变量类型对应C#变量
C# 基本数据类型,值类型隐式继承自System.ValueType->System.Object
基本类型占用字节
PLC变量 | C# |
---|---|
BOOL | bool |
INT | short |
UINT | ushort |
DINT | int |
UDINT(包括时间类型TIME) | uint |
REAL | float |
LREAL | double |
STRING(80) | string, new int[] { 80 } |
ARRAY[0…3] OF REAL | float[], new int[]{ 4 } |
结构体 | 结构体(考虑字节对齐问题) |
2、读写任意类型变量
2.0 官方例程
TwinCAT 3 -> TE1000 XAE -> Technologies ->ADS -> TwinCAT ADS
2.1、TcAdsClient类 ReadAny() 方法
//read
bool bTest1 = (bool)tcAdsClient.ReadAny(pHandle_bool1, typeof(Boolean));//read by handle
short nTest1 = (short)tcAdsClient.ReadAny(pHandle_int1, typeof(short));//read by handle
int dintTest1 = (int)tcAdsClient.ReadAny(pHandle_dint1, typeof(int));//read by handle
double dfTest1 = (double)tcAdsClient.ReadAny(pHandle_lreal1, typeof(double));//read by handle
string strTest1 = (string)tcAdsClient.ReadAny(pHandle_str1, typeof(string), new int[] { 11 });//read by handle
float fTest1 = (float)tcAdsClient.ReadAny(pHandle_array_real1, typeof(float));
MyStruct1 StructTest1 = (MyStruct1)tcAdsClient.ReadAny(pHandle_complexStruct1, typeof(MyStruct1))
//数组读写方式1,readany
float[] arrfTest1 = (float[])tcAdsClient.ReadAny(pHandle_array_real1, typeof(float[]), new int[] { 4 });//read by handle
Console.Write("=====结果=====:");
Console.Write("\n{0}\n{1}\n{2}\n{3}\n{4}\n", bTest1, nTest1, dintTest1, dfTest1, strTest1);
for (int i = 0; i < 4; i++){
Console.Write("{0} ",arrfTest1[i]);}
2.2、TcAdsClient类 WriteAny()方法
3、对上面ReadAny()、WriteAny()方法 做一些集成
3.1、使用泛型
/// <summary>
/// ReadAny<T> 泛型方法 实现
/// </summary>
/// <typeparam name="T">PLC数据类型,BOOL->bool, INT->short, REAL->float, LREAL->double 。</typeparam>
/// <param name="plcVariableName">plc变量名,字符串类型。</param>
/// <param name="Args">0:单个PLC变量; >0:PLC变量的字符串字符个数或数组元素个数。</param>
/// <returns>返回PLC变量值</returns>
public T ReadAny<T>(string plcVariableName, int Args = 0)
{
if (base.ClientIsConnected)
{
try
{
int varHandle = this.tcAdsClient.CreateVariableHandle(plcVariableName);
if (Args != 0)//是数组类型 或 字符串类型
{
return (T)this.tcAdsClient.ReadAny(varHandle, typeof(T), new int[] { Args });//read by handle
}
else//单个变量
{
return (T)this.tcAdsClient.ReadAny(varHandle, typeof(T));
}
}
catch (Exception)
{
return default(T);
throw;
}
}
else
{
return default(T);
//throw new Exception("base.ClientIsConnected 没有连接");
}
}
/// <summary>
/// WriteAny<T> 泛型方法 实现
/// </summary>
/// <typeparam name="T">PLC数据类型,BOOL->bool, INT->short, REAL->float, LREAL->double 。</typeparam>
/// <param name="plcVariableName">plc变量名,字符串类型。</param>
/// <param name="value">写入的值。</param>
/// <param name="Args">0:单个PLC变量; >0:PLC变量的字符串字符个数或数组元素个数。</param>
/// <returns>true:写成功。</returns>
public bool WriteAny<T>(string plcVariableName, T value, int Args = 0)
{
if (base.ClientIsConnected)
{
try
{
int varHandle = this.tcAdsClient.CreateVariableHandle(plcVariableName);
if (Args != 0)//是数组类型 或 字符串类型
{
this.tcAdsClient.WriteAny(varHandle, value, new int[] { Args });
}
else//单个变量
{
this.tcAdsClient.WriteAny(varHandle, value);
}
return true;
}
catch (Exception)
{
throw;
}
}
else
{
return false;
// throw new Exception("base.ClientIsConnected 没有连接");
}
}
3.2、使用List泛型 - 实现PLC变量的批量读写
/// <summary>
/// 批量读取 PLC变量值
/// </summary>
/// <typeparam name="T">PLC变量类型</typeparam>
/// <param name="plcVariableName">PLC 变量名</param>
/// <param name="args">0:单个变量,>0:数组/字符串型PLC变量元素个数</param>
/// <returns>.NET 批量访问变量列表</returns>
public List<T> ReadAnyBatch<T>(List<string> plcVariableName, int args = 0)
{
List<T> result = new List<T>();
try
{
foreach (var item in plcVariableName)
{
result.Add(ReadAny<T>(item, args));
}
return result;
}
catch (Exception)
{
return result;
throw;//读PLC变量失败
}
}
/// <summary>
/// 批量写
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="plcVariableName"></param>
/// <param name="value"></param>
/// <param name="args"></param>
/// <returns></returns>
public bool WriteAnyBatch<T>(List<string> plcVariableName, T value, int args = 0)
{
try
{
foreach (var item in plcVariableName)
{
if (WriteAny<T>(item, value, args) !=true)
{
return false;//写PLC变量失败
}
}
return true;
}
catch (Exception)
{
throw;
}
}