VB串口发送一串16进制跟接收问题
PrivateSubCommand6_Click()IfMSComm1.PortOpen=FalseThenLabel6.Caption="Openaportfirst"...
Private Sub Command6_Click()
If MSComm1.PortOpen = False Then
Label6.Caption = "Open a port first"
Label6.ForeColor = vbRed
Exit Sub
End If
Dim number As Integer
Dim outbyte(0) As Byte
number = CByte(Val("&H " & "bb"))
outbyte(0) = number
MSComm1.OutBufferCount = 0
MSComm1.Output = outbyte
End Sub
我text3内容是整数的0~8
text4是0~5(步进0.1的数,例如1.9 2 2.1 ....)
我想按command6就发送16进制的BB text3内容 text4内容,
例如text3为3 text4为3.5 就发送16进制 bb0335
text3为0 text4为0 就发送16进制 bb0000
text3为0 text4为3 就发送16进制 bb0030
command6的代码应该怎么改? 展开
If MSComm1.PortOpen = False Then
Label6.Caption = "Open a port first"
Label6.ForeColor = vbRed
Exit Sub
End If
Dim number As Integer
Dim outbyte(0) As Byte
number = CByte(Val("&H " & "bb"))
outbyte(0) = number
MSComm1.OutBufferCount = 0
MSComm1.Output = outbyte
End Sub
我text3内容是整数的0~8
text4是0~5(步进0.1的数,例如1.9 2 2.1 ....)
我想按command6就发送16进制的BB text3内容 text4内容,
例如text3为3 text4为3.5 就发送16进制 bb0335
text3为0 text4为0 就发送16进制 bb0000
text3为0 text4为3 就发送16进制 bb0030
command6的代码应该怎么改? 展开
1个回答
展开全部
首先在ford_load事件里要把MSComm1的发送接收方式改成字节接收方式,即MSComm1.InputMode = comInputModeBinary
Command6里的发送事件改为:
dim outbyte(2) as byte
outbyte(0)=&HBB
outbyte(1)="&H0" & trim(text3.text)
outbyte(2)="&H" & format(10*val(text4.text),"00")
MSComm1.OutBufferCount = 0
mscomm1.output=outbyte
Command6里的发送事件改为:
dim outbyte(2) as byte
outbyte(0)=&HBB
outbyte(1)="&H0" & trim(text3.text)
outbyte(2)="&H" & format(10*val(text4.text),"00")
MSComm1.OutBufferCount = 0
mscomm1.output=outbyte
追问
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询