查看: 2425|回复: 12
收起左侧

[用户资料分享] H3U上位机读写源程序分享

LuckyEric 2021-11-23 19:27:39 | 显示全部楼层 |阅读模式
邀请回答

马上注册,享受更多特权

您需要 登录 才可以下载或查看,没有帐号?立即注册   

x
[img][
  1. const int CycleMax = 4;
  2.         private enum SoftElemType
  3.         {
  4.             //H3U
  5.             REGI_H3U_Y = 0x20,       //Y元件的定义       
  6.             REGI_H3U_X = 0x21,                //X元件的定义                                                       
  7.             REGI_H3U_S = 0x22,                //S元件的定义                               
  8.             REGI_H3U_M = 0x23,                //M元件的定义                                                       
  9.             REGI_H3U_TB = 0x24,                //T位元件的定义                               
  10.             REGI_H3U_TW = 0x25,                //T字元件的定义                               
  11.             REGI_H3U_CB = 0x26,                //C位元件的定义                               
  12.             REGI_H3U_CW = 0x27,                //C字元件的定义                               
  13.             REGI_H3U_DW = 0x28,                //D字元件的定义                               
  14.             REGI_H3U_CW2 = 0x29,            //C双字元件的定义
  15.             REGI_H3U_SM = 0x2a,                //SM
  16.             REGI_H3U_SD = 0x2b,                //
  17.             REGI_H3U_R = 0x2c                //
  18.         }
  19.         #region //标准库
  20.         [DllImport("StandardModbusApi.dll", EntryPoint = "Init_ETH_String", CallingConvention = CallingConvention.Cdecl)]
  21.         public static extern bool Init_ETH_String(string sIpAddr, int nNetId = 0, int IpPort = 502);

  22.         [DllImport("StandardModbusApi.dll", EntryPoint = "Exit_ETH", CallingConvention = CallingConvention.Cdecl)]
  23.         public static extern bool Exit_ETH(int nNetId = 0);

  24.         /******************************************************************************
  25.          1.功能描述 : 写H3u软元件
  26.          2.返 回 值 :1 成功  0 失败
  27.          3.参    数 : nNetId:网络链接编号
  28.                                   eType:软元件类型  
  29.                                           REGI_H3U_Y    = 0x20,     //Y元件的定义       
  30.                                           REGI_H3U_X    = 0x21,                //X元件的定义                                                       
  31.                                           REGI_H3U_S    = 0x22,                //S元件的定义                               
  32.                                           REGI_H3U_M    = 0x23,                //M元件的定义                                                       
  33.                                           REGI_H3U_TB   = 0x24,                //T位元件的定义                               
  34.                                           REGI_H3U_TW   = 0x25,                //T字元件的定义                               
  35.                                           REGI_H3U_CB   = 0x26,                //C位元件的定义                               
  36.                                           REGI_H3U_CW   = 0x27,                //C字元件的定义                               
  37.                                           REGI_H3U_DW   = 0x28,                //D字元件的定义                               
  38.                                           REGI_H3U_CW2  = 0x29,            //C双字元件的定义
  39.                                           REGI_H3U_SM   = 0x2a,                //SM
  40.                                           REGI_H3U_SD   = 0x2b,                //SD
  41.                                           REGI_H3U_R    = 0x2c                //SD
  42.                                   nStartAddr:软元件起始地址
  43.                                   nCount:软元件个数
  44.                                   pValue:数据缓存区
  45.          4.注意事项 : 1.x和y元件地址需为8进制; 2. 当元件位C元件双字寄存器时,每个寄存器需占4个字节的数据
  46.         ******************************************************************************/
  47.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
  48.         private static extern int H3u_Write_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
  49.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
  50.         private static extern int H3u_Write_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
  51.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
  52.         private static extern int H3u_Write_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, int[] pValue, int nNetId = 0);
  53.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
  54.         private static extern int H3u_Write_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
  55.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
  56.         private static extern int H3u_Write_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, uint[] pValue, int nNetId = 0);
  57.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
  58.         private static extern int H3u_Write_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);

  59.         /******************************************************************************
  60.         1.功能描述 : 读H3u软元件
  61.         2.返 回 值 :1 成功  0 失败
  62.         3.参    数 : nNetId:网络链接编号
  63.                      eType:软元件类型  
  64.                          REGI_H3U_Y    = 0x20,     //Y元件的定义       
  65.                          REGI_H3U_X    = 0x21,                //X元件的定义                                                       
  66.                          REGI_H3U_S    = 0x22,                //S元件的定义                               
  67.                          REGI_H3U_M    = 0x23,                //M元件的定义                                                       
  68.                          REGI_H3U_TB   = 0x24,                //T位元件的定义                               
  69.                          REGI_H3U_TW   = 0x25,                //T字元件的定义                               
  70.                          REGI_H3U_CB   = 0x26,                //C位元件的定义                               
  71.                          REGI_H3U_CW   = 0x27,                //C字元件的定义                               
  72.                          REGI_H3U_DW   = 0x28,                //D字元件的定义                               
  73.                          REGI_H3U_CW2  = 0x29,            //C双字元件的定义
  74.                          REGI_H3U_SM   = 0x2a,                //SM
  75.                          REGI_H3U_SD   = 0x2b,                //SD
  76.                          REGI_H3U_R    = 0x2c                //SD
  77.                      nStartAddr:软元件起始地址
  78.                      nCount:软元件个数
  79.                      pValue:返回数据缓存区
  80.         4.注意事项 : 1.x和y元件地址需为8进制;
  81.                      2. 当元件位C元件双字寄存器时,每个寄存器需占4个字节的数据
  82.                      3.如果是读位元件,每个位元件的值存储在一个字节中,pValue数据缓存区字节数必须是8的整数倍
  83.        ******************************************************************************/



  84.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem", CallingConvention = CallingConvention.Cdecl)]
  85.         private static extern int H3u_Read_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[] pValue, int nNetId = 0);
  86.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Int16", CallingConvention = CallingConvention.Cdecl)]
  87.         private static extern int H3u_Read_Soft_Elem_Int16(SoftElemType eType, int nStartAddr, int nCount, short[] pValue, int nNetId = 0);
  88.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Int32", CallingConvention = CallingConvention.Cdecl)]
  89.         private static extern int H3u_Read_Soft_Elem_Int32(SoftElemType eType, int nStartAddr, int nCount, int[] pValue, int nNetId = 0);
  90.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_UInt16", CallingConvention = CallingConvention.Cdecl)]
  91.         private static extern int H3u_Read_Soft_Elem_UInt16(SoftElemType eType, int nStartAddr, int nCount, ushort[] pValue, int nNetId = 0);
  92.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_UInt32", CallingConvention = CallingConvention.Cdecl)]
  93.         private static extern int H3u_Read_Soft_Elem_UInt32(SoftElemType eType, int nStartAddr, int nCount, uint[] pValue, int nNetId = 0);
  94.         [DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Float", CallingConvention = CallingConvention.Cdecl)]
  95.         private static extern int H3u_Read_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount, float[] pValue, int nNetId = 0);
  96.         #endregion
