好吧,我想你可以讀取ASCII部分,因爲你設置了InputMode = comInputModeText
。取而代之的是,設置InputMode = comInputModeBinary
。
正如另一個答案已經提到,聲明此功能:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
比方說您收到這樣的緩衝作爲你的input
:
Dim buf(0 To 10) As Byte
' receiving...
buf(0) = &H62
buf(1) = &H69
buf(2) = &H6E
buf(3) = &H61
buf(4) = &H72
buf(5) = &H79
buf(6) = &H8
buf(7) = &HD0
buf(8) = &HF
buf(9) = &H49
buf(10) = &H40
你的第一個值是6個字節長,你可以將它轉換爲一個字符串,你的第二個值是1個字節長,所以你可以將它轉換爲一個字節(或者整數或長整數,無論你想要什麼)。你的產值是4個字節長,所以它將適合一個單一的。
Dim t as String, b As Byte, s As Single
t = StrConv(LeftB(buf, 6), vbUnicode)
Call CopyMemory(VarPtr(b), VarPtr(buf(6)), 1)
Call CopyMemory(VarPtr(s), VarPtr(buf(7)), 4)
Debug.Print t, b, s ' will print binary 8 3,14159
只要將所有內容都視爲二進制,ASCII對二進制字節值來說只是一個常見的情況。谷歌「VB6轉換字節爲單一」,很多很多點擊。 –
你已經設置了'InputMode = comInputModeText'? – deblocker