PC(上位机)与PLC串行通讯协议与串口DLL之 modbus rtu 协议(4)

2019-08-26 18:02

参数:

CHex: 待转换字符串,取值为(0000~FFFF);

使用举例:

CHexToInt(\则返回整数1000;

注:在参数不正确等出错情况下返回值为“-1”。

4、抽取(0~65535)所示整数中(0~15)某个位的值

Function CinBin(CHex,Start:longint):longint;stdcall;

参数:

CHex: 待转换整数,取值为0~65535(0000~FFFF);

Start: 抽取的位,取值为(0~15);

使用举例:

CinBin(15,3),则返回值1; CinBin(15,4),则返回值0; CinBin(1000,6),则返回值1;

读取MB0组合成的字节值为“FC”(252)时,要读取M0.1的值时,则调用CinBin(252,1)返回值0表示M0.1的值为0。

注:在参数不正确等出错情况下返回值为“-1”。

5、返回字符串Text左边的Count个字符 (为VC等非RAD开发环境所增设)

Function CLeftStr(Text:Pchar;Count:longint):Pchar;stdcall;

参数:

Text: 字符串原型;

Count: 指定返回左侧字符串个数;

在参数错误等非法的情况下返回“Error@”

使用举例:

CleftStr(\

\为字符串数据的终止符。

6、返回字符串Text右边的Count个字符 (为VC等非RAD开发环境所增设)

Function CRightStr(Text:Pchar;Count:longint):Pchar;stdcall;

参数:

Text: 字符串原型;

Count: 指定返回右侧字符串个数

在参数错误等非法的情况下返回“Error@”

使用举例:

CRightStr(\

\为字符串数据的终止符。

7、返回字符串Text从Start开始的Count个字符 (为VC等非RAD开发环境所增设)

Function CMidStr(Text:Pchar;Start,Count:longint):Pchar;stdcall;

参数:

Text: 字符串原型;

Start: 指定返回字符串的起始位置;

Count: 指定返回字符串个数;

在参数错误等非法的情况下返回“Error@”

使用举例:

CMidStr(\,3) = \

\为字符串数据的终止符。

8、字符串Cstr开始于字符串Ostr的位置 (为VC等非RAD开发环境所增设)

Function Cinstr(Ostr,Ckstr:Pchar;Dcint:Longint):Longint;stdcall;

参数:

Ostr: 字符串原型;

Cstr: 查询的字符串;

Dcint: 设定查询字符串的起始位置,取值>=1,即最少从源字符串的第一个字符开始查询;

返回值:长整型;

使用举例:

CinStr(\

注:在参数不正确等出错情况下返回值为“0”。

9、PC喇叭的报警过程函数;

Function MyBeep(SoundHz:longint):longint;stdcall;

参数:SoundHz:长整型;

返回值:长整型,操作成功返回“1”,否则返回“0”。

使用举例:

MyBeep(2000); 按2000的频率发出报警声。

DLL中关于传出字符串值的函数都以\为字符串函数值终止符,这是对多开发环境应用时方便用户正确读取返回字符串数据而设。

五、Delphi、VB、VC语言环境的开发使用说明

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类型,下面给出主要函数的声明:

Private Declare Function ComOpen Lib \ate As Long, ByVal DataBits As Long, ByVal Parity As Long, ByVal StopBits As Long, ByVal User As String) As Long

Private Declare Function ComClose Lib \Private Declare Function FCN01 Lib \ong, ByVal address As Long, ByVal Count As Long) As String

Private Declare Function FCN02 Lib \ong, ByVal address As Long, ByVal Count As Long) As String

Private Declare Function FCN03 Lib \ong, ByVal address As Long, ByVal Count As Long) As String

Private Declare Function FCN04 Lib \ong, ByVal address As Long, ByVal Count As Long) As String

Private Declare Function FCN05S Lib \Long, ByVal address As Long) As Long

Private Declare Function FCN05R Lib \Long, ByVal address As Long) As Long

Private Declare Function FCN06 Lib \ong, ByVal address As Long, ByVal Sendstr As String) As Long

Private Declare Function FCN15 Lib \ong, ByVal address As Long, ByVal Count As Long, ByVal Sendstr As String) As Long Private Declare Function FCN16 Lib \ong, ByVal address As Long, ByVal Count As Long, ByVal Sendstr As String) As Long Private Declare Function FCN16_xSet Lib \e As Long, ByVal address As Long, ByVal Bit As Long) As Long

Private Declare Function FCN16_xReset Lib \ode As Long, ByVal address As Long, ByVal Bit As Long) As Long


PC(上位机)与PLC串行通讯协议与串口DLL之 modbus rtu 协议(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:强制清算中应注意的问题

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: