PC(上位机)与PLC串行通讯协议与串口DLL之 modbus rtu 协议(4)
参数:
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 …… 此处隐藏:3202字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [实用模板]第八章:法国“新浪潮”与“左岸派”
- [实用模板]2021年北京上半年临床医学检验技师生物
- [实用模板]SAP GUI 7.10客户端安装配置文档
- [实用模板]2001年临床执业医师资格考试综合笔试试
- [实用模板]36机场工作实用英语词汇总结
- [实用模板](一)社会保险稽核通知书
- [实用模板]安全教育主题班会材料
- [实用模板]濉溪县春季呼吸道传染病防控应急演练方
- [实用模板]长沙房地产市场周报(1.30-2.3)
- [实用模板]六年级数学上册典中点 - 图文
- [实用模板]C程序设计(红皮书)习题官方参考答案
- [实用模板]中国证监会第一届创业板发行审核委员会
- [实用模板]桥梁工程复习题
- [实用模板]2011学而思数学及答案
- [实用模板]初中病句修改专项练习
- [实用模板]监理学习知识1 - 图文
- [实用模板]小机灵杯四年级试题
- [实用模板]国贸专业毕业论文模板
- [实用模板]教育学概论考试练习题-判断题4
- [实用模板]2015届高考英语一轮复习精品资料(译林
- 00Nkmhe_市场营销学工商管理_电子商务_
- 事业单位考试法律常识
- 诚信教育实施方案
- 吉大小天鹅食品安全检测箱方案(高中低
- 房地产销售培训资料
- 高一地理必修1复习提纲
- 新概念英语第二册lesson_1_练习题
- 证券公司内部培训资料
- 小学英语时间介词专项练习
- 新世纪英语专业综合教程(第二版)第1册U
- 【新课标】浙教版最新2018年八年级数学
- 工程建设管理纲要
- 外研版 必修一Module 4 A Social Surve
- Adobe认证考试 AE复习资料
- 基于H.264AVC与AVS标准的帧内预测技术
- 《食品检验机构资质认定管理办法》(质
- ABB变频器培训课件
- (完整版)小学说明文阅读练习题及答案
- 深思洛克(SenseLock) 深思IV,深思4,深
- 弟子规全文带拼音




