What is the best way to access a serial port from VBA? I have a need for some of our sales reps to be able to send a simple string over the serial port from an action button in PowerPoint.
Nov 2, 2015 - The purpose of this article is to demonstrate how you can perform serial port communication in the VBA (Visual Basic Applications – script. This forum has been a great help in getting me started with serial port communication in Excel/VBA. I have picked up a few tidbits about MSCOMM along the way and I am quite willing to share if anyone has an interest.
I don't commonly use VBA, especially for anything like this. Normally I would turn it into an application of some sort, but I actually don't think the idea is that bad. It will be a handy tool for them to demo this device with while on a projector and talking to other sales guys and non technical people. Also, this sales guy will have no problem making small modifications to the VBA or PowerPoint presentation, but would not do as well with recompiling a.NET application. I know we could do it through a batch file run from the presentation on the action, but that doesn't make me very happy. I figure we could probably access a COM object and run from there, but again I am not real up on the latest and greatest libraries to use in VBA, and it would also be nice to get a quick little primer in how to easily open, send and close the connection. Since this will need to be run on multiple people's computers, it would be nice if it would be easily transportable to other machines.
I should be able to say it has to run on Office 2007 and Windows XP. Compatibility with anything else would be a nice bonus though.
How should I go about handling this? Any good tips or tricks? Library recommendations? Here is a brief module of VBA code that can send and receive messages on a PC serial port. This is not very elegant, but it is simple and should work on modern versions of Excel and Windows. You are left on your own to expand the functionality and store or parse the messages. This just shows the low-level stuff to deal with the serial port.
The first 5 lines declare the millisecond 'Sleep' library function (based on Excel version). The SerialPort subroutine outlines the steps to open the port, transmit some data, receive some data, try again to receive some data (to show that it really does not run afoul of the 'end of file' error), and close the port. #If VBA7 Then ' Excel 2010 or later Public Declare PtrSafe Sub Sleep Lib 'kernel32' (ByVal Milliseconds As LongPtr) #Else ' Excel 2007 or earlier Public Declare Sub Sleep Lib 'kernel32' (ByVal Milliseconds As Long) #End If Public Sub SerialPort ' open a COM port, transmit a message, gather results, close the port. ' open the COM port as file #1 Debug.Print 'Open COM port 4' Open 'COM4:115200,N,8,1' For Binary Access Read Write As #1 transmit$ = Chr(2) + 'Hello, World.' + Chr(13) receiveDummy$ = ' ' transmit a message Put #1, transmit$ Debug.Print 'Message sent.'
![Excel Excel](http://www.pencomdesign.com/images/companies/1/blank_form.gif?1527105686121)
' wait a bit for a response Sleep 100 ' check for received message Debug.Print 'Look for incoming message.' On Error Resume Next Do While True receive$ = receiveDummy$ 'dummy value Input #1, receive$ If receive$ = receiveDummy$ Then Exit Do 'the string didn't change, so move on Debug.Print receive$ Loop On Error GoTo 0 ' do it again to show that the empty input queue doesn't stop the flow Debug.Print 'Look again for incoming message (should not stop on error).'
On Error Resume Next Do While True receive$ = receiveDummy$ 'dummy value Input #1, receive$ If receive$ = receiveDummy$ Then Exit Do 'the string didn't change, so move on Debug.Print receive$ Loop On Error GoTo 0 ' close the serial port Debug.Print 'Close COM port.' Close #1 Debug.Print 'Done.'