安全生产
首页资讯供应求购招商招聘展会社区
长期信息合作请联系:QQ66821730
PLC技术首页-->PLC基础plc应用PLC文案资料PLC手册国标规程plc资料下载技术培训人才招聘职业认证产品商情论坛
PLC搜索
我 要 找
标题 内容 作者
PLCRSS订阅PLC技术信息
电工技术资料 您的位置: 机电之家-->plc技术资料栏目首页-> plc应用 -> 三菱plc应用 --> VB与AB的PLC之间通讯
阅读plc技术相关资料
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 '显示结果

字串2


      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()
字串9

      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 字串4
      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)

字串8


      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 字串7
      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 字串6
      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 字串9
      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) 字串6
      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

字串7


      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



有搞不定的程序,交给机电之家吧!去悬赏竞标
 VB与AB的PLC之间通讯相关资料
  • 用三菱的FX2N PLC实现N:N网络(令牌总线)
  • PLC程序调试步骤
  • 三菱PLC的几个常见问题解答
  • PLC内置高速计数器的简单应用介绍
  • PC与FX30DUE传输数据办法
  • 变频器逆变器件的原理
  • 三菱PLC程序编写注意事项一例
  • 三菱FX,A,QnA系列PLC特殊寄存器查询软件
  • 三菱FX2N系列PLC与Profibus的连接
  • 计数器级联PLC程序梯形图 
  • ⊕这地方投资政策最优
    ⊕上千份机电行业研究报告
    ⊕机电项目招商啦
    ⊕谁把我买了?
    ⊕机电行业展会大全
    ⊕十万企业抢登行业网址大全
    机电之家会议开通
    ⊕每日最新求购信息
    ⊕电工技术资料为了谁?
    ⊕机电设备维修与管理
    机电之家(中国)plc技术资料中心资讯版权声明:
    1、凡注明“机电之家采编”字样的所有作品均系本网原创,版权归机电之家所有,任何媒体摘编或享用本作品,需注明文章来源。违反声明者,本网将追究其相关法律责任。
    2、凡本网注明“来源:XXX网(非本网)”的作品,均转载自其他媒体,目的在于传达更多资讯,本网不承担相关法律责任。

    3、如在资讯、广告等方面想与本网合作,请致电:0571-87774297。Email:donemi@hz.cn

    ·工程项目经理培训
    ·欧姆龙PLC编程维护培训
    ·杭州西门子PLC应用培训
    ·模具加工设计培训
    ·变频器维修培训
    ·安全员认证培训
    ·电工培训


    ·招聘项目管理人员
    ·首席技术执行官
    ·自控工程师
    ·数控编程学徒
    ·总工程师

    项目竞标

    最新商业情报
    代理
    [代理] 寻求地区代理
    [代理] 电工产品诚招代理..
    采购
    [采购] 电动车控制器外壳
    [采购] 高品质缓冲器
    论坛最新话题
    ·超级搞笑的安全事故
    ·电工技术资料flash大全
    ·最新的搞笑图片
    ·工控行业应用软件下载
    首页
    首页
    合作网站:
    | 中国机电网机电之家安全生产网 | 机电论文 | 机电论坛 | 机电设备贸易 | 机电网址大全 | 浙江机电网 | 陕西机电网 | 变频器技术网 |
    中国电工网 | 电工网 |环球会展网机电产品网 | 机电人才网 | 中国工控网 | 五金工具网 | 安全生产网 | 甘肃机电网 | PLC技术网 |
    友情连接:
    | 中国机电网 | 中国工控网 | 行业培训网 | 中国工程机械网 | 机电一体化网 | 行业下载网 | 行业国标网 | 商业情报站 |
    关于我们 | 联系我们 | 广告联系 | 付款方式 | 使用帮助 | PLC技术网 | 会员助手 | 友情链接
    电话:0571-87774297(杭州) 传真:0571-87774298(杭州)点击这里给我发消息66821730(技术) 点击这里给我发消息58733127(审核)
    机电之家 PLC技术网 站所共享的PLC知识,PLC技术,PLC应用,PLC行情分析,PLC学习资料,PLC国标规程,PLC维修知识,
    PLC国家标准,PLC操作规程,PLC岗位职责,PLC管理制度,PLC工作总结,PLC实习报告,PLC考试题库,
    等都是来自会员发表或 网上收集整理。如果有任何侵犯您权益的地方,请联系我们,我们将马上进行处理。
    企业登陆可自行免费发布资料,本站代发布邮箱为88ctv@163.com
    Copyright 2007 plc.jdzj.com Inc All Rights Reserved.PLC技术网
    chinaplc.net 联合建设
    技术支持:PLC技术网 mailto:88ctv@163.com
    免费发布信息主办:浙江-杭州-PLC技术网网络运营部安全生产