国产蝌蚪视频在线观看_亚洲亚色_日批网站免费_亚拍一区_69网站_九热精品_最新中文在线视频_欧美视频日韩视频_国内精品二区_天堂资源站_av综合一区_国产精品久免费的黄网站_四虎免费在线观看_少妇做爰奶水狂喷视频_野花视频韩国免费观看3_久久国产精品亚洲人一区二区三区_国产日产精品_国产精品毛片_亚洲精品久久草草起碰_国产偷国产偷亚洲综合av_两个人日本WWW免费版_91麻豆视频_亚欧在线观看视频_久久久99免费_久久精品视频12_麻豆视频播放_日本高清WWW午色夜COM_公和我做好爽添厨房_成人午夜剧场在线观看_gay无套ⅹxxoo_在线观看成人福利

tangfeng
學(xué)海無(wú)涯
級(jí)別: 探索解密
精華主題: 0
發(fā)帖數(shù)量: 0 個(gè)
工控威望: 143 點(diǎn)
下載積分: 922 分
在線時(shí)間: 99(小時(shí))
注冊(cè)時(shí)間: 2010-01-24
最后登錄: 2025-09-13
查看tangfeng的 主題 / 回貼
樓主  發(fā)表于: 2017-09-02 16:50
'在窗體的Load事件中加入下列代碼對(duì)串口進(jìn)行初始化:
Private Sub Form_Load()
  MSComm1.CommPort = 1                  ' 設(shè)置通信端口號(hào)為COM1
  MSComm1.InputMode = 1                 ' 接收二進(jìn)制型數(shù)據(jù)
  MSComm1.RThreshold = 1                ' 設(shè)置并返回要接收的字符數(shù)
  MSComm1.SThreshold = 1                ' 設(shè)置并返回發(fā)送緩沖區(qū)中允許的最小字符數(shù)
  MSComm1.Settings = "9600,E,7,1"       ' 設(shè)置串口1通信參數(shù)
  MSComm1.PortOpen = True               ' 打開(kāi)通信端口1
  Timer1.Enabled = False
End Sub
'向PLC發(fā)送指令:02 30 31 30 30 30 32 30 03 35 36,功能是從D0開(kāi)始讀取32個(gè)字節(jié)數(shù)據(jù)
Private Sub Cmdsend_Click()
  MSComm1.Output = Chr(&H2) & Chr(&H30) & Chr(&H31) & Chr(&H30) & Chr(&H30) & Chr(&H30) & Chr(&H32) & Chr(&H30) & Chr(&H3) & Chr(&H35) & Chr(&H38)
  Timer1.Enabled = True
End Sub
'獲得各輸入端口狀態(tài)
Private Sub Timer1_Timer()
  Dim Inbyte() As Byte
  Dim buffer As String
  ReDim Inbyte(100)
  Inbyte = MSComm1.Input                         '返回的數(shù)據(jù)串
  For i = LBound(Inbyte) To UBound(Inbyte)
     buffer = buffer + Hex(Inbyte(i)) + Chr(32)
  Next i
  'Text1.Text = buffer                            '顯示返回的數(shù)據(jù)串,需自己添加TextBox
  If Hex(Inbyte(2)) = "31" Then                  '31表示觸點(diǎn)閉合
    Shape1(0).FillColor = QBColor(12)
  Else                                           '30表示觸點(diǎn)斷開(kāi)
    Shape1(0).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(6)) = "31" Then
    Shape1(1).FillColor = QBColor(12)
  Else
    Shape1(1).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(10)) = "31" Then
    Shape1(2).FillColor = QBColor(12)
  Else
    Shape1(2).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(14)) = "31" Then
    Shape1(3).FillColor = QBColor(12)
  Else
    Shape1(3).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(18)) = "31" Then
    Shape1(4).FillColor = QBColor(12)
  Else
    Shape1(4).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(22)) = "31" Then
    Shape1(5).FillColor = QBColor(12)
  Else
    Shape1(5).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(26)) = "31" Then
    Shape1(6).FillColor = QBColor(12)
  Else
    Shape1(6).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(30)) = "31" Then
    Shape1(7).FillColor = QBColor(12)
  Else
    Shape1(7).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(34)) = "31" Then
    Shape1(8).FillColor = QBColor(12)
  Else
    Shape1(8).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(38)) = "31" Then
    Shape1(9).FillColor = QBColor(12)
  Else
    Shape1(9).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(42)) = "31" Then
    Shape1(10).FillColor = QBColor(12)
  Else
    Shape1(10).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(46)) = "31" Then
    Shape1(11).FillColor = QBColor(12)
  Else
    Shape1(11).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(50)) = "31" Then
    Shape1(12).FillColor = QBColor(12)
  Else
    Shape1(12).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(54)) = "31" Then
    Shape1(13).FillColor = QBColor(12)
  Else
    Shape1(13).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(58)) = "31" Then
    Shape1(14).FillColor = QBColor(12)
  Else
    Shape1(14).FillColor = QBColor(10)
  End If
  If Hex(Inbyte(62)) = "31" Then
    Shape1(15).FillColor = QBColor(12)
  Else
    Shape1(15).FillColor = QBColor(10)
  End If
  Timer1.Enabled = False
End Sub
'當(dāng)退出程序時(shí),關(guān)閉串行口
Private Sub Cmdquit_Click()
  MSComm1.PortOpen = False           '關(guān)閉串口
  Unload Me
End Sub
// 讀不到PLC 輸入點(diǎn),下標(biāo)越界,望高手指點(diǎn)修改
不小心遇見(jiàn)你
自動(dòng)項(xiàng)目、PLC、視覺(jué)、通訊等工控軟件開(kāi)發(fā)QQ3515716
級(jí)別: 工控俠客

精華主題: 3 篇
發(fā)帖數(shù)量: 0 個(gè)
工控威望: 3056 點(diǎn)
下載積分: 25423 分
在線時(shí)間: 1537(小時(shí))
注冊(cè)時(shí)間: 2014-01-05
最后登錄: 2025-09-15
查看不小心遇見(jiàn)你的 主題 / 回貼
1樓  發(fā)表于: 2017-09-09 14:38
不要這樣寫程序 給你推薦個(gè)官方使用的控件即可 使用方便
MX Component
項(xiàng)目、視覺(jué)、通訊QQ3515716

绥江县| 汉寿县| 祁阳县| 华容县| 金华市| 米脂县| 石泉县| 渭南市| 泽州县| 长沙市| 十堰市| 拜泉县| 威海市| 鹤岗市| 长沙市| 任丘市| 沙湾县| 宜兰市| 沙坪坝区| 益阳市| 城市| 威远县| 象州县| 葫芦岛市| 满城县| 濮阳县| 桐城市| 林周县| 四子王旗| 太湖县| 博爱县| 桐柏县| 澄迈县| 大方县| 昭觉县| 蓬安县| 望谟县| 钟祥市| 屏山县| 资源县| 连州市|