|
邀请回答
马上注册,享受更多特权
您需要 登录 才可以下载或查看,没有帐号?立即注册 ![](source/plugin/zhanmishu_wechat/template/static/img/wechat_login.png)
x
/// <summary>
/// 和PLC通信异常时循环次数的最大值
/// </summary>
const int CycleMax = 4;
private enum SoftElemType
{
//H3U
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 //
}
#region //标准库
[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);
[DllImport("StandardModbusApi.dll", EntryPoint = "Exit_ETH", CallingConvention = CallingConvention.Cdecl)]
public static extern bool Exit_ETH(int nNetId = 0);
/******************************************************************************
1.功能描述 : 写H3u软元件
2.返 回 值 :1 成功 0 失败
3.参 数 : nNetId:网络链接编号
eType:软元件类型
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, //SD
REGI_H3U_R = 0x2c //SD
nStartAddr:软元件起始地址
nCount:软元件个数
pValue:数据缓存区
4.注意事项 : 1.x和y元件地址需为8进制; 2. 当元件位C元件双字寄存器时,每个寄存器需占4个字节的数据
******************************************************************************/
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, int[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, uint[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Write_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
/******************************************************************************
1.功能描述 : 读H3u软元件
2.返 回 值 :1 成功 0 失败
3.参 数 : nNetId:网络链接编号
eType:软元件类型
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, //SD
REGI_H3U_R = 0x2c //SD
nStartAddr:软元件起始地址
nCount:软元件个数
pValue:返回数据缓存区
4.注意事项 : 1.x和y元件地址需为8进制;
2. 当元件位C元件双字寄存器时,每个寄存器需占4个字节的数据
3.如果是读位元件,每个位元件的值存储在一个字节中,pValue数据缓存区字节数必须是8的整数倍
******************************************************************************/
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, int[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, uint[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
private static extern int H3u_Read_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
#endregion
/// <summary>
/// 读取32位D
/// </summary>
/// <param name="nStratAddr"></param>
/// <param name="Dx"></param>
public static bool readDoubleD(int nStratAddr,out int Dx)
{
int[] pValue = new int[1];
pValue[0] = 0;
int read_d = 0;
int nRet = 0;
do
{
nRet = H3u_Read_Soft_Elem_Int32(SoftElemType.REGI_H3U_DW, nStratAddr, 2, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
Dx = pValue[0];
return true;
}
else
{
Dx = 0;
return false;
}
}
/// <summary>
/// 读出多个依32位格式的D寄存器
/// </summary>
/// <param name="nStratAddr">地址</param>
/// <param name="Dx">返回的数据</param>
/// <param name="num">读取的个数</param>
/// <returns></returns>
public static bool readDoubleD(int nStratAddr, out int[] Dx, int num = 1)
{
int read_d = 0;
int nRet = 0;
Dx = new int[num];
int[] pValue = new int[num];
for (int i = 0; i < Dx.Length; i++)
{
Dx[i] = 0;
pValue[i] = 0;
}
do
{
nRet = H3u_Read_Soft_Elem_Int32(SoftElemType.REGI_H3U_DW, nStratAddr, num*2, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
for (int i = 0; i < Dx.Length; i++)
{
Dx[i] = pValue[i];
}
return true;
}
else
{
for (int i = 0; i < Dx.Length; i++)
{
Dx[i] = 0;
}
return false;
}
}
/// <summary>
/// 读出多个依16位格式的D寄存器
/// </summary>
/// <param name="nStratAddr">地址</param>
/// <param name="Dx">返回的数据</param>
/// <param name="num">读取的个数</param>
/// <returns></returns>
public static bool ReadD(int nStratAddr, out int[] Dx, int num = 1)
{
int read_d = 0;
int nRet = 0;
Dx = new int[num];
short[] pValue = new short[num];
for (int i = 0; i < Dx.Length; i++)
{
Dx[i]= 0;
pValue[i] = 0;
}
do
{
nRet = H3u_Read_Soft_Elem_Int16(SoftElemType.REGI_H3U_DW, nStratAddr, num, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
for (int i = 0; i < Dx.Length; i++)
{
Dx[i] = pValue[i];
}
return true;
}
else
{
for (int i = 0; i < Dx.Length; i++)
{
Dx[i] = 0;
}
return false;
}
}
/// <summary>
/// 依16位的格式读取D寄存器
/// </summary>
/// <param name="nStratAddr">地址</param>
/// <param name="Dx">返回的数据</param>
/// <returns></returns>
public static bool ReadD(int nStratAddr,out int Dx)
{
short[] pValue = new short[1];
pValue[0] = 0;
int read_d = 0;
int nRet=0;
do
{
nRet = H3u_Read_Soft_Elem_Int16(SoftElemType.REGI_H3U_DW, nStratAddr, 1, pValue);
read_d++;
} while (nRet==0 && read_d<=CycleMax);
if (read_d<=CycleMax)
{
Dx = pValue[0];
return true;
}
else
{
Dx = 0;
return false;
}
}
/// <summary>
/// 读取1个或多个X点
/// </summary>
/// <param name="nStratAddr">起始位</param>
/// <param name="Xm">X点</param>
/// <param name="num">数量</param>
/// <returns> 返回值 =1时正常</returns>
public static bool readX(int nStratAddr, out byte[] Xm,int num=1)//读取X点
{
int read_d = 0;
int nRet = 0;
Xm = new byte[num];
byte[] pValue = new byte[num];
for (int i = 0; i < Xm.Length; i++)
{
Xm[i] = 0;
pValue[i] = 0;
}
do
{
nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_X, nStratAddr, num, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
for (int i = 0; i < Xm.Length; i++)
{
Xm[i] = pValue[i];
}
return true;
}
else
{
for (int i = 0; i < Xm.Length; i++)
{
Xm[i] = 0;
}
return false;
}
}
/// <summary>
/// 读取单个X点
/// </summary>
/// <param name="nStratAddr"></param>
/// <param name="Xm"></param>
/// <returns>返回值 =1时正常</returns>
public static bool readX(int nStratAddr,out int Xm)//读取X点
{
byte[] pValue = new byte[1];
pValue[0] = 0;
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_X, nStratAddr, 1, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
Xm = pValue[0];
return true;
}
else
{
Xm = 0;
return false;
}
}
/// <summary>
/// 读取1个或多个M点
/// </summary>
/// <param name="nStratAddr">起始地址</param>
/// <param name="Mx">返回的变量</param>
/// <param name="num">读取的个数默认为1</param>
/// <returns>返回值 =1时正常</returns>
public static bool readM(int nStratAddr, out byte[] Mx,int num=1)//读取M点
{
int read_d = 0;
int nRet = 0;
Mx = new byte[num];
byte[] pValue = new byte[num];
for (int i = 0; i < Mx.Length; i++)
{
Mx[i] = 0;
pValue[i] = 0;
}
do
{
nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_M, nStratAddr, num, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
for (int i = 0; i < Mx.Length; i++)
{
Mx[i] = pValue[i];
}
return true;
}
else
{
for (int i = 0; i < Mx.Length; i++)
{
Mx[i] = 0;
}
return false;
}
}
/// <summary>
/// 读取单个X点
/// </summary>
/// <param name="nStratAddr"></param>
/// <param name="Mx"></param>
/// <returns>返回值 =1时正常</returns>
public static bool readM(int nStratAddr, out int Mx)//读取M点
{
byte[] pValue = new byte[1];
pValue[0] = 0;
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_M, nStratAddr, 1, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
Mx = pValue[0];
return true;
}
else
{
Mx = 0;
return false;
}
}
/// <summary>
/// 读1个或多个S
/// </summary>
/// <param name="nStratAddr">起始地址</param>
/// <param name="Sx">返回的数据</param>
/// <param name="num">数量</param>
/// <returns>返回值 =1时正常</returns>
public static bool readS(int nStratAddr, out byte[] Sx,int num=1)//读取S点
{
int nRet = 0;
int read_d = 0;
Sx = new byte[num];
byte[] pValue = new byte[num];
for (int i = 0; i < Sx.Length; i++)
{
Sx[i] = 0;
pValue[i] = 0;
}
do
{
nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_S, nStratAddr, num, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
for (int i = 0; i < Sx.Length; i++)
{
Sx[i] = pValue[i];
}
return true;
}
else
{
for (int i = 0; i < Sx.Length; i++)
{
Sx[i] = 0;
}
return false;
}
}
/// <summary>
/// 读取单个S点
/// </summary>
/// <param name="nStratAddr">起始地址</param>
/// <param name="Sx">返回的数据</param>
/// <returns>返回值 =1时正常</returns>
public static bool readS(int nStratAddr, out int Sx)//读取S点
{
byte[] pValue = new byte[1];
pValue[0] = 0;
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_S, nStratAddr, 1, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
Sx = pValue[0];
return true;
}
else
{
Sx = 0;
return false;
}
}
/// <summary>
/// 读取1个或多个Y
/// </summary>
/// <param name="nStratAddr">起始地址</param>
/// <param name="Yx">返回的数据</param>
/// <param name="num">数量</param>
/// <returns>返回值 =1时正常</returns>
public static bool readY(int nStratAddr, out byte[] Yx,int num=1)//读取Y点
{
int nRet = 0;
int read_d = 0;
Yx = new byte[num];
byte[] pValue = new byte[num];
for (int i = 0; i < Yx.Length; i++)
{
Yx[i] = 0;
pValue[i] = 0;
}
do
{
nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_Y, nStratAddr, num, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
for (int i = 0; i < Yx.Length; i++)
{
Yx[i] = pValue[i];
}
return true;
}
else
{
for (int i = 0; i < Yx.Length; i++)
{
Yx[i] = 0;
}
return false;
}
}
/// <summary>
/// 读取单个Y
/// </summary>
/// <param name="nStratAddr">起始地址</param>
/// <param name="Yx">返回的数据</param>
/// <returns>返回值 =1时正常</returns>
public static bool readY(int nStratAddr, out int Yx)//读取Y点
{
byte[] pValue = new byte[1];
pValue[0] = 0;
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_Y, nStratAddr, 1, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax)
{
Yx = pValue[0];
return true;
}
else
{
Yx = 0;
return false;
}
}
//public static void readFloat(int nStratAddr, out float Dx)//读取16位D
//{
// float[] pValue = new float[1];
// pValue[0] = 0;
// int nRet = 0;
// nRet= H3u_Read_Soft_Elem_Float(SoftElemType.REGI_H3U_DW, nStratAddr, 1, pValue);
// if (nRet == 1)
// {
// Dx = pValue[0];
// }
// else
// {
// Dx = pValue[0];
// MessageBox.Show("与PLC的通信出错,出错代码005");
// }
//}
public static bool writeDoubleD(int nStratAddr,int Dx)//写入32位D
{
int[] pValue = new int[1];
pValue[0] = Dx;
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Write_Soft_Elem_Int32(SoftElemType.REGI_H3U_DW, nStratAddr, 2, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax || nRet==1)
{
return true;
}
else
{
return false;
}
}
public static bool writeD(int nStratAddr, short Dx)//写入16位D
{
short[] pValue = new short[1];
pValue[0] = Dx;
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Write_Soft_Elem_Int16(SoftElemType.REGI_H3U_DW, nStratAddr, 1, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax || nRet == 1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 将值写入到M
/// </summary>
/// <param name="nStratAddr">起始地址</param>
/// <param name="On_Off">ON或OFF</param>
/// <returns>返回值 =1时正常</returns>
public static bool writeM(int nStratAddr, bool On_Off)//写入M
{
byte[] pValue = new byte[1];
if (On_Off ==true)
{
pValue[0] = 1;
}
else
{
pValue[0] = 0;
}
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Write_Soft_Elem(SoftElemType.REGI_H3U_M, nStratAddr, 1, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax || nRet == 1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 写入1个多个M
/// </summary>
/// <param name="nStratAddr">起始地址</param>
/// <param name="pValue">需要写入的数组</param>
/// <param name="num">需要写入的数量</param>
/// <returns>返回值 =1时正常</returns>
public static bool writeM(int nStratAddr, byte[] pValue, int num = 1)
{
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Write_Soft_Elem(SoftElemType.REGI_H3U_M, nStratAddr, num, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax || nRet == 1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 将值写入到X
/// </summary>
/// <param name="nStratAddr">起始地址</param>
/// <param name="On_Off">ON或OFF</param>
/// <returns>返回值 =1时正常</returns>
public static bool writeX(int nStratAddr, bool On_Off)//写入X
{
byte[] pValue = new byte[1];
if (On_Off == true)
{
pValue[0] = 1;
}
else
{
pValue[0] = 0;
}
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Write_Soft_Elem(SoftElemType.REGI_H3U_X, nStratAddr, 1, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax || nRet == 1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 将值写入到Y
/// </summary>
/// <param name="nStratAddr">起始地址</param>
/// <param name="On_Off">ON或OFF</param>
/// <returns>返回值 =1时正常</returns>
public static bool writeY(int nStratAddr, bool On_Off)//写入Y
{
byte[] pValue = new byte[1];
if (On_Off == true)
{
pValue[0] = 1;
}
else
{
pValue[0] = 0;
}
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Write_Soft_Elem(SoftElemType.REGI_H3U_Y, nStratAddr, 1, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax || nRet == 1)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 写入1个多个Y
/// </summary>
/// <param name="nStratAddr">起始地址</param>
/// <param name="pValue">需要写入的数组</param>
/// <param name="num">需要写入的数量</param>
/// <returns>返回值 =1时正常</returns>
public static bool writeY(int nStratAddr, byte[] pValue, int num=1)//写入Y
{
int nRet = 0;
short read_d = 0;
do
{
nRet = H3u_Write_Soft_Elem(SoftElemType.REGI_H3U_Y, nStratAddr, num, pValue);
read_d++;
} while (nRet == 0 && read_d <= CycleMax);
if (read_d <= CycleMax || nRet == 1)
{
return true;
}
else
{
return false;
}
}
|
上一篇: 提问:InoProShop离线仿真,监控轴当前速度,为什么波这么大下一篇: AM600 轴控学习 含程序与PPT解析
|