你当前的位置:首页 > 编程技巧 >

VB与AB的PLC之间通讯

返回文章列表 作者:老耿 时间:01-27 来源:大测控  字体:【

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
文章相关
现有0条评论 | 更多评论..

我要发表看法



  如果您还不是本站会员
欢迎加入
  • ※ 评论注意事项:
  • 不是会员或未登陆发表评论,评论人名字显示为匿名。
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规。
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任。
  • 本站管理人员有权保留或删除评论中的任意内容。
  • 参与本评论即表明您已经阅读并接受上述条款。
站内搜索
本栏热门
计算机及通讯技术已成为工业环境中大部分解决方案的核心部分,其在系统中的比重正在迅速增加。在工业控制中,交流电机的拖动越来越多的采用变频器完成,不仅作为一个单独的执行机构,而是随着不断的智能化,同远程计算机之间可以通过各种通讯方式结合成一个有机的整体。
自来水是保障城市经济发展和人民生活的重要基础设施,是保障城市繁荣发展、人民生活以及发展国民经济不可缺少的先决条件。从另一个方面来说,我国是一个高度缺水的发展中国家,随着改革开放的不断深入和发展以及人口数量的不断增长,无论从自来水产量和质量上,社会都对城市供水提供了更高的要求。而实现水厂与供水调度系统的自动化,是保证自来水安全
西门子提供的最新软件:Simatic Net PC-Software CD 2005为各种组态软件的开发提供了一个统一的平台,它建立的PC站既为一些组态软件,如:WinCC、Protol等提供了与PLC的通讯平台,也提供了一套编程接口,可使用高级语言编程通过Simatic Net访问PLC数据。
针对控制系统中上位机与S7-200系列PLC通信的实际问题,总结了几种监控计算机与S7-200系列PLC的通信方法,这几种方法在不同的控制系统中能较好解决监控计算机与S7-200系列PLC通信问题。西门子工控产品在工控领域应用市场中有较高的占有率,S7-200系列PLC是西门子SIMATIC PLC
西门子S7-200PLC是德国西门子公司生产的小型PLC。S7-200以其高可靠性、指令丰富、内置功能丰富、强劲的通讯能力、较高的性价比等特点,在工业控制领域中被广泛应用。S7-200PLC的突出特点之一是自由口通讯功能。如何实现S7-200PLC与个人计算机的互联通信,是S7-200PLC应用

设为首页| 加入收藏| 联系我们
Copyright 2004-2007 Www.plc365.coM All Rights Reserved
免费提供PLC可编程控制器编程资料,应用案例,软件下载
请使用1024*768分辨率浏览本站以达到最佳视觉效果