发表于:2004/8/17 16:27:00
#0楼
本人用松下FP0的PLC传输数据,但用VB却读不出数据来,十分着急!哪位高人看看我的程序,帮帮忙,小弟不胜感激!以下是我的代码
模块:
Option Explicit
----------------------------------------------------------------------------------------
Public Function CalBCC(ByVal CC$) As String
Dim i As Integer
Dim tempresult As Byte
CC$ = LTrim(RTrim(CC$))
tempresult = 0
For i = 1 To Len(CC$)
tempresult = tempresult Xor Asc(Mid$(CC$, i, 1))
Next i
CalBCC = Right$("0" + Hex$(tempresult), 2)
End Function
----------------------------------------------------------------------------------------------
Public Function Form_Command(ByVal CC$) As String
Form_Command = LTrim(RTrim(CC$)) + CalBCC(CC$) + Chr$(&HD)
End Function
--------------------------------------------------------------------------------------------
Public Function Decimalstr(ByVal id_str) As Long
Dim str As String
Dim d As Integer
str = Mid$(id_str, 3, 2) + Mid$(id_str, 1, 2)
Dim i As Integer
Dim comp As Long
comp = 0
For i = 1 To 4 Step 1
If Mid$(str, i, 1) = "A" Then
d = 10
ElseIf Mid$(str, i, 1) = "B" Then
d = 11
ElseIf Mid$(str, i, 1) = "C" Then
d = 12
ElseIf Mid$(str, i, 1) = "D" Then
d = 13
ElseIf Mid$(str, i, 1) = "E" Then
d = 14
ElseIf Mid$(str, i, 1) = "F" Then
d = 15
Else
d = Val(Mid$(str, i, 1))
End If
comp = comp + d * 16 ^ (4 - i)
Next i
Decimalstr = comp
End Function
窗体程序:
Dim i As Integer
Dim comm As String
Dim comm1 As String
Dim str As String
Dim bcc As Byte
-----------------------------------------------------------
Private Sub Command1_Click() '打开端口
MSComm2.PortOpen = True
End Sub
------------------------------------------------------------
Private Sub Command2_Click() '写数据
If MSComm2.PortOpen = False Then
MsgBox "端口没有打开!正在打开"
MSComm2.PortOpen = True
End If
comm = "%01#WDD00101002001234"
MSComm2.Output = Form_Command(comm)
End Sub
-----------------------------------------------------------
Private Sub Command3_Click()
Unload Me
End Sub
--------------------------------------------------------------------
Private Sub Command4_Click() '读数据
comm1 = "%01#RDD0010100200"
MSComm2.Output = Form_Command(comm1)
leng = MSComm2.InBufferCount
str = MSComm2.Input
Text1.Text = Decimalstr(str)
End Sub
模块:
Option Explicit
----------------------------------------------------------------------------------------
Public Function CalBCC(ByVal CC$) As String
Dim i As Integer
Dim tempresult As Byte
CC$ = LTrim(RTrim(CC$))
tempresult = 0
For i = 1 To Len(CC$)
tempresult = tempresult Xor Asc(Mid$(CC$, i, 1))
Next i
CalBCC = Right$("0" + Hex$(tempresult), 2)
End Function
----------------------------------------------------------------------------------------------
Public Function Form_Command(ByVal CC$) As String
Form_Command = LTrim(RTrim(CC$)) + CalBCC(CC$) + Chr$(&HD)
End Function
--------------------------------------------------------------------------------------------
Public Function Decimalstr(ByVal id_str) As Long
Dim str As String
Dim d As Integer
str = Mid$(id_str, 3, 2) + Mid$(id_str, 1, 2)
Dim i As Integer
Dim comp As Long
comp = 0
For i = 1 To 4 Step 1
If Mid$(str, i, 1) = "A" Then
d = 10
ElseIf Mid$(str, i, 1) = "B" Then
d = 11
ElseIf Mid$(str, i, 1) = "C" Then
d = 12
ElseIf Mid$(str, i, 1) = "D" Then
d = 13
ElseIf Mid$(str, i, 1) = "E" Then
d = 14
ElseIf Mid$(str, i, 1) = "F" Then
d = 15
Else
d = Val(Mid$(str, i, 1))
End If
comp = comp + d * 16 ^ (4 - i)
Next i
Decimalstr = comp
End Function
窗体程序:
Dim i As Integer
Dim comm As String
Dim comm1 As String
Dim str As String
Dim bcc As Byte
-----------------------------------------------------------
Private Sub Command1_Click() '打开端口
MSComm2.PortOpen = True
End Sub
------------------------------------------------------------
Private Sub Command2_Click() '写数据
If MSComm2.PortOpen = False Then
MsgBox "端口没有打开!正在打开"
MSComm2.PortOpen = True
End If
comm = "%01#WDD00101002001234"
MSComm2.Output = Form_Command(comm)
End Sub
-----------------------------------------------------------
Private Sub Command3_Click()
Unload Me
End Sub
--------------------------------------------------------------------
Private Sub Command4_Click() '读数据
comm1 = "%01#RDD0010100200"
MSComm2.Output = Form_Command(comm1)
leng = MSComm2.InBufferCount
str = MSComm2.Input
Text1.Text = Decimalstr(str)
End Sub