关于VB和西门子PLC的通讯
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于VB和西门子PLC的通讯相关的知识,希望对你有一定的参考价值。
急求一段VB链接PLC的程序,比如用VB监控西门子PLCS7-200 CPU224 Q0.0端口的程序。
十分感谢!
最好是用MSComm控件链接的。
如果有源码最好了,我的邮箱是 oracle@chervon.com.cn
2.在VB工程中引用sopcdaauto.dll或者通用的opcdaauto.dll,这些可以安装组态软件或者西门子系列软件得到
3.完成2后,就可以直接声明OPC SERVER以及OPC GROUP还有OPC ITEMS了
Public WithEvents ServerObj As OPCServer
Public WithEvents GroupObj As OPCGroup
Dim ItemObj1 As OPCItem
添加该标签
Set ItemObj1 = GroupObj.OPCItems.AddItem("S7:[S7 connection_1]Q0.0", 1)
4.设置读取数据,添加按钮,在其中输入代码
Dim OutText As String
Dim myValue As Variant
Dim myQuality As Variant
Dim myTimeStamp As Variant
Dim ClientID As Long
Dim ServerID As Long
Dim ErrorNr() As Long
Dim ErrorString As String
On Error GoTo ErrorHandler
OutText = "Reading Value of Item"
ClientID = READASYNC_ID
GroupObj.AsyncRead 1, Serverhandle, ErrorNr, ClientID, ServerID
If ErrorNr(1) <> 0 Then
ErrorString = ServerObj.GetErrorString(ErrorNr(1))
MsgBox ErrorString, vbCritical, "Error AsyncRead()"
End If
Erase ErrorNr
Exit Sub
ErrorHandler:
MsgBox Err.Description + Chr(13) + _
OutText, vbCritical, "ERROR"
这样就可以取数了
还是不明白的话,去我QQ空间,有篇文章写的很详细QQ:56229986 参考技术B
提供一个完整的VB6的代码:
Option ExplicitDim PLC As New WinS7_PPI.PlcCom
Dim Linked As Boolean
Private Sub Form_Load()
Dim i As Integer
Me.Top = Screen.Height / 2 - Me.Height / 2
Me.Left = Screen.Width / 2 - Me.Width / 2
'
cmbRdMemory.Clear
cmbRdMemory.AddItem "I"
cmbRdMemory.AddItem "Q"
cmbRdMemory.AddItem "M"
cmbRdMemory.AddItem "V"
cmbRdMemory.ListIndex = 2
cmbWtMemory.Clear
cmbWtMemory.AddItem "I"
cmbWtMemory.AddItem "Q"
cmbWtMemory.AddItem "M"
cmbWtMemory.AddItem "V"
cmbWtMemory.ListIndex = 2
'
cmbRdDataType.Clear
cmbRdDataType.AddItem "W_INT16"
cmbRdDataType.AddItem "W_UINT16"
cmbRdDataType.AddItem "D_DINT32"
cmbRdDataType.AddItem "D_HEX32"
cmbRdDataType.AddItem "D_REAL32"
cmbRdDataType.AddItem "W_BIN16"
cmbRdDataType.AddItem "B_CHAR8"
cmbRdDataType.AddItem "B_BYTE8"
cmbRdDataType.ListIndex = 0
cmbWtDataType.Clear
cmbWtDataType.AddItem "W_INT16"
cmbWtDataType.AddItem "W_UINT16"
cmbWtDataType.AddItem "D_DINT32"
cmbWtDataType.AddItem "D_HEX32"
cmbWtDataType.AddItem "D_REAL32"
cmbWtDataType.AddItem "W_BIN16"
cmbWtDataType.AddItem "B_CHAR8"
cmbWtDataType.AddItem "B_BYTE8"
cmbWtDataType.ListIndex = 0
'
cmbBitMemory.Clear
cmbBitMemory.AddItem "I"
cmbBitMemory.AddItem "Q"
cmbBitMemory.AddItem "M"
cmbBitMemory.AddItem "V"
cmbBitMemory.ListIndex = 2
For i = 0 To 7 Step 1
cmbBitNum.AddItem i
Next i
cmbBitNum.ListIndex = 0
End Sub
Private Sub Online_Click()
Dim re As Integer
Dim port As Integer
port = Val(txtPort.Text)
re = PLC.ComLink(port, 9600, Val(txtNode.Text), "DEMO")
txtState.Text = re
Linked = CBool(re = 0)
End Sub
Private Sub Offline_Click()
Dim re As Integer
re = PLC.DeLink()
txtState.Text = re
Linked = False
End Sub
Private Sub R_CIO_Click()
Dim i As Integer
Dim Node As Integer
Dim Memory As PlcMemory
Dim dtype As DataType
Dim add As Integer
Dim Count As Integer
Dim rd() As String
Dim re As Integer
Dim tstr As String
If Not Linked Then Exit Sub
Node = Val(txtNode.Text)
Select Case cmbRdMemory.ListIndex
Case 0: Memory = PlcMemory.DI
Case 1: Memory = PlcMemory.DQ
Case 2: Memory = PlcMemory.MR
Case 3: Memory = PlcMemory.DR
End Select
Select Case cmbRdDataType.ListIndex
Case 0: dtype = DataType.tINT16
Case 1: dtype = DataType.tUINT16
Case 2: dtype = DataType.tDINT32
Case 3: dtype = DataType.tHEX32
Case 4: dtype = DataType.tREAL32
Case 5: dtype = DataType.tBIN16
Case 6: dtype = DataType.tCHAR8
Case 7: dtype = DataType.tBYTE8
End Select
add = Val(txtRdAddress.Text)
Count = Val(txtRdCount.Text)
re = PLC.CmdRead(Node, Memory, dtype, add, Count, rd())
txtRdState.Text = re
tstr = ""
If re = 0 Then
For i = 0 To UBound(rd)
tstr = tstr & rd(i) & vbCrLf
Next i
End If
txtRead.Text = tstr
End Sub
Private Sub W_CIO_Click()
Dim i As Integer
Dim Node As Integer
Dim Memory As PlcMemory
Dim dtype As DataType
Dim add As Integer
Dim Count As Integer
Dim wt() As String
Dim re As Integer
Dim tstr() As String
'
If Not Linked Then Exit Sub
Node = Val(txtNode.Text)
Select Case cmbWtMemory.ListIndex
Case 0: Memory = PlcMemory.DI
Case 1: Memory = PlcMemory.DQ
Case 2: Memory = PlcMemory.MR
Case 3: Memory = PlcMemory.DR
End Select
Select Case cmbWtDataType.ListIndex
Case 0: dtype = DataType.tINT16
Case 1: dtype = DataType.tUINT16
Case 2: dtype = DataType.tDINT32
Case 3: dtype = DataType.tHEX32
Case 4: dtype = DataType.tREAL32
Case 5: dtype = DataType.tBIN16
Case 6: dtype = DataType.tCHAR8
Case 7: dtype = DataType.tBYTE8
End Select
add = Val(txtWtAddress.Text)
Count = Val(txtWtCount.Text)
ReDim wt(Count - 1) As String
tstr = Split(Trim(txtWrite.Text), vbCrLf)
For i = 0 To UBound(wt)
wt(i) = Trim(tstr(i))
Next i
re = PLC.CmdWrite(Node, Memory, dtype, add, Count, wt())
txtWtState.Text = re
End Sub
Private Sub T_BIT_Click()
Dim Node As Integer
Dim Memory As PlcMemory
Dim add As Integer
Dim bitnum As Integer
Dim re As Integer
Dim tb As Boolean
'
If Not Linked Then Exit Sub
Node = Val(txtNode.Text)
Select Case cmbBitMemory.ListIndex
Case 0: Memory = PlcMemory.DI
Case 1: Memory = PlcMemory.DQ
Case 2: Memory = PlcMemory.MR
Case 3: Memory = PlcMemory.DR
End Select
add = Val(txtBitAddress.Text)
bitnum = cmbBitNum.ListIndex
re = PLC.Bit_Test(Node, Memory, add, bitnum, tb)
txtBitState.Text = re
txtReBit.Text = IIf(tb, "ON", "OFF")
Linked = CBool(re = 0)
End Sub
Private Sub S_BIT_Click()
Dim Node As Integer
Dim Memory As PlcMemory
Dim add As Integer
Dim bitnum As Integer
Dim re As Integer
Dim tb As Boolean
'
If Not Linked Then Exit Sub
Node = Val(txtNode.Text)
Select Case cmbBitMemory.ListIndex
Case 0: Memory = PlcMemory.DI
Case 1: Memory = PlcMemory.DQ
Case 2: Memory = PlcMemory.MR
Case 3: Memory = PlcMemory.DR
End Select
add = Val(txtBitAddress.Text)
bitnum = cmbBitNum.ListIndex
re = PLC.Bit_Set(Node, Memory, add, bitnum)
txtBitState.Text = re
Linked = CBool(re = 0)
End Sub
Private Sub R_BIT_Click()
Dim Node As Integer
Dim Memory As PlcMemory
Dim add As Integer
Dim bitnum As Integer
Dim re As Integer
Dim tb As Boolean
If Not Linked Then Exit Sub
Node = Val(txtNode.Text)
Select Case cmbBitMemory.ListIndex
Case 0: Memory = PlcMemory.DI
Case 1: Memory = PlcMemory.DQ
Case 2: Memory = PlcMemory.MR
Case 3: Memory = PlcMemory.DR
End Select
add = Val(txtBitAddress.Text)
bitnum = cmbBitNum.ListIndex
re = PLC.Bit_Reset(Node, Memory, add, bitnum)
txtBitState.Text = re
Linked = CBool(re = 0)
End Sub 参考技术C 使用S7-200,但是数据交换量不是很大,用WINCC感觉比较浪费,所以就自己做个监控软件,就想到了用VB编写程序,来调用w95_s7.dll动态链接数据库。
1.Prodave 是用于pc 与s7 系列plc 之间的数据链接通信的工具箱。其实质就是用户通过调用“w95_s7.dll”等库函数来解决 PLC 与PC之间的数据交换和数据处理问题。 所需硬件为:CPU22x,PC/PPI编程电缆,带COM口的PC。这可能是最简单的配置,很容易实现试验的目的。这里不需要安装Prodave软件,只要把“w95_s7.dll”文件拷贝到您程序相应的目录中去即可。2.通讯设置:打开“控制面板”中“设置PG/PC接口”选项,在“应用程序访问点”列表框内选择“S7ONLINE (STEP7) PC/PPI Cable”,在其属性内的“超时”可以设置大点,这里设置为100ms,“波特率”设置为9.6kbps(因为我的pc/ppi电缆为国产的,不支持多主站),“地址”为0。3。源代码:①PLC程序:LD I0.0 MOVW 4766, VW0 ②VB程序:
****在模块中声明****:
Public plcadr As plcadrtype
Public gb_PlcConnected As Boolean
Type plcadrtype
adr As Byte ‘站地址,默认值为2
SEGMENTID As Byte ‘段标识符,固定值为0
SLOTNO As Byte ‘槽的编号,默认值为2
RACKNO As Byte ‘机架号,固定为0
End Type
Public Res As Long
Declare Function load_tool Lib "w95_s7.dll" (ByVal nr As Byte, ByVal dev As String, adr As plcadrtype) As Long
。。。。。。。 ‘省略其它定义函数。
Declare Function as200_vs_field_read Lib "w95_s7.dll" (ByVal nr As Long, ByVal anz As Long, value As Byte) As Long ‘读200PLC V存储区的数据函数
****在窗体中声明****
Private Sub Form_Load()
Dim buffer(1) As Byte
plcadr.adr = 2
plcadr.SEGMENTID = 0
plcadr.SLOTNO = 2
plcadr.RACKNO = 0
Res = load_tool(1, "S7ONLINE", plcadr)
Res = as200_vs_field_read(0, 2, buffer(0))
If Res <> 0 Then
MsgBox ("PLC连接错误")
gb_PlcConnected = False
Else
gb_PlcConnected = True
MsgBox ("plc连接正确")
Label1.Caption = buffer(0) * 256 + buffer(1)
End If
End Sub
通过以上程序可以正确读出PLC中的数据。之后可以扩展内容,编写一个监控软件。
相关源码把邮箱给 我 ,发到你邮箱,我的邮箱:acion@163.com本回答被提问者采纳
三菱PLC网关,西门子PLC网关,台达PLC网关是啥?
PLC网关,就是工业设备网关,目前就是有磨砂,物通博联等等公司有接这种网关 参考技术A 这个是卖那些以太网抉的人这样说的,因为每一种皮尔西都不一样 参考技术B 建议使用物通博联的网关,可以采集三菱,西门子,台达等PLC数据,做远程维护和上下载程序,具有边缘计算,协议解析的功能。 参考技术C 没懂啥网关?三菱plc的cpu有网口,还有E100网络模块,还有modbus网口。不知道你说的网关是啥玩意。以上是关于关于VB和西门子PLC的通讯的主要内容,如果未能解决你的问题,请参考以下文章