VB 串口接收数据
有什么方法可以把两个字节组合起来,因为一个字节最多只能255个数例如我用单片机发送2521到VB里面可是VB一次只能接一个字节所以我就分两次发!!例如我在单片机写到!I=...
有什么方法可以把两个字节组合起来,因为一个字节最多只能255个数 例如我用单片机发送2521到VB里面 可是VB一次只能接一个字节 所以我就分两次发!!例如我在单片机写到!
I=2521
第一个字节I&0XFF=09 第二个字节 (I&0XFF00)>>8=D9
我想怎么在用VB把这个两个字节组合成2521????? 展开
I=2521
第一个字节I&0XFF=09 第二个字节 (I&0XFF00)>>8=D9
我想怎么在用VB把这个两个字节组合成2521????? 展开
展开全部
' 描述:取高字节
' 参数:high:数值Long
' 返回:高字节
Public Function hibyte(high As Long)
Dim D
D = high And &HFF00
D = D \ 256
If D < 0 Then D = D + 256
hibyte = D
End Function
' 描述:取低字节
' 参数:high:数值Long
' 返回:低字节
Public Function lowbyte(high As Long)
Dim D
D = high And &HFF
lowbyte = D
End Function
'高低字节组合
Public Function Join2Byte(lowbyte As Byte, highbyte As Byte)
Dim c As Integer
c = highbyte
If c > 127 Then
c = (c - 256) * 256
Else
c = c * 256
End If
Join2Byte = c + lowbyte
End Function
例如,这是温控仪的代码。。
' 写入 pa=给定值 paNum=参数代号 ADDR=仪表地址 MSCommX=通信控件
Sub cmdWrite(pa As Long, paNum As Integer, ADDR As Integer, MSCommX As MSComm)
Dim X(0 To 7) As Byte
Dim crc As Long ' 效验码
MSCommX.InBufferCount = 0
crc = CLng((paNum * 256) + 67 + pa + ADDR) ' 效验码计算公式
X(0) = ADDR + &H80
X(1) = X(0)
X(2) = &H43
X(3) = paNum
X(4) = CByte(lowbyte(pa))
X(5) = CByte(hibyte(pa))
X(6) = CByte(lowbyte(crc))
X(7) = CByte(hibyte(crc))
MSCommX.InBufferCount = 0
MSCommX.Output = X
End Sub
*******************
希望能采纳,全是自己写的程序代码
' 参数:high:数值Long
' 返回:高字节
Public Function hibyte(high As Long)
Dim D
D = high And &HFF00
D = D \ 256
If D < 0 Then D = D + 256
hibyte = D
End Function
' 描述:取低字节
' 参数:high:数值Long
' 返回:低字节
Public Function lowbyte(high As Long)
Dim D
D = high And &HFF
lowbyte = D
End Function
'高低字节组合
Public Function Join2Byte(lowbyte As Byte, highbyte As Byte)
Dim c As Integer
c = highbyte
If c > 127 Then
c = (c - 256) * 256
Else
c = c * 256
End If
Join2Byte = c + lowbyte
End Function
例如,这是温控仪的代码。。
' 写入 pa=给定值 paNum=参数代号 ADDR=仪表地址 MSCommX=通信控件
Sub cmdWrite(pa As Long, paNum As Integer, ADDR As Integer, MSCommX As MSComm)
Dim X(0 To 7) As Byte
Dim crc As Long ' 效验码
MSCommX.InBufferCount = 0
crc = CLng((paNum * 256) + 67 + pa + ADDR) ' 效验码计算公式
X(0) = ADDR + &H80
X(1) = X(0)
X(2) = &H43
X(3) = paNum
X(4) = CByte(lowbyte(pa))
X(5) = CByte(hibyte(pa))
X(6) = CByte(lowbyte(crc))
X(7) = CByte(hibyte(crc))
MSCommX.InBufferCount = 0
MSCommX.Output = X
End Sub
*******************
希望能采纳,全是自己写的程序代码
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询