Home > Software engineering >  Using VB to get hexadecimal array to a serial port communication, how to deal with it
Using VB to get hexadecimal array to a serial port communication, how to deal with it

Time:10-09

Such as use MSCOMM communication after get a hexadecimal values such as "41 0 c 0 f A0," how can handle to turn it into a normal engine speed? The specific procedures, thank you

CodePudding user response:

Are you sure you received is a string (do you use comInputModeText mode, or is not set an InputMode=comInputModeBinary)?
Are you sure is to use a space between each two characters separated?
What is you want to convert the rotational speed of numerical data type, is Long?
Both Dim strData
 As String, lngData As Long 

0 c 0 f A0 strData="https://bbs.csdn.net/topics/41"
StrData=https://bbs.csdn.net/topics/Replace (strData, ""," ")
LngData=https://bbs.csdn.net/topics/CLng (" & amp; H "& amp; StrData)

CodePudding user response:

Received a hexadecimal array, using hexadecimal model, numerical type Long and integer should speed can be, what I want is "how to use VB to handle a use MSCOMM communication receive hexadecimal array, convert him to Long or integer type" such a program code, can you help me?

CodePudding user response:

refer to the second floor jamesarere response:
received a hexadecimal array, using hexadecimal model, numerical type Long and integer should speed can be, what I want is "how to use VB to handle a use MSCOMM communication receive hexadecimal array, convert him to Long or integer type" such a program code, can you help me?

Hexadecimal in the sense that there is the essence of which is 2 into the system, may refer to: will convert the Long a Byte array type and reverse
[code=vbOption Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any Source, As Any, ByVal Length As Long)
Dim Buffer (3) As Byte
Private Sub LngToBin_Click ()
Dim As Integer I
Dim hexData As String
Dim As Long a
A=Val (Text1)
CopyMemory Buffer (0), 4 a,
For I=0 To UBound (Buffer)
The Debug. Print Buffer (I)
Next
End Sub
Private Sub BinToLng_Click ()
Dim sinStr As String
Dim sinSj As Long
Dim As Integer I
CopyMemory ByVal VarPtr (sinSj), ByVal VarPtr (Buffer (0)), 4
Text2=sinSj
End Sub
Private Sub Form_Load ()
Text1=""
Text2=""
End Sub] [/code]

CodePudding user response:

 Option Explicit 
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any Source, As Any, ByVal Length As Long)
Dim Buffer (3) As Byte
Private Sub LngToBin_Click ()
Dim As Integer I
Dim hexData As String
Dim As Long a
A=Val (Text1)
CopyMemory Buffer (0), 4 a,
For I=0 To UBound (Buffer)
The Debug. Print Buffer (I)
Next
End Sub
Private Sub BinToLng_Click ()
Dim sinStr As String
Dim sinSj As Long
Dim As Integer I
CopyMemory ByVal VarPtr (sinSj), ByVal VarPtr (Buffer (0)), 4
Text2=sinSj
End Sub
Private Sub Form_Load ()
Text1=""
Text2=""
End Sub
  • Related