Forum Discussion
Serial COM Ports in Excel VBA
First of all, thanks for the VBA code! I'm one of those infrequent VAB user and use it occasionally. I do have a problem using the code to write/read from an instrument connected to a PC via serial port. The problem is that the analytical instrument does not start sampling when I send "SS" using TRANSMIT_COM_PORT with VBA in Access to the COM port, whereas it does if I use Putty or a Windows form application previously written with VB.net. I don't know why. It seems to me the transmission mechanism is different. Any advice will be much appreciated!
- serialcommsOct 11, 2023Copper Contributor
Hello and thanks for your interest in SerialComms VBA.
You may have a timing problem between VBA and your instrument.
Modern PC is simply far too fast for relatively slow serial ports and attached devices.
Try adding a delay between starting the COM port and sending the Start Sampling command.
Add a second delay between sending the command and waiting for a response.
There's an example function at the end of the listing below which shows how to do that.
https://github.com/Serialcomms/Serial-Ports-in-VBA-new-for-2022/blob/main/SERIAL_PORT_VBA.bas
Regards,
SerialComms
- Andrew_M395Jan 06, 2024Copper ContributorIt is great that you have provided these functions. However I am having a similar issue talking to an Arduino. Neither the example() or the ..open, ..transmit, ..receive in a subroutine work. However if I run the functions in the immediate window, then it does work. If I insert breakpoints on my subroutine at each function then again it does work. However if I add delays using either sleep() or the waitcomport() function it does not work. If I step through the breakpoints too fast in my subroutine I can get it to not work, so it is clearly a time related issues, but I cannot see why the delays within example() or the delay in my subroutine do not work for me.
Regards Andrew Mellon- Andrew_M395Jan 06, 2024Copper ContributorI have solved this: I needed to add a delay between the open port and transmit in my own subroutine. This despite the fact that the transmit reported sending the data sucessfully. I then went back to the example and invcreased the delay before If Port_Started(Port_Number) Then to 2000 and it worked.
Thanks again for the code.
Regards Andrew M
- LeoKingOct 12, 2023Copper ContributorThanks for your reply. I figured it out - it's as simple as sending a carriage return at the end of the command string!