发表于: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
我感觉好像错了 大家有这方面的资料吗
可以帮我一下 给我一份好吗 谢谢大家了
'(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
我感觉好像错了 大家有这方面的资料吗
可以帮我一下 给我一份好吗 谢谢大家了