Link to home
Start Free TrialLog in
Avatar of nigelr99
nigelr99Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Accessing Serial Port in Access / VBA

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.
ASKER CERTIFIED SOLUTION
Avatar of als315
als315
Flag of Russian Federation 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
Avatar of nigelr99

ASKER

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!