用户登陆 用户注册
您的位置:首页>
技术文章>内容正文
VB与AB的PLC之间通讯
[正文]: ab系列的plc一般都有专用的驱动程序用于实现plc和计算机之间的通讯,如rslinx就是专门用于做这项工作的,但使用rslinx也具有一定的局限性,这里提供一个使用vb编程实现plc和计算机之间的通讯程序,使用的协议是df1,可以支持micrologix、slc500等系列的plc。
使用的代码如下: option explicit dim tns%, comunicating private sub command1_click() redim tb%(10) dim st if readtable(0, tb%()) then for st = 0 to 9 '显示结果 text1.seltext = str(tb%(st)) + chr(32) next st text1.seltext = chr(13) + chr(10) end if end sub private sub command2_click() redim tm%(5) tm%(0) = rnd * 32768 tm%(1) = rnd * 32768 tm%(2) = rnd * 32768 tm%(3) = rnd * 32768 tm%(4) = rnd * 32768 if not writetable(4, tm%()) then text1.seltext = "写入错误!!" end sub private sub exit_click() unload me end end sub private sub form_load() comm1.portopen = true end sub private sub form_unload(cancel as integer) comm1.portopen = false end sub private sub calccrc(mes36;) dim byt%, res& '对消息进行crc校验,然后将结果添加到消息的结尾。
byt% = 3 do res& = res& xor asc(mid(mes36;, byt%, 1)) rotate res& if asc(mid(mes36;, byt%, 1)) = 16 then mes36; = left36;(mes36;, byt%) + chr(16) + right36;(mes36;, len(mes36;) - byt%) byt% = byt% + 1 end if byt% = byt% + 1 loop while (byt% <= len(mes36;) - 2) res& = res& xor 3 rotate res& mes36; = mes36; + chr(res& mod 256) + chr(int(res& / 256)) end sub function readtable(start, n%()) dim st, com36; '从plc cif数据表中读取数据, micrologix=n7 slc500=n9 if comunicating then exit function comunicating = true form1.comm1.inputlen = 0 '清缓冲区 com36; = form1.comm1.input '构建消息 com36; = chr(16) + chr(2) + chr(0) + chr(0) com36; = com36; + chr(1) + chr(0) + chr(tns%) + chr(0) com36; = com36; + chr(start) + chr(0) + chr(ubound(n%) * 2) com36; = com36; + chr(16) + chr(3) '进行crc计算并附加到结尾。
calccrc com36; tns% = tns% + 1 if tns% = 256 then tns% = 0 '发送命令 form1.comm1.output = com36; '等待确认 st = timer do doevents loop while st + 3 > timer and form1.comm1.inbuffercount < 2 '从缓冲中移除确认 form1.comm1.inputlen = 2 com36; = form1.comm1.input if com36; <> chr(16) + chr(6) then comunicating = false exit function end if st = timer '等待应答 do doevents loop while st + 3 > timer and form1.comm1.inbuffercount < 12 + (ubound(n%) * 2) '超时则退出 if form1.comm1.inbuffercount < 12 + (ubound(n%) * 2) then comunicating = false exit function end if '发送确认 form1.comm1.output = chr(16) + chr(6) '得到应答 form1.comm1.inputlen = 0 com36; = form1.comm1.input st = 3 do if mid(com36;, st, 1) = chr(16) then com36; = left(com36;, st) + right(com36;, len(com36;) - 1 - st) end if st = st + 1 loop while st < len(com36;) - 4 '保存结果 for st = 0 to ubound(n%) - 1 n%(st) = 256 * asc(mid(com36;, 2 * st + 10, 1)) + asc(mid(com36;, 2 * st + 9, 1)) next st readtable = true comunicating = false end function private sub rotate(res&) dim bitout%, shift% for shift% = 1 to 8 bitout% = res& mod 2 res& = int(res& / 2) if bitout% then res& = res& xor &h1000a001 res& = res& - &h10000000 end if next shift% end sub function writetable(start, n%()) dim st, com36; '写到 plc cif数据表, micrologix=n7 slc500=n9 if comunicating then exit function comunicating = true form1.comm1.inputlen = 0 com36; = form1.comm1.input com36; = chr(16) + chr(2) + chr(0) + chr(0) com36; = com36; + chr(8) + chr(0) + chr(tns%) + chr(0) com36; = com36; + chr(start) + chr(0) for st = 0 to ubound(n%) com36; = com36; + chr(n%(st) mod 256) + chr(int(n%(st) / 256)) next st com36; = com36; + chr(16) + chr(3) tns% = tns% + 1 if tns% = 256 then tns% = 0 calccrc com36; form1.comm1.output = com36; st = timer do doevents loop while st + 3 > timer and form1.comm1.inbuffercount < 2 form1.comm1.inputlen = 2 com36; = form1.comm1.input if com36; <> chr(16) + chr(6) then comunicating = false exit function end if st = timer do doevents loop while st + 3 > timer and form1.comm1.inbuffercount < 12 form1.comm1.output = chr(16) + chr(6) if form1.comm1.inbuffercount < 12 then comunicating = false exit function end if form1.comm1.output = chr(16) + chr(6) writetable = true comunicating = false end function
网站首页
培训课程
维修指南
技术文章
家电专栏
供应信息
求购信息
培训资讯
展会信息
电脑专栏
教程下载
资料下载
常用软件
PLC教程
PLC资料
变频伺服
低压电器
维修资料
人机界面
自控仪表
工控机类
文章标题:
中国工控资源网手机版 2012
电话:010-67577139 13811659603
培训咨询QQ:657167934 471895637 销售咨询QQ:623769457
联系邮箱:zggkzyw@163.com
京ICP备11002135号
报时(2026-04-06 03:37:52)