您现在所在的是:

串口通信

回帖:1个,阅读:990 [上一页] [1] [下一页]
802
kang05084113
文章数:12
年度积分:50
历史总积分:802
注册时间:2009/5/27
发站内信
发表于:2009/6/1 14:45:17
#0楼
用VB 实现西门子 200与计算机间的串行通信    下面这段程序对吗  谢谢 请您看一下


'(1) 控件参数的初始化。
Private Sub Form_Load()
MSComm1.CommPort = 2
MSComm1.Settings = "9600,E,7,2"
MSComm1.PortOpen = True

End Sub

Function fcs(ByVal InputStr As String) As String
'(2) 计算校验码FCS,计算FCS的VB自定义函数如下:
Dim Slen, i, Xorresult As Integer
Dim Tempfes As String
Slen = Len(InputStr) '求输入字符串长度
Xorresult = 0
For i = 1 To Slen
Xorresult = Xorresult Xor Asc(Mid$(InputStr, i, 1)) '按位异或
Next i
Tempfes = Hex$(Xorresult) '转化为16进制
If Len(Tempfes) = 1 Then Tempfes = "0" + Tempfes
fcs = Tempfes
End Function
Function ReadData(ByVal InputStr As String, ByVal Num As Integer) As String
'(3) 计算机与PLC通信程序。
Dim OutputStr As String
Dim InString As String
Dim ReturnStr As String
Dim EndString As String
Dim FCSString As String
Dim ReturnFCSString As String
MSComm1.InBufferCount = 0
OutputStr = InputStr + FSC(InputStr) + "*" '给出命令帧
MSComm1.Output = OutputStr + Chr$(13)  '向PLC传送命令帧
Do
DoEvents
Loop While MSComm1.InBufferCount < 15
InString = MSComm1.Input  '获取PLC的响应帧
'`结束码判断
EndString = Mid$(InString, Len(InString) - Num - 5, 2)
If EndString = "13" Then
ReadData = "Error"
Exit Function
ElseIf EndString = "14" Then
ReadData = "Error"
Exit Function
ElseIf EndString = "15" Then
ReadData = "Error"
Exit Function
ElseIf EndString = "18" Then
ReadData = "Error"
Exit Function
ElseIf EndString = "A3" Then
ReadData = "Error"
Exit Function
ElseIf EndString = "A8" Then
ReadData = "Error"
Exit Function
End If
'响应帧校验
EndString = Mid$(InString, 1, Len(InString) - 4)
ReturnFCSString = Mid$(InString, Len(InString) - 3, 2)
FCSString = fcs(EndString)
If FCSString <> ReturnFCSString Then
ReadData = "Error"
Exit Function
End If
ReturnStr = Mid$(InString, Len(InString) - Num - 3, Num)
ReadData = ReturnStr
End Function


我感觉好像错了    大家有这方面的资料吗  

可以帮我一下 给我一份好吗  谢谢大家了
802
kang05084113
文章数:12
年度积分:50
历史总积分:802
注册时间:2009/5/27
发站内信
发表于:2009/6/1 17:51:20
#1楼
有哪位大师有  s7-200的PLC与上位机PC机的通信程序吗   谢谢  帮帮忙吧!

关于我们 | 联系我们 | 广告服务 | 本站动态 | 友情链接 | 法律声明 | 非法和不良信息举报

工控网客服热线:0755-86369299
版权所有 工控网 Copyright©2024 Gkong.com, All Rights Reserved

31.2002