如何用vb开发程序直接通过以太网tcp协议访问s7-1200的存储区域

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用vb开发程序直接通过以太网tcp协议访问s7-1200的存储区域相关的知识,希望对你有一定的参考价值。

tcp协议是一个世界公开的协议,既然你在vb端能搞定如何进行数据通讯,1200就很简单了。
在1200侧你只需建立一个tcp的连接即可,连接类型为未指定,之后按照你自己的要求确定谁是客户机,谁是服务器就好了,具体的建立连接的步骤可以参照1200的文档,你可以在下面的文档的通讯一章找到如何建立TCP连接的过程
西门子 S7-1200 PLC Smart Plus 技术参考Version 1.8
参考技术A

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

如何用VB语言求水仙花数和求1~1000以内的素数

我和朋友一起发现了一个更简单的方法求1~1000以内的素数之和,VB代码如下:

Private Sub Command1_Click()
Dim i, s, n, c As Double
i = 3: s = 0
Do While i <= 1000
n = 1: c = 0
Do While n <= i
If i Mod n = 0 Then
c = c + 1
End If
n = n + 1
Loop
If c = 2 Then
s = s + i
End If
i = i + 2
Loop
s = s + 2
Print s
End Sub
运行结果是对的,就是76127
参考技术A Private Sub Form1_Click()Dim a As Integer, b As Integer, c As IntegerFor a = 1 To 9For b = 0 To 9For c = 0 To 9If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c ThenForm1.Print a * 100 + b * 10 + cEnd IfNext cNext bNext aEnd 参考技术B '求水仙花数
Private Sub Form1_Click()
Dim a As Integer, b As Integer, c As Integer
For a = 1 To 9
For b = 0 To 9
For c = 0 To 9
If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c Then
Form1.Print a * 100 + b * 10 + c
End If
Next c
Next b
Next a
End

'求1~1000以内的素数,
Private Sub Command1_Click()
Dim a As Integer, b As Integer
Text1 = ""
For a = 3 To 999 step 2
For b = 2 To Sqr(a)
If a Mod b = 0 Then
Exit For
End If
If b > Sqr(a) Then
Text1 = Text1 & a & VbNewline
End If
Next b
Next a
End追问

Text1 = ""是什么?

追答

Text1是一个多行文本框,上面那一句是先清空其中的内容(以防多次运行结果太乱)。

以上是关于如何用vb开发程序直接通过以太网tcp协议访问s7-1200的存储区域的主要内容,如果未能解决你的问题,请参考以下文章

西门子 S7-300 以太网模块连接 WINCC方案

如何用VB通过485串口来读取电能表的数据?

西门子 S7200 以太网模块连接组态王方法

西门子 S7300 以太网模块连接组态王方法

西门子plc有哪些通讯协议

400PLCDP转以太网实现S7TCP转ModbusTCPwincc通信