1、Delphi语言环境开发说明
在Delphi环境下将modbus.dll、serialmodbus.slip(许可文件)复制到应用程序目录下(即将上述文件与编译后的可执行文件方入同一文件内);
在工程文件的主程序窗体(pas)文件中声明:
Function ComOpen(nport,BaudRate,DataBits,Parity,StopBits:longint;User:Pchar):longint;stdcall;External‘modbus.dll‘;
Function ComClose(nport:longint):longint;stdcall;External‘modbus.dll‘;
Function FCN01(nport,node,address,Count:longint):Pchar;stdcall;External‘modbus.dll‘;
Function FCN02(nport,node,address,Count:longint):Pchar;stdcall;External‘modbus.dll‘;
Function FCN03(nport,node,address,Count:longint):Pchar;stdcall;External‘modbus.dll‘;
Function FCN04(nport,node,address,Count:longint):Pchar;stdcall;External‘modbus.dll‘;
Function FCN05S(nport,node,address:longint):longint;stdcall;External‘modbus.dll‘;
Function FCN05R(nport,node,address:longint):longint;stdcall;External‘modbus.dll‘;
Function FCN06(nport,node,address:longint;Sendstr:pchar):longint;stdcall;External‘modbus.dll‘;
Function FCN15(nport,node,address,Count:longint;Sendstr:pchar):longint;stdcall;External‘modbus.dll‘;
Function FCN16(nport,node,address,Count:longint;Sendstr:pchar):longint;stdcall;External‘modbus.dll‘;
Function FCN16_xSet(nport,node,address,Bit:longint):longint;stdcall;External‘modbus.dll‘;
Function FCN16_xReset(nport,node,address,Bit:longint):longint;stdcall;External‘modbus.dll‘;
Function FCN16_xSetReset(nport,node,address,Bit:longint):longint;stdcall;External‘modbus.dll‘;
Function ComTrue(nport:longint):longint;stdcall;External‘modbus.dll‘;
Function MyBeep(SoundHz:longint):longint;stdcall;External‘modbus.dll‘;
其它附加函数Delphi有实用函数,建议用Delphi自带函数,如需使用声明参照上例;
注:所有DLL的函数必须声明方能使用,建议本DLL的声明采用示例所示的静态声明方式。 声明后可以在程序中使用这些函数,附加函数
中除ComTrue、CinBin、MyBeep等Delphi系统自带有类似功能函数。通信时必须先使用ComOpen函数打开串口,在串口打开后可以有效操作
相关函数,为确保通信可在程序运行开始时打开串口,程序退出前关闭串口。应用程序退出之前请务必将关闭所有串口,如串口没有关闭
而退出程序将抛出异常错误。
确保应用程序在关闭释放前关闭打开的串口。解决方法,在form的OnDestroy事件中加入如下语句:
for i:=1 to 8 do
begin
if ComTrue(i)=1 then ComClose(i);
end;
在Delphi中给中给DLL中的函数传pchar值问题,建议string型转换到Pchar型采用strPCopy()函数,Pchar型转换到string型采用straps()函数,
不推荐使用直接转换法即string到Pchar类型采用Mpchar=Pchar(str),Pchar到string采用str:=Mpchar (str为string类型、Mpchar为Pchar
类型)。上述可以详细参照DEMO程序。
2、VB语言环境开发说明
在VB环境下将modbus.dll、serialmodbus.slip(许可文件)复制到应用程序目录下(即将上述文件与编译后的可执行文件方入同一文件夹内);
函数说明中给出的是Delphi的函数原型,在VB中声明时只要注意一下类型的对应即可Delphi中的longint类型对应VB中的Long类型、
Delphi中的Pchar对应VB中的String类型,下面给出主要函数的声明:
PrivatedeclareFunction ComOpen Lib "modbus.dll" (ByVal nport As Long, ByVal BaudRate As Long, ByVal DataBits As Long, ByVal Parity As Long, ByVal StopBits As Long, ByVal User As String) As Long
PrivatedeclareFunction ComClose Lib "modbus.dll" (ByVal nport As Long) As Long
PrivatedeclareFunction FCN01 Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByValcountAs Long) As String
PrivatedeclareFunction FCN02 Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByValcountAs Long) As String
PrivatedeclareFunction FCN03 Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByValcountAs Long) As String
PrivatedeclareFunction FCN04 Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByValcountAs Long) As String
PrivatedeclareFunction FCN05S Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long) As Long
PrivatedeclareFunction FCN05R Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long) As Long
PrivatedeclareFunction FCN06 Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByVal Sendstr As String) As Long
PrivatedeclareFunction FCN15 Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByValcountAs Long, ByVal Sendstr As String) As Long
PrivatedeclareFunction FCN16 Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByValcountAs Long, ByVal Sendstr As String) As Long
PrivatedeclareFunction FCN16_xSet Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByVal Bit As Long) As Long
PrivatedeclareFunction FCN16_xReset Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByVal Bit As Long) As Long
PrivatedeclareFunction FCN16_xSetReset Lib "modbus.dll" (ByVal nport As Long, ByVal node As Long, ByVal address As Long, ByVal Bit As Long) As Long
PrivatedeclareFunction ComTrue Lib "modbus.dll" (ByVal nport As Long) As Long
PrivatedeclareFunction MyBeep Lib "modbus.dll" (ByVal SoundHz As Long) As Long
PrivatedeclareFunction 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;
str.Format("%s",sz); //可以用此函数将读取的值转成字符串
(2) CString -> char*
CString str;
char* sz = str.GetBuffer(0);//可将字符串转成char*给函数赋值
5)、当不再需要使用DLL时记得关闭串口及释放动态链接库,如:
关闭串口
if cTrue(1)==1 then
{
cClose(1);
}
6)、释放DLL
FreeLibrary(m_handle);
六、详细的DLL使用请参DEMO程序,相关DEMO程序和说明所使用的开发环境VB指的是VB6.0,Delphi指的是Delphi6.0/Delphi7.0,
VC指的是VC6.0。如果使用了高版本开发环境请根据开发软件更新后所兼容的声明方式进行声明调用。VC的串口调用没DEMO程序
请参照本手册的说明进行调用(本说明的方法经过测试是可行有效的)。
DLl已经过使用和测试具有很好的稳定性(测试平台为Win2000/WinXP),目前还没有用户方面返回的缺陷报告,如果你在使用过程中发现
返回值: 16进制字符串数据,字符串数据的终止符为"@";
使用举例:
FCN04(1,17,8,1),由COM1读取modbus 17号从站30009(标准modbus地址)的字状态值,返回值为“000A@”
则表示30009:000A;
在读取错误或不能读取的情况下返回“Error@”
注:没有注册的用户只能读取30001~30003范围的状态值;