using System.Runtime.InteropServices;
namespace MainForm
{
class InovancePlc
{
///
/// H3U 软元件
///
public enum Soft
{
REGI_H3U_Y = 0x20, //Y元件的定义
REGI_H3U_X = 0x21, //X元件的定义
REGI_H3U_S = 0x22, //S元件的定义
REGI_H3U_M = 0x23, //M元件的定义
REGI_H3U_TB = 0x24, //T位元件的定义
REGI_H3U_TW = 0x25, //T字元件的定义
REGI_H3U_CB = 0x26, //C位元件的定义
REGI_H3U_CW = 0x27, //C字元件的定义
REGI_H3U_DW = 0x28, //D字元件的定义
REGI_H3U_CW2 = 0x29, //C双字元件的定义
REGI_H3U_SM = 0x2a, //SM
REGI_H3U_SD = 0x2b, //
REGI_H3U_R = 0x2c, //
//H5u
REGI_H5U_Y = 0x30, //Y元件的定义
REGI_H5U_X = 0x31, //X元件的定义
REGI_H5U_S = 0x32, //S元件的定义
REGI_H5U_M = 0x33, //M元件的定义
REGI_H5U_B = 0x34, //B元件的定义
REGI_H5U_D = 0x35, //D字元件的定义
REGI_H5U_R = 0x36, //R字元件的定义
}
#region 声明
///
/// 创建网络连接
///
/// 以太网IP地址
/// 网络链接编号,用于标记是第几条网络链接,取值范围0~255,默认0
/// 以太网端口号,默认502(modbusTcp协议默认端口号为502)
/// TRUE 成功 FALSE 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "Init_ETH_String", CallingConvention = CallingConvention.Cdecl)]
public static extern bool Init_ETH_String(string sIpAddr, int nNetId = 0, int IpPort = 502);
///
/// 关闭网络连接
///
/// 网络链接编号,与Init_ETH()调用的ID一样
/// TRUE 成功 FALSE 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "Exit_ETH", CallingConvention = CallingConvention.Cdecl)]
private static extern bool Exit_ETH(int nNetId = 0);
///
/// 写入位状态(X,Y,M,C,T等软元件)
/// 注意 : X和Y元件地址需为8进制;
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始位置
/// 软元件个数
/// 写入的数据数组,byte=0关闭软元件,byte=1打开软元件
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_Bit(Soft eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
///
/// 写入16位有符号整形
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始位置
/// 软元件个数
/// 写入的数据数组
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_Int16(Soft eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
///
/// 写入32位有符号整形
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始位置
/// 软元件个数
/// 写入的数据数组
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_Int32(Soft eType, int nStartAddr, int nCount, int[] pValue, int nNetId = 0);
///
/// 写入16位无符号整形
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始位置
/// 软元件个数
/// 写入的数据数组
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_UInt16(Soft eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
///
/// 写入32位无符号整形
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始位置
/// 软元件个数
/// 写入的数据数组
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_UInt32(Soft eType, int nStartAddr, int nCount, uint[] pValue, int nNetId = 0);
///
/// 写入32位浮点数
///
///
///
///
///
///
///
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_Float(Soft eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
///
/// 读取位状态(X,Y,M,C,T等软元件)
/// 注意 : X和Y元件地址需为8进制;
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始地址
/// 软元件个数
/// 读取的软元件状态数组,byte=0为关闭,byte=1为打开
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_Bit(Soft eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
///
/// 读取16位有符号整形
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始地址
/// 软元件个数
/// 读取的数据数组
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_Int16(Soft eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
///
/// 读取32位有符号整形
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始地址
/// 软元件个数
/// 读取的数据数组
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_Int32(Soft eType, int nStartAddr, int nCount, int[] pValue, int nNetId = 0);
///
/// 读取16位无符号整形
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始地址
/// 软元件个数
/// 读取的数据数组
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_UInt16(Soft eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
///
/// 读取32位无符号整形
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始地址
/// 软元件个数
/// 读取的数据数组
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_UInt32(Soft eType, int nStartAddr, int nCount, uint[] pValue, int nNetId = 0);
///
/// 读取32位浮点数
///
/// 软元件类型,使用SoftElemType枚举
/// 软元件起始地址
/// 软元件个数
/// 读取的数据数组
/// 网络链接编号,与Init_ETH()调用的ID一样
/// 1 成功 0 失败
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_Float(Soft eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Write_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
public static extern int H5u_Write_Soft_Elem(Soft eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Read_Device_Block", CallingConvention = CallingConvention.Cdecl)]
public static extern int H5u_Read_Device_Block(Soft eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
#endregion
#region 字段
// PLC 编号
private int _card = 0;
///
/// 获取或设置PLC的编号
///
public int Card
{
get { return _card; }
set { _card = value; }
}
// PLC IP地址
private string _ip = "192.168.1.89";
///
/// 获取或设置PLC的IP地址
///
public string IP
{
get { return _ip; }
set { _ip = value; }
}
// PLC 端口号
private int _port = 502;
///
/// 获取或设置PLC的端口号
///
public int Port
{
get { return _port; }
set { _port = value; }
}
// 打开状态
private bool _openStatus = false;
///
/// 获取PLC的打开状态
///
public bool OpenStatus
{
get { return _openStatus; }
}
#endregion
#region 公有字段
#endregion
///
/// 构造函数,设置PLC的编号、IP、端口属性
///
/// PLC 编号
/// PLC IP地址
/// PLC 端口号
public InovancePlc(int card, string ip, int port)
{
this._card = card;
this._ip = ip;
this._port = port;
}
#region 封装
///
/// 打开连接
///
/// TRUE 成功 FALSE 失败
public bool Open()
{
if (!_openStatus)
{
_openStatus = Init_ETH_String(_ip, _card, _port);
return _openStatus;
}
else
return true;
}
///
/// 关闭连接
///
/// TRUE 成功 FALSE 失败
public bool Close()
{
_openStatus = false;
return Exit_ETH(_card);
}
///
/// 写入位状态(位寄存器)
/// 注意 : X和Y元件地址需为8进制
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 写入的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Write_Bit(Soft softName, int softNum, int softCount, bool[] writeData)
{
byte[] data = new byte[writeData.Length];
for (int i = 0; i < writeData.Length; i++)
{
data[i] = writeData[i] == true ? (byte)1 : (byte)0;
}
int rtn = H3u_Write_Soft_Elem_Bit(softName, softNum, softCount, data, _card);
return rtn == 1 ? true : false;
}
///
/// 写入16位有符号整形
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 写入的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Write_Int16(Soft softName, int softNum, int softCount, short[] writeData)
{
int rtn = H3u_Write_Soft_Elem_Int16(softName, softNum, softCount, writeData, _card);
return rtn == 1 ? true : false;
}
///
/// 写入16位无符号整形
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 写入的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Write_UInt16(Soft softName, int softNum, int softCount, ushort[] writeData)
{
int rtn = H3u_Write_Soft_Elem_UInt16(softName, softNum, softCount, writeData, _card);
return rtn == 1 ? true : false;
}
///
/// 写入32位有符号整形
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 写入的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Write_Int32(Soft softName, int softNum, int softCount, int[] writeData)
{
int count = softCount * 2;
int rtn = H3u_Write_Soft_Elem_Int32(softName, softNum, count, writeData, _card);
return rtn == 1 ? true : false;
}
///
/// 写入32位无符号整形
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 写入的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Write_UInt32(Soft softName, int softNum, int softCount, uint[] writeData)
{
int count = softCount * 2;
int rtn = H3u_Write_Soft_Elem_UInt32(softName, softNum, count, writeData, _card);
return rtn == 1 ? true : false;
}
///
/// 写入32位浮点型
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 写入的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Write_Float(Soft softName, int softNum, int softCount, float[] writeData)
{
int count = softCount * 2;
int rtn = H3u_Write_Soft_Elem_Float(softName, softNum, count, writeData, _card);
return rtn == 1 ? true : false;
}
///
/// 读取位状态(位寄存器)
/// 注意 : X和Y元件地址需为8进制
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 读取的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Read_Bit(Soft softName, int softNum, int softCount, ref bool[] readData)
{
byte[] data = new byte[readData.Length];
int rtn = H3u_Read_Soft_Elem_Bit(softName, softNum, softCount, data, _card);
for (int i = 0; i < readData.Length; i++)
{
readData[i] = data[i] == 1 ? true : false;
}
return rtn == 1 ? true : false;
}
///
/// 读取16位有符号整形
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 读取的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Read_Int16(Soft softName, int softNum, int softCount, ref short[] readData)
{
int rtn = H3u_Read_Soft_Elem_Int16(softName, softNum, softCount, readData, _card);
return rtn == 1 ? true : false;
}
///
/// 读取16位无符号整形
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 读取的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Read_UInt16(Soft softName, int softNum, int softCount, ref ushort[] readData)
{
int rtn = H3u_Read_Soft_Elem_UInt16(softName, softNum, softCount, readData, _card);
return rtn == 1 ? true : false;
}
///
/// 读取32位有符号整形
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 读取的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Read_Int32(Soft softName, int softNum, int softCount, ref int[] readData)
{
int count = softCount * 2;
int rtn = H3u_Read_Soft_Elem_Int32(softName, softNum, count, readData, _card);
return rtn == 1 ? true : false;
}
///
/// 读取32位无符号整形
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 读取的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Read_UInt32(Soft softName, int softNum, int softCount, ref uint[] readData)
{
int count = softCount * 2;
int rtn = H3u_Read_Soft_Elem_UInt32(softName, softNum, count, readData, _card);
return rtn == 1 ? true : false;
}
///
/// 读取32位有符号浮点型
///
/// 软元件类型
/// 起始软元件编号
/// 软元件数量
/// 读取的数据
/// TRUE 成功 FALSE 失败
public bool H3u_Read_Float(Soft softName, int softNum, int softCount, ref float[] readData)
{
int count = softCount * 2;
int rtn = H3u_Read_Soft_Elem_Float(softName, softNum, count, readData, _card);
return rtn == 1 ? true : false;
}
#endregion
}
}