Private Declare Function FCN16_xSetReset Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByVal Bit As Long) As Long
Private Declare Function ComTrue Lib "modbus.dll" (ByVal nport As Long) As Long
Private Declare Function MyBeep Lib "modbus.dll" (ByVal SoundHz As Long) As Long
Private Declare Function CinBin Lib "modbus.dll" (ByVal CHex As Long, ByVal start As Long) As Long
其它附加函数VB有实用函数,建议用VB自带函数,如需使用声明参上面的例子给相应的函数声明;做完上述声明后,便可以在程序中使用
相关函数了。本DLL是串口通信库,通信时必须先使用ComOpen函数打开串口,在串口打开后可以有效操作相关函数,为保证通信可以在
程序运行开始时打开串口,程序退出前关闭串口。在应用程序关闭之前请务必将关闭所有串口,如您的程序串口没有关闭退出程序将抛
出异常。当出现这样的异常请更改您的程序,确保应用程序在关闭释放前关闭所打开的串口。
解决方法,在form的Unload事件中加入如下例:
If ComTrue(1)=1 then
ComClose(1)
End if
为确保Unload事件有效执行程序中不应使用“End”语句,而尽量使用“Unload”语句释放所有窗体,因使用“End”语句系统不会执行
正常的窗体释放等事件而直接退出程序,如程序中有form1,form2两个窗体,则使用下面语句:
Unload form1
Unload form2
VB会在所有窗体关闭后释放所有占用资源。上述可以详细参照DEMO程序。当然也可在使用“End”语句前将串口关闭也是可以释放串口的。
3、VC语言环境开发说明
在VC环境下将modbus.dll、serialmodbus.slip(许可文件)复制到应用程序目录下(即将上述文件与编译后的可执行文件方入同一文件夹内);
在VC中使用DLL一般都是采用动态声明的方式,函数说明中给出的是Delphi的函数原型,在VC中声明时只要注意一下类型的对应即可
Delphi中的longint类型对应VC中的Long类型、Delphi中的Pchar对应VC中的char* 类型,下面给出主要函数的声明:
在工程主文件cpp中声明一个句柄:
HINSTANCE m_handle;
用来标识导入的动态链接库。
1)、导入动态链接库,如例所示:
m_handle =:: LoadLibrary("modbus.dll");
2)、按下例说明声明相关各个函数:
typedef long (CALLBACK* pOpen)(long nport, long BaudRate, long DataBits, long Parity, long StopBits, char* User);
typedef long (CALLBACK* pClose)(long nport);
typedef char* (CALLBACK* pFCN01)(long nport, long node, long address, long Count);
typedef char* (CALLBACK* pFCN02)(long nport, long node, long address, long Count);
typedef char* (CALLBACK* pFCN03)(long nport, long node, long address, long Count);
typedef char* (CALLBACK* pFCN04)(long nport, long node, long address, long Count);
typedef long (CALLBACK* pFCN05S)(long nport, long node, long address);
typedef long (CALLBACK* pFCN05R)(long nport, long node, long address);
typedef long (CALLBACK* pFCN06)(long nport, long node, long address, char* Sendstr);
typedef long (CALLBACK* pFCN15)(long nport, long node, long address, long Count, char* Sendstr);
typedef long (CALLBACK* pFCN16)(long nport, long node, long address, long Count, char* Sendstr);
typedef long (CALLBACK* pFCN16_xSet)(long nport, long node, long address, long Bit);
typedef long (CALLBACK* pFCN16_xReset)(long nport, long node, long address, long Bit);
typedef long (CALLBACK* pFCN16_xSetReset)(long nport, long node, long address, long Bit);
typedef long (CALLBACK* pTrue)(long nport);
typedef char* (CALLBACK* pIntHex)(long Dcint,long Digits);
typedef long (CALLBACK* pHexInt)( char* CHex);
typedef long (CALLBACK* pBin)( long Chex, long Start);
typedef char* (CALLBACK* pLeft)( char* Text, long Count);
typedef char* (CALLBACK* pRight)( char* Text, long Count);
typedef char* (CALLBACK* pMid)( char* Text, long Start, long Count);
typedef long (CALLBACK* pinstr)( char* Ostr, char* Ckstr, Long Dcint);
typedef long (CALLBACK* pBeep)( long SoundHz);
3)、声明并建立动态链接库中的函数与新函数名的对应关系,如下:
pOpen Copen = (pOpen)GetProcAddress(m_handle,"ComOpen");
pClose Cclose = (pClose)GetProcAddress(m_handle,"ComClose");
pFCN01 CFCN01 = (pFCN01)GetProcAddress(m_handle,"FCN01");
pFCN02 CFCN02 = (pFCN02)GetProcAddress(m_handle,"FCN02");
pFCN03 CFCN03 = (pFCN03)GetProcAddress(m_handle,"FCN03");
pFCN04 CFCN04 = (pFCN04)GetProcAddress(m_handle,"FCN04");
pFCN05S CFCN05S = (pFCN05S)GetProcAddress(m_handle,"FCN05S");
pFCN05R CFCN05R = (pFCN05R)GetProcAddress(m_handle,"FCN05R");
pFCN15 CFCN15 = (pFCN15)GetProcAddress(m_handle,"FCN15");
pFCN16 CFCN16 = (pFCN16)GetProcAddress(m_handle,"FCN16");
pFCN16_xSet CFCN16_xSet = (pFCN16_xSet)GetProcAddress(m_handle,"FCN16_xSet");
pFCN16_xReset CFCN16_xReset = (pFCN16_xReset)GetProcAddress(m_handle,"FCN16_xReset");
pFCN16_xSetReset CFCN16_xSetReset = (pFCN16_xSetReset)GetProcAddress(m_handle,"FCN16_xSetReset");
pTrue Ctrue = (pTrue)GetProcAddress(m_handle," ComTrue");
pIntHex Cinthex = (pIntHex)GetProcAddress(m_handle," CIntToHex");
pHexInt Chexint = (pHexInt)GetProcAddress(m_handle," CHexToInt");
pBin Cbin = (pBin)GetProcAddress(m_handle," CinBin");
pLeft Cleft = (pLeft)GetProcAddress(m_handle," CLeftStr");
pRight Cright = (pRight)GetProcAddress(m_handle," CRightStr");
pMid Cmid= (pMid)GetProcAddress(m_handle," CMidStr");
pinstr Cinstr=(pinstr)GetProcAddress(m_handle," CinStr");
pTrue CBeep = (pBeep)GetProcAddress(m_handle," MyBeep");
注:双引号中为动态链接库中的函数名。
4)、接下来就可以自由使用动态链接库中的函数了,如:
Copen(参数略);
Cclose(参数略);
CFCN01(参数略);
CFCN02(参数略);
CFCN03(参数略);
CFCN04(参数略);
CFCN05S(参数略);
CFCN05R(参数略);
CFCN15(参数略);
CFCN16(参数略);
CFCN16_xSet(参数略);
CFCN16_xReset(参数略);
CFCN16_xSetReset(参数略);
Ctrue(参数略);
Cinthex(参数略);
Chexint(参数略);
Cbin(参数略);
Cleft(参数略);
Cright(参数略);
Cmid(参数略);
Cinstr(参数略);
CBeep(参数略);
注:函数中用到了char*型参数,这里介绍下char*与Cstring的相互转换的函数:
(1)char*->CString
char* sz;
CString str;
上一页 [1] [2] [3] [4] [5] [6] [7] 下一页
本文关键字:暂无联系方式PLC维护,plc技术 - PLC维护