紫外工控论坛

 找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 2193|回复: 0

[VB/VB.NET] VB与AB的PLC之间通讯

[复制链接]
冰糖 发表于 2011-11-15 10:08:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
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(mes$)
      Dim byt%, res&
      '对消息进行crc校验,然后将结果添加到消息的结尾。
      byt% = 3
      Do
      res& = res& Xor Asc(Mid(mes$, byt%, 1))
      rotate res&
      If Asc(Mid(mes$, byt%, 1)) = 16 Then
      mes$ = Left$(mes$, byt%) + Chr(16) + Right$(mes$, Len(mes$) - byt%)
      byt% = byt% + 1
      End If
      byt% = byt% + 1
      Loop While (byt% <= Len(mes$) - 2)
      res& = res& Xor 3
      rotate res&
      mes$ = mes$ + Chr(res& Mod 256) + Chr(Int(res& / 256))
      End Sub

      Function ReadTable(start, n%())
      Dim st, com$
      '从PLC CIF数据表中读取数据, Micrologix=N7 SLC500=N9
      If comunicating Then Exit Function
      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(UBound(n%) * 2)
      com$ = com$ + Chr(16) + Chr(3)
      '进行crc计算并附加到结尾。
      CalcCRC com$
      tns% = tns% + 1
      If tns% = 256 Then tns% = 0
      '发送命令
      Form1.Comm1.Output = com$
      '等待确认
      st = Timer
      Do
      DoEvents
      Loop While st + 3 > Timer And Form1.Comm1.InBufferCount < 2
      '从缓冲中移除确认
      Form1.Comm1.InputLen = 2
      com$ = Form1.Comm1.Input
      If com$ <> 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
      com$ = Form1.Comm1.Input
      st = 3
      Do
      If Mid(com$, st, 1) = Chr(16) Then
      com$ = Left(com$, st) + Right(com$, Len(com$) - 1 - st)
      End If
      st = st + 1
      Loop While st < Len(com$) - 4
      '保存结果
      For st = 0 To UBound(n%) - 1
      n%(st) = 256 * Asc(Mid(com$, 2 * st + 10, 1)) + Asc(Mid(com$, 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, com$
      '写到 PLC CIF数据表, Micrologix=N7 SLC500=N9
      If comunicating Then Exit Function
      comunicating = True
      Form1.Comm1.InputLen = 0
      com$ = Form1.Comm1.Input
      com$ = Chr(16) + Chr(2) + Chr(0) + Chr(0)
      com$ = com$ + Chr(8) + Chr(0) + Chr(tns%) + Chr(0)
      com$ = com$ + Chr(start) + Chr(0)
      For st = 0 To UBound(n%)
      com$ = com$ + Chr(n%(st) Mod 256) + Chr(Int(n%(st) / 256))
      Next st
      com$ = com$ + Chr(16) + Chr(3)
      tns% = tns% + 1
      If tns% = 256 Then tns% = 0
      CalcCRC com$
      Form1.Comm1.Output = com$
      st = Timer
      Do
      DoEvents
      Loop While st + 3 > Timer And Form1.Comm1.InBufferCount < 2
      Form1.Comm1.InputLen = 2
      com$ = Form1.Comm1.Input
      If com$ <> 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家园|www.plczone.com 原文链接:http://www.plczone.com/lw/181/201109/20858.html
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


--------------------------------------------------------------------------------------------------------------------
本站是工控技术交流站点,论坛内容均为网络收集或会员所发表,并不代表本站立场,会员拥有该内容的所有权力及责任!
本站内容如有侵犯您的版权,请按下面方式联系本站管理员,我们将及时删除处理
管理员:冰糖 QQ:5483695(请直击主题), Mail:admin#ziwai.net(#改成@) 其它非本人.
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!

QQ|Archiver|手机版|小黑屋|紫外工控论坛. ( 苏ICP备11032118号-1 )

GMT+8, 2024-4-29 17:01 , Processed in 0.343752 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表