none
VB2008中如何建立以UDP方式與PLC通信 RRS feed

  • 问题

  • 請教站上各位高手..小弟剛由VB6轉進VB2008 在與PLC通訊的部分因為沒有Winsock這各元件可調用.請問要如何完成對PLC下達指令跟收集指令
    這是以前在VB6下所寫的簡單連線測試.但換到VB2008中卻抓不著頭緒!請各位大哥幫忙了
    Private Sub Command3_Click()
    Dim MS() As Byte
    
    If Text1.Text = "" Then
    MsgBox "警告!請輸入字串", vbCritical
    Exit Sub
    End If
    
     l = Len(Text1.Text)
        For j = 1 To l Step 2
            a = a + ChrB(Val("&H" + Mid$(Text1.Text, j, 2)))
        Next j
     L1 = LenB(a)
     MS() = LeftB(a, L1)
    On Error GoTo DoWith
    Winsock1.SendData MS() '對甲電腦下達 MS指令
    Exit Sub
    DoWith:
    MsgBox "警告!對方早在下達結束指令前就已斷線", vbCritical
    End Sub
    
    Private Sub Text3_Change()
    Winsock1.RemoteHost = "192.168.0.1"
    End Sub
    
    Private Sub Text4_Change()
    Winsock1.RemotePort = "9600"
    End Sub
    
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim data As String
        For i = 1 To 100000
        Next i
        Winsock1.GetData data, vbString
        Rec = data 'CStr(data)
        l = Len(Rec)
        For j = 1 To l
        p = Mid(Rec, j, 1)
        q = Asc(p)
        s = Hex(q)
        If Len(Hex(q)) < 2 Then
        s = "0" + Hex(q)
        End If
        r = r + s
        Next
       Text2.Text = r
            
    End Sub
    
    
    以上程式:是針對OMRON的PLC做連線測試 ,在text1中輸入通信指令..再由text2顯示回應碼!
    2009年8月7日 14:14

全部回复