如何通过tcp/ip协议读取西门子s7-300plc地址块的数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何通过tcp/ip协议读取西门子s7-300plc地址块的数据相关的知识,希望对你有一定的参考价值。
1:S7-300集成的PN口,是支持TCP/IP协议的!至于怎么读取,你上位机 按照TCP 协议的编程方式来进行读取数据就行,PLC里也有相关的功能块原来发送和接受数据。需要注意的是,那方系统作为服务器和客户机的问题:2:集成的PN口支持 Modbus TCP,但是使用方式和用到的功能块和CP有些区别。原理是一样的
基于 S7-300,400 CPU 集成 PN 接口 Modbus TCP 通讯快速入门 参考如下
https://support.industry.siemens.com/cs/document/90276758/%E5%9F%BA%E4%BA%8E-s7-300400-cpu-%E9%9B%86%E6%88%90-pn-%E6%8E%A5%E5%8F%A3-modbus-tcp-%E9%80%9A%E8%AE%AF%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8?dti=0&lc=zh-CN
基于 S7-300,400 以太网通讯模块 CP343-1&CP443-1 Modbus TCP 通讯快速入门 参考如下
https://support.industry.siemens.com/cs/document/90276761/%E5%9F%BA%E4%BA%8E-s7-300400-%E4%BB%A5%E5%A4%AA%E7%BD%91%E9%80%9A%E8%AE%AF%E6%A8%A1%E5%9D%97-cp343-1cp443-1-modbus-tcp-%E9%80%9A%E8%AE%AF%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8?dti=0&lc=zh-CN
补充:
1:见上面的连接,PN口的TCP连接是需要在PLC 中调用功能块建立连接的,不需要组态。但是双方都要编程的!
2:都用高级语言自己开发上位机了,那么,你只要看下TCPIP协议,我想你应该为Socket编程吧!按照协议标准进行发送和接受数据就行。至于要度上面数据,自己和PLC 厂家定义个就行。
既然你需要走TCP协议,为什么不直接使用MODBUS TCP 这个公开的协议了,S7-300 PN口支持这个协议。 参考技术A 搜一下 WinTcpS7 的官方开发包; 参考技术B 采用 S7 协议就可以了
如何用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 Int32Dim 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
以上是关于如何通过tcp/ip协议读取西门子s7-300plc地址块的数据的主要内容,如果未能解决你的问题,请参考以下文章
三菱PLC的TCP/IP通讯是不是与西门子的TCP/TP相同?所有的PLC的该通讯是不是相同?