variant 如何转为integer??

bubbyyby 2008-03-25 09:39:37
从串口读回数据aa = MSComm1.Input
想把他传递给process函数进行处理
请问为什么在传递0X2F、0XC9时提示“类型不匹配”,但是在传递0X38时却正常?
------------------------------------------------------------------------
Dim bbb As Variant
Dim aa As Variant

MSComm1.InputMode = 1 '二进读取制模式
If MSComm1.InBufferCount > 0 Then
aa = MSComm1.Input
ABuffer = aa
i = Hex(ABuffer(UBound(ABuffer)))
bbb = i
Process (bbb)

----------------------------------------------------------------------

Function Process(rec As Variant)
Dim CodeType
Static counttt

If (rec And &HC0) = &HC0 Then
CodeType = 1
counttt = 1
If (rec And &H20) = &H20 Then LeftKeyDown = True
If (rec And &H10) = &H10 Then RightKeyDown = True
If (rec And &H8) = &H8 Then Xup = True
If (rec And &H4) = &H4 Then Xdown = True
If (rec And &H2) = &H2 Then Yup = True
If (rec And &H1) = &H1 Then Ydown = True
GoTo label
End If
If (counttt = 1) Then
If (rec > 63) Then GoTo label
counttt = 2
X = rec And &H3F
GoTo label
End If
If (counttt = 2) Then
If (rec > 63) Then GoTo label
counttt = 3
Y = rec And &H3F
GoTo label
End If
label:
End Function
----------------------------------------------------------------
代码很幼稚,本人没学过VB,昨晚因为要实现一个上位机程序才在网站拼凑的,能实现我要求的功能就好
...全文
191 点赞 收藏 15
写回复
15 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
[Quote=引用 10 楼 bubbyyby 的回复:]
我串口发送的值最大是0XFF啊, i不可能超过32768的.
调试了,i=0XC9,还是一样的错误[/Quote]

0XC9是发送过来的字符?还是你自己转换出来的字符?

如果是发过来就这样的字符,那就得转换一下

把所有的0X都替换成&H,就可以了

VB里面表示十六进制是在前面加&H,比如你这0XC9应该就是&HC9.

然后直接I=cint("&HC9")就可以了.
回复
bubbyyby 2008-03-26
找到规律了。.
只要i 带有十六进制中的 A、B、C、D、E、F其中一个字母就不行

我对VB十分不了解,哪位大虾帮帮忙
多谢了
回复
bubbyyby 2008-03-26
这里i并没有定义他是什么类型
那i是什么类型?
VB不是很安全啊,容易把人搞糊涂了
回复
bubbyyby 2008-03-26
[Quote=引用 9 楼 myjian 的回复:]
引用 8 楼 bubbyyby 的回复:
引用 1 楼 chenjl1031 的回复:
variant 如何转为integer?? 用CInt函数

我把bbb定义为integer类型以后
i = Hex(ABuffer(UBound(ABuffer)))
bbb=Cint(i)
还是提示类型不匹配


如果bbb=Cint(i)这句提示的,那就是I的值已经大于了32768.

你调试一下,下个断点.
[/Quote]
我串口发送的值最大是0XFF啊, i不可能超过32768的.
调试了,i=0XC9,还是一样的错误
回复
[Quote=引用 8 楼 bubbyyby 的回复:]
引用 1 楼 chenjl1031 的回复:
variant 如何转为integer?? 用CInt函数

我把bbb定义为integer类型以后
i = Hex(ABuffer(UBound(ABuffer)))
bbb=Cint(i)
还是提示类型不匹配
[/Quote]

如果bbb=Cint(i)这句提示的,那就是I的值已经大于了32768.

你调试一下,下个断点.
回复
zdingyun 2008-03-26
Option Explicit
Dim sTemp As String
Dim yTemp() As Byte
Dim Hsum As Integer
Dim strData As String
Dim intSj() As Integer
Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 1
MSComm1.InputMode = comInputModeBinary '二进读取制模式
MSComm1.RThreshold = 1
MSComm1.SThreshold = 0
MSComm1.PortOpen = True
End Sub

Private Sub MSComm1_OnComm()
Dim ss As Variant
Dim ww As String '定义临时变量
Dim i, a As Integer
Select Case MSComm1.CommEvent 'Select Case 语句分解事件
Case comEvReceive
ss = MSComm1.Input
sTemp = StrConv(ss, vbUnicode) '转换为Unicode
yTemp = StrConv(sTemp, vbFromUnicode) '转换为Byte
Hsum = UBound(yTemp)
Dim j As Integer
For j = 0 To Hsum '转换为16进制
If Len(Hex(yTemp(j))) = 1 Then
strData = strData & "0" & Hex(yTemp(j))
Else
strData = strData & Hex(yTemp(j))
End If
Next
zh '转换为Integer
End Select
End Sub

