欢迎来到天天文库
浏览记录
ID:38209880
大小:29.50 KB
页数:5页
时间:2019-05-24
《VB与AB的PLC之间通讯》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、VB与AB的PLC之间通讯VB与ABPLC之间通讯 AB系列PLC一般都有专用驱动程序用于实现PLC和计算机之间通讯,如RSLINX就是专门用于做这项工作,但使用RSLINX也具有一定局限性,这里提供一个使用VB编程实现PLC和计算机之间通讯程序,使用协议是DF1,可以支持Micrologix、SLC500等系列PLC。使用代码如下: OptionExplicit Dimtns%,comunicating PrivateSubCommand1_Click() ReDimtb%(10)
2、 Dimst IfReadTable(0,tb%())Then Forst=0To9'显示结果 Text1.SelText=Str(tb%(st))+Chr(32) Nextst Text1.SelText=Chr(13)+Chr(10) EndIf EndSub PrivateSubCommand2_Click() ReDimtm%(5) tm%(0)=Rnd*32768 tm%(1)=Rnd*32768 tm%(2)=Rnd*
3、32768 tm%(3)=Rnd*32768 tm%(4)=Rnd*32768 IfNotWriteTable(4,tm%())ThenText1.SelText="写入错误!!" EndSub PrivateSubExit_Click() UnloadMe End EndSub PrivateSubForm_Load() Comm1.PortOpen=True EndSub PrivateSubForm_Unload(Cancel
4、AsInteger) Comm1.PortOpen=False EndSub PrivateSubCalcCRC(mes$) Dimbyt%,res& '对消息进行crc校验,然后将结果添加到消息结尾。 byt%=3 Do res&=res&XorAsc(Mid(mes$,byt%,1)) rotateres& IfAsc(Mid(mes$,byt%,1))=16Then mes$=Left$(mes$,byt%)+Chr(16)+Righ
5、t$(mes$,Len(mes$)-byt%) byt%=byt%+1 EndIf byt%=byt%+1 LoopWhile(byt%<=Len(mes$)-2) res&=res&Xor3 rotateres& mes$=mes$+Chr(res&Mod256)+Chr(Int(res&/256)) EndSub FunctionReadTable(start,n%()) Dimst,com$ '从PLCCIF数据表中读取数据,Micr
6、ologix=N7SLC500=N9 IfcomunicatingThenExitFunction comunicating=True Form1.Comm1.InputLen=0'清缓冲区 com$=Form1.Comm1.Input '构建消息 com$=Chr(16)+Chr(2)+Chr(0)+Chr(0) com$=com$+Chr(1)+Chr(0)+Chr(tns%)+Chr(0) com$=com$+Chr(start)+Chr(0)+Chr(UB
7、ound(n%)*2) com$=com$+Chr(16)+Chr(3) '进行crc计算并附加到结尾。 CalcCRCcom$ tns%=tns%+1 Iftns%=256Thentns%=0 '发送命令 Form1.Comm1.Output=com$ '等待确认 st=Timer Do DoEvents LoopWhilest+3>TimerAndForm1.Comm1.InBufferCount<2 '从缓冲中移除确认
8、 Form1.Comm1.InputLen=2 com$=Form1.Comm1.Input Ifcom$<>Chr(16)+Chr(6)Then comunicating=False ExitFunction EndIf
此文档下载收益归作者所有