Accessing Serial Port in Access / VBA

nigelr99
nigelr99 used Ask the Experts™
on
Hi,
I have a need to send simple text/hex values to a serial port from an Access application (which controls some relays incidentally). I've read many long articles regarding MSCOMM and variants thereof but I'm sure it shouldn't be that difficult.
From a previous answer to another question, I have arrived at the code below which is nearly there except Windows seems to be adding data at the start and end of my own.

Global Const stx = 4
Global Const etx = 15

Public Sub relay_on(n)
    cmd = 20
    mask = 2 ^ (n - 1)
    'param1 and 2 both zero
    chk = 231
    
    Open "COM3" For Output As #1
    Write #1, Chr$(stx) & Chr$(cmd) & Chr$(mask) & Chr$(0) & Chr$(0) & Chr$(chk) & Chr$(etx);
    Close #1
End Sub

Open in new window


I'm using a serial port 'sniffer' and when I run the procedure shown, I see the data I'm sending but also 22h at the start and then 22h,2Ch at the end - is there a way to remove this extra data? I guess it's to do with the port communication settings but I'm not sure how to configure those within VBA.

Any help would be appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Change Write to Print

Author

Commented:
Well I thought it should be straightforward but that certainly exceeded my expectation! I noticed afterwards that the extra characters were simply surrounding quotes and a comma but wouldn't have thought of changing write to print!
Many thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial