troubleshooting Question

How to convert Hex text in Ms Access VBA to Binary code

Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScFlag for Zambia asked on
Microsoft AccessVBA
5 Comments1 Solution170 ViewsLast Modified:
Dear All;
I need help to convert some Hex text to binary code using Ms Access VBA, see below:
Header1 :  0X1A
Header2 : OX5D
CmdID: (0x01,0x02 & 0x03)
Length : ( Content to big-endian)
I have tried to convert the above string using the online convertor, see the screen shoot below, for example
(1)       0x01 = 00110000 01111000 00110000 00110001
The objective here is send a command string to the serial port in the recommended format below:
<Header1><Header2><CmdID><Length><Content><CRC>
Both the content & CRC is already done , the problem is the four ( Header1 to Length)

Simple VBA code:

Dim Header1 as string, Header2 as string , CmdID as string, length as string, Content as string , CRC as string
Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
Dim lngStatus As Long
Dim strError  As String
Dim strData   As String

Header1 = XXXX (converted to binary code)
Header2 = XXXX (converted to binary code)
CmdID = XXXX (converted to binary code)
Length = XXXX (converted to binary code)
Content =????? (converted to binary code all in Json)
CRC = XXXX (converted to binary code)
strData = <Header1><Header2><CmdID><Length><Content><CRC>
  ' Initialize Communications
    lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), _
        "baud=9600 parity=N data=8 stop=1")
    
    If lngStatus <> 0 Then
	' Handle error.
        lngStatus = CommGetError(strError)
	MsgBox "COM Error: " & strError
    End If
    

    ' Set modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, True)
    lngStatus = CommSetLine(intPortID, LINE_DTR, True)

    ' Write data to serial port.
    lngSize = Len(strData)
    lngStatus = CommWrite(intPortID, strData)
    If lngStatus <> lngSize Then
    ' Handle error.
    End If
That is what is required to communicate to this serial device.



string-to-binary.png
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros