Link to home
Start Free TrialLog in
Avatar of Swafnil
SwafnilFlag for Germany

asked on

MSCOMM and sending Hex values (like "#0A")

Hi experts,

I got a new quest for you:
---------------------------------
I'd like to use MSCOMM in my Excel VBA project to acquire data from a handheld on COM1.
First I have to send "#04#0A" (Hex 4, Hex 10) to the device which then replies with its program version.
I tried all solutions listed in google but all tries just send something like "#4#10" without zeroes to the device instead of the above stated.
I currently use a COM port sniffer ("Advanced Serial Port Monitor") - and it's definitely able to send the needed "#04#0A" ...  the handheld then replies correctly.
How do I have to send Hex codes? Can you perhaps just list your snippets where you send hex to a COM-Port?
Thanks for your help and knowledge!

Sascha
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi Swafnil,

From your description, it may be that you have to send a string rather than the bytes themselves. Have you tried simply:

MSComm1.Output "#04#10"

Just a thought.

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
Either specify a string (but a device would only interpret it is ascii value I would think) or send a byte array, e.g.,

Dim byteArray(1 To 2)
byteArray = CByte(&H4)
byteArray = CByte(&HA)

MSComm.Output = byteArray
try

MSComm1.Output = CByte(&H4) & CByte(&HA)
Avatar of GivenRandy
GivenRandy

Sounds like you want to use the Hex$() function to turn the numeric value into a hex string (e.g., 10 becomes "A").
Avatar of Swafnil

ASKER

First of all, thanks for your really fast help!
I increased the points, because I think it's hard to solve so a solution should be honored accordingly...

Sad but true, none of your ideas helped in my case:

- my tries with "Advanced Serial Port monitor":
that's ASPM's interface and how it should look like, the handheld replies correctly:
(picture: <a href="http://www.ars-aventuris.de/tries/ASPM.jpg">Picture 1a</a>)
(picture: <a href="http://www.ars-aventuris.de/tries/correct.jpg">Picture 1b</a>)

- TimCottee's solution:
submitted the right value, but the values where handled as a 6 byte long string, instead of only two hex values ...
(picture: <a href="http://www.ars-aventuris.de/tries/TimCottee.jpg">Picture 2</a>)

-AzraSound's solution:
good idea, but I had to modify some statements, and it still gives me an error "mismatching properties value" (translated from german, so this won't be exactly your english error message). This is my portion of source code that I used:
[CODE]
  MSComm.PortOpen = True
  Dim byteArray(1 To 2)
  byteArray(1) = CByte(&H4)
  byteArray(2) = CByte(&HA)
  MSComm.Output = byteArray
  MSComm.PortOpen = False
[/CODE]

- EddYKT's solution:
The result of this try would probably be the same as in AzraSound's version, only "410" are sent ...
(picture: <a href="http://www.ars-aventuris.de/tries/EDDYKT.jpg">Picture 3</a>)

- GivenRandy's soultion:
Hex$() also removes leading zeroes in a hex value, resulting in an output of "4A"
(picture: <a href="http://www.ars-aventuris.de/tries/GivenRandy.jpg">Picture 4</a>)

Any other ideas?

Sascha
After using Hex$() to a substring, you can check if the Len() is 1 or 2 characters -- if it is 1 character, you can put a "0" at the front of the substring and then concatenate it.
Something like this:

---
    Dim S As String
    Dim SubS As String
    Dim Value1 As Long
    Dim Value2 As Long
   
    Value1 = 4
    Value2 = 10
   
    S = "#"
    SubS = Hex$(Value1)
    If (Len(SubS) < 2) Then
        SubS = "0" & SubS
    End If
    S = S & SubS
    SubS = Hex$(Value2)
    If (Len(SubS) < 2) Then
        SubS = "0" & SubS
    End If
    S = S & SubS
    MSComm1.Output = 2
---
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Right -- in most cases. Perhaps I mistook what the questioner was asking. If you need binary output, do as AzraSound says. If you need a string representation (often called "AsciiHex"), use the strings that I mentioned. It all depends on the protocol you need to interface to the unit.
Avatar of Swafnil

ASKER

I've forgotten to post my results after some more tests, and I'm sorry for this!
I had a problem with my comm component, and AzraSound's tip was the right solution.
Thank you!

Sascha