Click here to Skip to main content
15,662,926 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi From Excel VBA (Office365 on Win 10) I am trying to send a string to my new USB relay. It requires a Hex string "FF 01 01" to put the relay On, but it does not go On.

I use the VBA code, which I have copied from "somewhere".

I have also tried to end the string with Chr(13), no effect.

It opens and closes the port correct (it seems), but it does not turn the relay on.
When I use a small program (DockLight ver. 2.3) to send the Hex string "FF 01 01" it works perfect, the relay goes On.

What am I doing wrong in VBA?

Best regards

What I have tried:

Sub SendHexToCom()
COMPort = FreeFile (I dont know what this does)
Close #COMPort
Open "COM8:9600,N,8,1" For Binary Access Read Write As #COMPort
VarString$ = "FF 01 01"

Put #COMPort, , VarString$
Close #COMPort
End Sub
Updated 27-Nov-21 5:14am

VarString$ = "FF 01 01"

That is what you are doing wrong. You are sending the ASCCI character string FF<space>01<space>01 instead of the actual hex (i.e. 8-bit) bytes FF0101. You need to use the VBA HEX function to get the correct values, something like:
Put #COMPort, , HEX(255) # FF
Put #COMPort, , HEX(1)   # 01
Put #COMPort, , HEX(1)

I think I may have misread the documentation, you can send the hex characters directly by:
Put #COMPort, , &HFF
Put #COMPort, , &H01
Put #COMPort, , &H01


And as an aside, if you don't understand some code that you download from "somewhere", then be aware that it could do all sorts of damage to your system.
Share this answer
Member 15445727 26-Nov-21 9:47am    
Thank you for the input.
It does not work with
Put #COMPort, , &HFF
Put #COMPort, , &H01
Put #COMPort, , &H01

You are right about unknown code :-)
Richard MacCutchan 26-Nov-21 10:34am    
Sorry, but that is the best I can suggest. You could always ask the person who wrote the code that you copied.
Maciej Los 26-Nov-21 10:47am    
Richard MacCutchan 26-Nov-21 10:51am    
Thanks Maciej; even though it may not work.
The port may open, but are the parameters you specified the ones your device is expecting?

You opened the port with a baud rate of 9600 bps, no parity, 8 bits, 1 stop bit. You're going to have to consult the documentation on your device to see what parameters it's expecting. Get any of those parameters wrong and your communication won't work.
Share this answer
Thank you, Dave
The documentation says 9600 bps, no parity, 8 bits, 1 stop bit.
And command to activate relay is Hex FF 01 01.

I really dont know what to do else.
Share this answer
Richard MacCutchan 27-Nov-21 11:53am    
This is not a solution. If you want to reply to a posted solution then use the Have a Question or Comment? link below the posted message.
Member 15445727 28-Nov-21 3:52am    
Thank you, Richard
Sorry for the disaster :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900