如何用vb开发程序直接通过以太网tcp协议访问s7-1200的存储区域
2个回答
展开全部
VB6.0还是VB.NET?
在此写一段VB.NET的代码:
Dim Handle1 As Int32
Dim EntLink As Boolean
Dim PLC As New WinTcpS7_1K.PlcClient
Private Sub butLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butLink.Click
Dim re As Short
Dim restr As String = ""
re = PLC.EntLink(Trim(txtLocalIP.Text), Val(txtLocalPort.Text), Trim(txtRemoteIP.Text), Val(txtRemotePort.Text), 0, 1, "DEMO", Handle1)
txtReLink.Text = re.ToString
If re = 0 Then
EntLink = True
MsgBox("PLC联接成功!")
Else
EntLink = False
MsgBox("PLC联接失败: " & restr)
End If
End Sub
Private Sub butClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butClose.Click
Dim re As Short
re = PLC.DeLink(Handle1)
txtReClose.Text = re.ToString
EntLink = False
End Sub
Private Sub butRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butRead.Click
Dim i As Short
Dim re As Short
Dim RD() As Object
ReDim RD(Val(txtReadCnt.Text - 1))
If Not EntLink Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
Dim var1 As Integer = cmbReadType.SelectedIndex + 1
Dim typ As WinTcpS7_1K.PlcClient.DataType = var1
Select Case cmbReadMry.SelectedIndex
Case 0 : re = PLC.CmdRead(Handle1, WinTcpS7_1K.PlcClient.PlcMemory.DI, typ, CUShort(Val(txtReadBlock.Text)), CUShort(Val(txtReadAdd.Text)), CUShort(Val(txtReadCnt.Text)), RD)
Case 1 : re = PLC.CmdRead(Handle1, WinTcpS7_1K.PlcClient.PlcMemory.DQ, typ, CUShort(Val(txtReadBlock.Text)), CUShort(Val(txtReadAdd.Text)), CUShort(Val(txtReadCnt.Text)), RD)
Case 2 : re = PLC.CmdRead(Handle1, WinTcpS7_1K.PlcClient.PlcMemory.MR, typ, CUShort(Val(txtReadBlock.Text)), CUShort(Val(txtReadAdd.Text)), CUShort(Val(txtReadCnt.Text)), RD)
Case 3 : re = PLC.CmdRead(Handle1, WinTcpS7_1K.PlcClient.PlcMemory.DR, typ, CUShort(Val(txtReadBlock.Text)), CUShort(Val(txtReadAdd.Text)), CUShort(Val(txtReadCnt.Text)), RD)
End Select
txtReRead.Text = re.ToString
lstRead.Items.Clear()
For i = 0 To UBound(RD) Step 1
If Not IsNothing(RD(i)) Then
lstRead.Items.Add(RD(i))
Else
lstRead.Items.Add("0")
End If
Next i
End Sub
Private Sub butWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butWrite.Click
Dim i As Short
Dim re As Short
Dim temp() As String
Dim WD() As Object
If Not EntLink Then
MsgBox("还未与PLC建立联接!")
Exit Sub
End If
ReDim WD(Val(txtWriteCnt.Text) - 1)
temp = Split(txtWrite.Text, vbCrLf)
For i = 0 To UBound(WD) Step 1
If i > UBound(temp) Then
WD(i) = 0
Else
WD(i) = Trim(temp(i))
End If
Next i
Dim var1 As Integer = cmbWriteType.SelectedIndex + 1
Dim typ As WinTcpS7_1K.PlcClient.DataType = var1
Select Case cmbWriteMry.SelectedIndex
Case 0 : re = PLC.CmdWrite(Handle1, WinTcpS7_1K.PlcClient.PlcMemory.DI, typ, CUShort(Val(txtWriteBlock.Text)), CUShort(Val(txtWriteAdd.Text)), CUShort(Val(txtWriteCnt.Text)), WD)
Case 1 : re = PLC.CmdWrite(Handle1, WinTcpS7_1K.PlcClient.PlcMemory.DQ, typ, CUShort(Val(txtWriteBlock.Text)), CUShort(Val(txtWriteAdd.Text)), CUShort(Val(txtWriteCnt.Text)), WD)
Case 2 : re = PLC.CmdWrite(Handle1, WinTcpS7_1K.PlcClient.PlcMemory.MR, typ, CUShort((txtWriteBlock.Text)), CUShort(Val(txtWriteAdd.Text)), CUShort(Val(txtWriteCnt.Text)), WD)
Case 3 : re = PLC.CmdWrite(Handle1, WinTcpS7_1K.PlcClient.PlcMemory.DR, typ, CUShort(Val(txtWriteBlock.Text)), CUShort(Val(txtWriteAdd.Text)), CUShort(Val(txtWriteCnt.Text)), WD)
End Select
txtReWrite.Text = re.ToString
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询