Public Sub zh()
Dim l As Integer
ReDim intSj((Len(strData) / 4) - 1)
For l = 1 To Len(strData) Step 4
intSj((l - 1) / 4) = Val("&H" & Mid(strData, l, 4))
Print intSj(l / 4)
Next l
End Sub
回复
舉杯邀明月 2008-03-26
[Quote=引用 13 楼 myjian 的回复:]
引用 10 楼 bubbyyby 的回复:
我串口发送的值最大是0XFF啊, i不可能超过32768的.
调试了,i=0XC9,还是一样的错误

0XC9是发送过来的字符?还是你自己转换出来的字符?

如果是发过来就这样的字符,那就得转换一下

把所有的0X都替换成&H,就可以了

VB里面表示十六进制是在前面加&H,比如你这0XC9应该就是&HC9.

然后直接I=cint("&HC9")就可以了.
[/Quote]

用这种方法应该可以了.......
回复
bubbyyby 2008-03-25
[Quote=引用 1 楼 chenjl1031 的回复:]
variant 如何转为integer?? 用CInt函数
[/Quote]
我把bbb定义为integer类型以后
i = Hex(ABuffer(UBound(ABuffer)))
bbb=Cint(i)
还是提示类型不匹配
回复
bubbyyby 2008-03-25
[Quote=引用 4 楼 qianjin036a 的回复:]

Dim xx As Variant, yy As Variant
xx = &HC9
yy = Process(xx)

也一样.当xx=&HC9时,Xup=true Ydown=true
当xx=&H2F时,所有键都不置位.
[/Quote]

贴上鼠标控制模块
----------------------------------------------------------------------------------------
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Declare Function Getasynckeystate Lib "user32" Alias "GetAsyncKeyState" (ByVal VKEY As Long) As Integer
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Type POINTAPI
x As Long
y As Long
End Type
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN = &H8
Public Const MOUSEEVENTF_RIGHTUP = &H10

Function WriteINI(sSection As String, sKeyName As String, sNewString As String, sFileName) As Integer


Dim r
r = WritePrivateProfileString(sSection, sKeyName, sNewString, sFileName)
End Function

Function ReadINI(Section, KeyName, filename As String) As String


Dim sRet As String
sRet = String(255, Chr(0))
ReadINI = Left(sRet, GetPrivateProfileString(Section, ByVal KeyName, "", sRet, Len(sRet), filename))
End Function

Function LeftC(x As Long, y As Long)
mouse_event MOUSEEVENTF_LEFTDOWN, x, y, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, x, y, 0, 0
End Function

Function RightC(x As Long, y As Long)
mouse_event MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0
mouse_event MOUSEEVENTF_RIGHTUP, x, y, 0, 0
End Function

Function DblC(x As Long, y As Long)
mouse_event MOUSEEVENTF_LEFTDOWN, x, y, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, x, y, 0, 0



mouse_event MOUSEEVENTF_LEFTDOWN, x, y, 0, 0
mouse_event MOUSEEVENTF_LEFTUP, x, y, 0, 0
End Function


-------------------------------------------------------------------------------------------

试试跟我的代码一样的调用,可以吗??
回复
bubbyyby 2008-03-25
[Quote=引用 3 楼 qianjin036a 的回复:]
引用 1 楼 chenjl1031 的回复:
variant 如何转为integer?? 用CInt函数

用不着转,他本来就是二进制运算.估计楼主原来是学汇编的.
[/Quote]

我是搞单片机、嵌入式的。
一个项目要通过触摸屏控制PC的鼠标,搞了一个星期的Microsoft串口鼠标通信协议,一直没有结果
索性自己写一个程序,自己定协议
回复
bubbyyby 2008-03-25
[Quote=引用 2 楼 qianjin036a 的回复:]
不会吧,能运行的啊!

Dim xx As Byte, yy As Variant
xx = &HC9
yy = Process(xx)

调用函数,没出错.

对了,你的函数怎么没有返回值呢?
[/Quote]

也许这样是可以的,但是放到串口数据接收函数就不行了
你定义XX 为 BYTE 类型,但是如果我定义bbb为Byte类型

bbb = i 这一句
就会提示类型不匹配
函数不一定要有返回值的吧。 函数里面操作的都是全局变量
回复
-晴天 2008-03-25

Dim xx As Variant, yy As Variant
xx = &HC9
yy = Process(xx)

也一样.当xx=&HC9时,Xup=true Ydown=true
当xx=&H2F时,所有键都不置位.
回复
-晴天 2008-03-25
[Quote=引用 1 楼 chenjl1031 的回复:]
variant 如何转为integer?? 用CInt函数
[/Quote]
用不着转,他本来就是二进制运算.估计楼主原来是学汇编的.
回复
-晴天 2008-03-25
不会吧,能运行的啊!

Dim xx As Byte, yy As Variant
xx = &HC9
yy = Process(xx)

调用函数,没出错.

对了,你的函数怎么没有返回值呢?
回复
东方之珠 2008-03-25
variant 如何转为integer?? 用CInt函数
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7489

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-03-25 09:39
社区公告
暂无公告