复制代码
/img]




上一篇:FX5U带5轴和两轴做插补案例
下一篇:项目管理思维导图

已有 0 人打赏作者

回复 邀请回答送花

使用道具 举报

ricardoeric 2021-11-24 14:49:34 | 显示全部楼层
你好,想问一下,你这个程序可以在x64运行吗还是说只能在32位环境下运行
回复 送花

使用道具 举报

18306202286 2021-12-3 13:31:56 | 显示全部楼层

希望楼主更新其他系列PLC的相关样例
回复 送花

使用道具 举报

zhi_good 2022-7-1 10:31:05 | 显示全部楼层
感谢分享这么好的资料
回复 送花

使用道具 举报

silky 2022-7-1 20:55:47 | 显示全部楼层
随便看看,谢谢楼主分享
回复 送花

使用道具 举报

Parker 2022-7-4 07:54:45 | 显示全部楼层
内容详细,感谢分享
回复 送花

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册   

本版积分规则

有技术问题,就上汇川技术社区

INOVANCE汇川技术 公众号

扫码下载掌上汇川APP

全国服务热线:8:30-17:30

4000-300124

苏州地址:江苏省苏州市吴中区越溪友翔路16号

深圳地址:深圳市龙华新区观澜街道高新技术产业园汇川技术总部大厦

Copyright © 2003-2100 汇川技术 Powered by Discuz! X3.4 ( 苏ICP备12002088号 )
快速回复 返回列表 返回顶部