Serial Port Interfacing in Excel

An example how to use the StrokeReader ActiveX to control a serial port from Excel.

How to Proceed

Serial port interfacing in Excel is a powerful tool that allows users to connect external devices, such as temperature sensors, barcode scanners, or microcontrollers, directly to Excel for real-time data collection and analysis.

By using the serial port to send and receive data, users can seamlessly import measurements or process control information into Excel spreadsheets, enabling efficient data processing in automation, industrial equipment monitoring, and lab experiments.

Furthermore, serial port interfacing can be easily implemented with VBA code and StrokeReader serial port ActiveX control to enhance workflow efficiency for various data-driven tasks.

How to Modify the Serial Port Settings

1. Switch to the Design mode on the Developer tab:

Switching to the Design mode

2. Right-click the serial port object and then choose Properties:

The context menu of the serial port ActiveX

3. In the Properties box, set:

NOTE: These are the most common values used to set up the data transfer with the most of barcode scanners. Please always consult to your remote device's programming manual, if unsure.

The serial port Properties box

The VBA Code

While in the Design mode, double-click the ActiveX object. Paste the following code into the VBA window.

Uncheck the Design mode button (the CommEvent Sub does not receive serial port events in the Design mode).

Private Sub StrokeReader1_CommEvent(ByVal Evt As StrokeReaderLib.Event, ByVal data As Variant) Select Case Evt Case EVT_DISCONNECT Debug.Print "Disconnected" Case EVT_CONNECT Debug.Print "Connected" Case EVT_DATA buf = StrokeReader1.Read(TEXT) 'Use BINARY to receive a byte array Debug.Print buf End Select End Sub ' Use this to connect and set the port properties from the code Sub connect() StrokeReader1.Port = 22 StrokeReader1.BaudRate = 9600 StrokeReader1.Parity = NOPARITY StrokeReader1.StopBits = ONESTOPBIT StrokeReader1.DsrFlow = False StrokeReader1.CtsFlow = False StrokeReader1.DTR = True StrokeReader1.RTS = True StrokeReader1.Connected = True If StrokeReader1.Error Then Debug.Print StrokeReader1.ErrorDescription End If End Sub ' Use this to send data to the remote device Sub send() StrokeReader1.Send "ABCD" ' A text string Dim x(3) As Byte 'A byte array x(1) = 1 x(2) = 2 x(3) = 3 StrokeReader1.Send x End Sub

Open the Immediate Window (press Ctrl+G in VBA) to see output of the Debug.Print statements.