vb的问题(与电机有关)
用vb编一个控制界面,将起停等控制命令发过去之后能控制电机的起停,但是实际返回的数据却与要求返回的数据不匹配。比如说:发送起动的指令时"FA01520053FB",要求返...
用vb编一个控制界面,将起停等控制命令发过去之后能控制电机的起停,但是实际返回的数据却与要求返回的数据不匹配。比如说:发送起动的指令时"FA01520053FB",要求返回的数据是“FA00520052FB”,但是实际返回的数据却是00005200000052000000,请教到底是怎么回事?我的程序是这样的:
Private Sub Form_Load() '初始化
With MSComm1
MSComm1.CommPort = 2 '使用COM1
MSComm1.Settings = "9600,N,8,1" '设置通信口参数
MSComm1.InBufferSize = 512 '设置MSComm1接收缓冲区为512字节
MSComm1.OutBufferSize = 512 '设置MSComm1发送缓冲区为512字节
MSComm1.InputMode = comInputModeText '设置接收数据模式为文本形式
MSComm1.InputLen = 0 '设置Input 一次从接收缓冲读取字节数为全部
MSComm1.SThreshold = 1 '设置Output 一次从发送缓冲读取字节数为1
MSComm1.RThreshold = 1 '设置接收一个字节产生OnComm事件
If MSComm1.PortOpen = False Then '判断通信口是否打开
MSComm1.PortOpen = True '打开通信口
If Err Then '错误处理
MsgBox "串口通信无效"
Exit Sub
End If
End If
End With
End Sub
Private Sub Run_Click()
Dim Q_Run(5) As Byte '定义发送的启动的数据包
Dim S_data(5) As String '发送的数据
Dim Answer(5) As String '定义返回的数据
Dim Rec_Run As String '定义接收正确回复的数据包
Dim a As Integer '连续发送中用到的计数器
Dim i As Integer
Dim j As Integer
a = 1
S_data(0) = &HFA
S_data(1) = &H1 '去往驱动板
S_data(2) = &H52
S_data(3) = &H0
S_data(4) = S_data(1) Xor S_data(2) Xor S_data(3)
S_data(5) = &HFB
For i = 0 To 5
Q_Run(i) = Val(S_data(i))
Next i
MSComm1.Output = Q_Run
Shape1.FillColor = QBColor(12)
Shape2.FillColor = QBColor(7)
Dim s() As Byte
Dim stuff As String
Dim R_strdata As String
Dim k As Integer
Answer(0) = &HFA
Answer(1) = &H0
Answer(2) = &H52
Answer(3) = &H0
Answer(4) = Answer(1) Xor Answer(2) Xor Answer(3)
Answer(5) = &HFB
For j = 0 To 5
If Len(Answer(j)) = 1 Then
Rec_Run = Rec_Run & "0" & Answer(j)
Else
Rec_Run = Rec_Run + Answer(j)
End If
Next j
Do While a < 3
MSComm1.InputLen = 0
stuff = MSComm1.Input
s() = stuff
For k = 0 To UBound(s())
If Len(Hex(s(k))) = 1 Then
R_strdata = R_strdata & "0" & Val(Hex(s(k)))
Else
R_strdata = R_strdata & Val(Hex(s(k)))
End If
Next
If (R_strdata <> Rec_Run) Then
Timer1.Enabled = False
MSComm1.Output = Q_Run
a = a + 1
Timer1.Enabled = True
End If
Loop
MsgBox R_strdata
R_strdata = ""
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.InBufferCount = 0
End Sub 展开
Private Sub Form_Load() '初始化
With MSComm1
MSComm1.CommPort = 2 '使用COM1
MSComm1.Settings = "9600,N,8,1" '设置通信口参数
MSComm1.InBufferSize = 512 '设置MSComm1接收缓冲区为512字节
MSComm1.OutBufferSize = 512 '设置MSComm1发送缓冲区为512字节
MSComm1.InputMode = comInputModeText '设置接收数据模式为文本形式
MSComm1.InputLen = 0 '设置Input 一次从接收缓冲读取字节数为全部
MSComm1.SThreshold = 1 '设置Output 一次从发送缓冲读取字节数为1
MSComm1.RThreshold = 1 '设置接收一个字节产生OnComm事件
If MSComm1.PortOpen = False Then '判断通信口是否打开
MSComm1.PortOpen = True '打开通信口
If Err Then '错误处理
MsgBox "串口通信无效"
Exit Sub
End If
End If
End With
End Sub
Private Sub Run_Click()
Dim Q_Run(5) As Byte '定义发送的启动的数据包
Dim S_data(5) As String '发送的数据
Dim Answer(5) As String '定义返回的数据
Dim Rec_Run As String '定义接收正确回复的数据包
Dim a As Integer '连续发送中用到的计数器
Dim i As Integer
Dim j As Integer
a = 1
S_data(0) = &HFA
S_data(1) = &H1 '去往驱动板
S_data(2) = &H52
S_data(3) = &H0
S_data(4) = S_data(1) Xor S_data(2) Xor S_data(3)
S_data(5) = &HFB
For i = 0 To 5
Q_Run(i) = Val(S_data(i))
Next i
MSComm1.Output = Q_Run
Shape1.FillColor = QBColor(12)
Shape2.FillColor = QBColor(7)
Dim s() As Byte
Dim stuff As String
Dim R_strdata As String
Dim k As Integer
Answer(0) = &HFA
Answer(1) = &H0
Answer(2) = &H52
Answer(3) = &H0
Answer(4) = Answer(1) Xor Answer(2) Xor Answer(3)
Answer(5) = &HFB
For j = 0 To 5
If Len(Answer(j)) = 1 Then
Rec_Run = Rec_Run & "0" & Answer(j)
Else
Rec_Run = Rec_Run + Answer(j)
End If
Next j
Do While a < 3
MSComm1.InputLen = 0
stuff = MSComm1.Input
s() = stuff
For k = 0 To UBound(s())
If Len(Hex(s(k))) = 1 Then
R_strdata = R_strdata & "0" & Val(Hex(s(k)))
Else
R_strdata = R_strdata & Val(Hex(s(k)))
End If
Next
If (R_strdata <> Rec_Run) Then
Timer1.Enabled = False
MSComm1.Output = Q_Run
a = a + 1
Timer1.Enabled = True
End If
Loop
MsgBox R_strdata
R_strdata = ""
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.InBufferCount = 0
End Sub 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询