PC与西门子PLC 通讯实例+源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
PC与西门子PLC 通讯实例+源代码
此实例为PC调用西门子第三方驱动libnodave.dll,通过VB6.0编写而成,可成功从SIEMENS SL系统PLC中读取其DB数据块,input输入/output输出映像区,Marker位存储区数据.【libnodave.dll 可百度下载,或者Mail我】Private Sub Command1_Click()
Dim peer As String
peer = Form1.Text1.Text
ph = openSocket(102, peer)
If ph = 0 Then
MsgBox "Connected Falied", vbInformation, "Siemens-Hugl" Else
If ph > 0 Then '需要更换新的端口
di = daveNewInterface(ph, ph, "IFI", 0, daveProtoISOTCP, daveSpeed187k)
'建立新的连接
res = daveInitAdapter(di) '初始化适配器
If res = daveResOK Then
dc = daveNewConnection(di, 2, Rack, Slot)
'建立新的PLC连接
res = daveConnectPLC(dc) '连接PLC
If res = daveResOK Then
MsgBox "Connected Seccussfully!", vbInformation, "Siemens-Hugl"
End If
End If
End If
End If
End Sub
Private Sub Command2_Click()
If dc <> 0 Then
res = daveDisconnectPLC(dc) '断开连接
Call daveFree(dc) '释放内存
dc = 0
End If
If di <> 0 Then
res = daveDisconnectAdapter(di) '断开适配器
Call daveFree(di) '释放内存
di = 0
End If
If ph <> 0 Then
res = closePort(ph) '关闭端口
ph = 0
End If
MsgBox "Disconnected Successfully!", vbInformation, "Siemens-Hugl"
End Sub
Private Sub Command3_Click()
Dim RetCode As Long
Dim PLCTime As T_S7_PLCTime
RetCode = S7_ReadPLCTime(dc, PLCTime)
If RetCode <> daveResOK Then
MsgBox " Read PLC Time failed!", vbInformation, "Siemens-Hugl"
Exit Sub
End If
bel1.Caption = "PLC Date is " & Format(PLCTime.Jahr, "0000") & "-" & Format(PLCTime.Monat, "00") & "-" & Format(PLCTime.Tag, "00")
bel2.Caption = "PLC Time is " & Format(PLCTime.Stunden, "00") & "h" & Format(PLCTime.Minuten, "00") & "m" & Format(PLCTime.Sekunden, "00") & "s" & Format(lisekunden, "0") & "ms"
End Sub
Private Sub Command4_Click()
res = daveReadBytes(dc, daveFlags, 0, 0, 16, 0)
If res = daveResOK Then
v1 = daveGetS32(dc)
MsgBox "MD0 is " & v1
v2 = daveGetS32(dc)
MsgBox "MD4 is " & v2
v3 = daveGetS32(dc)
MsgBox "MD8 is " & v3
v4 = daveGetFloat(dc)
MsgBox "MD12 is " & v4
v5 = daveGetFloatAt(dc, 12)
MsgBox "MD12 Float is " & v5
Else
MsgBox "Error", vbCritical, "SIEMENS-Hugl"
End If
End Sub
Private Sub Command5_Click()
res = daveReadBytes(dc, daveDB, 21, 3, 1, 0)
If res = daveResOK Then
MsgBox daveGetS32(dc)
Else
MsgBox "Fuck"
End If
End Sub
2015-12-29