Excel vba com порт

VBA Serial Port routines for Microsoft Office

New for 2022 — Windows 10, Office 2019 (Excel, Word, Access)

Getting Serial (COM) Ports working as intended in VBA can be surprisingly difficult in certain usage scenarios.

New VBA routines here will help resolve these issues in Excel, Word and Access (Windows PC versions only).

Functions are straightforward to use with coding style to support infrequent VBA users and developers.

Intended to help implement ad-hoc projects for serial data acquisition or transfer.

No plug-ins, DLLs, ActiveX, licences, payments or registrations are required.

More Information
VBA Issues

The in-built VBA functions for COM Port data can suffer from the following issues :-

  1. Setting port parameters with the VBA open command may not work in some Windows versions e.g.

    Open "COM1:9600,N,8,1" For Read Access As #1
    (command line workaround known, settings can revert after reboot)

  2. Attempting to read data when there is none waiting will cause VBA to hang with a ‘not responding’ message.

    Get #1, , Read_Data_Byte

    The new functions address both of these issues, and also where data transfers take longer than the 5-6 second VBA timeout.

Background

The legacy of serial comms means that many online solution searches are now time-expired with links to defunct web sites etc.

New functions here are therefore a fresh start for 2022 and are based largely on Microsoft’s Win32 API calls and documentation.

Developed on Windows 10 (64-Bit) with a local Microsoft Office 2019 Professional (32-Bit VBA7) installation.

Tested on Office 2016 Professional (64-bit VBA7) and Office 2019 Professional (32-Bit VBA7)

COM Ports

Multiple com ports are supported, including physical hardware ports and synthetic virtual software ports.

All read and write functions are synchronous, in part because not all serial port types support overlapped operation.

Performance on a modern PC is good, with software timing delays required to allow the relatively slow serial com ports to catch up.

Reading, Writing and Waiting are ‘timesliced’ to ensure that VBA remains responsive during any extended data transfers or waiting times.

Debugging

  • Debugging can be set on/off per port with results shown in the VBA immediate window.

  • Extensive debug functionality makes several modules quite verbose.

Other Versions

  • No-Debug (more compact)

  • Simplified (single com port)

  • Minimal (single com port, no settings)

  • VBA6 / 32-Bit (legacy Windows/Office)

Alternatives (Excel Only)

  • Microsoft Data Streamer for Excel

Optional steps for Excel only

  • Functions can be used directly in Worksheet cells as formulas where appropriate.
  • Remove comment mark before Option Private Module to prevent function names appearing in cell formula drop-down lists.
  • Remove comment mark before Application.Volatile where indicated to refresh results when functions are used in cells and the worksheet is recalculated (e.g. with F9 key).

Optional Ribbon Customisation

Office 2010 XML and SERIAL_PORT_RIBBON example files are available in the Ribbon folder.

Function List

COM Port Control

Read/Write/Check Data

Port Signalling Functions

Show Functions

Private functions are not intended to be called directly by users.


Download Article


Download Article

This tutorial explains how to establish a bidirectional real-time communication between Microsoft Excel and your RS232 COM Port devices like a Scale, Proximity reader, Barcode reader, temperature sensor, Caliper, Micrometer, Gage. This solution uses the software «Bill Redirect» with the «Excel Plugin» to easily connect your peripheral and receive and send the data. The communication between Microsoft Excel and your device is made via a direct DDE link. An Excel Macro can be called after each data received from your device to validate the data received. This solution explains how you can send a command via VBA to your device and totally control your device. No programming or additional hardware required!

  1. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 1

    1

  2. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 2

    2

    Download and install the Excel plugin. This is at http://www.billproduction.com plugin_DDE.zip. This plugin software is used to establish the communication with Microsoft Excel via DDE

  3. Advertisement

  1. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 3

    1

    Start the software: Bill Redirect. The default password to edit the configuration is: www.billproduction.com.

  2. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 4

    2

    Disable all section except the Serial Port and Plugin.

    • In section «Serial Port» set the switch to: On.
    • In section «Plugin» set the switch to: On.
  3. Advertisement

  1. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 5

    1

    Click on: Serial Port Configuration.

  2. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 6

    2

    Enter the Serial Port Number where your device is connected.

    • If the background color is green, it means that the serial port works and the port is open.

      • Select the Bauds, Parity and Databits configured in your device.
  3. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 7

    3

    Click on button: Save Configuration

    • If you use a RS-232 to USB converter to connect your device on the computer, it is important to use a converter with FTDI chipset for a good stability !
  4. Advertisement

  1. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 8

    1

    Start: Microsoft Excel
    . This solution is compatible with all Excel version and language !

  2. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 9

    2

    Create a in Excel a: new blank document.

  3. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 10

    3

    Make sure that the sheet name is: Sheet1.

  4. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 11

    4

    Save your Excel document to: C:BillProduction.CFGMyFile.xlsx.

  5. Advertisement

  1. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 12

    1

    Start the Excel Plugin
    .
    The default password to edit the configuration is: www.billproduction.com.

    • Important: The TCP Connection Status must indicate: Connect. If it is not to Connect then make sure that Bill Redirect Software is open.
    • In trial mode you must restart the software Bill Redirect after each 15 minutes to continue your test.»
  2. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 13

    2

    In the Excel Plugin click on the folder icon and select your Excel File created at step before.

    • If all is okay, the DDE Connection Status indicate: Connect.
    • At this point the communication is establish with your Device via Excel. The data received from your device is sent in Excel.
  3. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 14

    3

    Test your communication with Excel. Use the button: Test DDE. To see what your device sends or receive, use the section Debugger in Bill Redirect.

  4. Advertisement

  1. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 15

    1

    Consider example #1:’ Call an Excel Macro after each data received:

    • Simply enter your macro name in the field «Run macro:«
    • To test the call of your macro use the button at right.
  2. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 16

    2

    Consider example #2:’ Send a command to your device via VBA:

  3. Image titled Read RS232 to Excel Macro VBA Data Acquisition Step 17

    3

    To send data command from Excel to your device use this VBA Excel source code:

    • ChannelNumber = DDEInitiate(«BPEXCEL», «BPEXCEL»)
    • DDEExecute ChannelNumber, «{TX_SERIAL[Hello Word !{ASCII:13}]}»
    • DDETerminate ChannelNumber
    • Replace the command Hello Word !{ASCII:13} by the command you want.
    • Supplemental documentation.
    • Excel Plugin full manual with all explanation: http://www.billproduction.com
    • /Bill_DDE_over_Ethernet.pdf.
    • Bill Redirect Software full manual with all command:
      http://www.billproduction.com
    • /Bill_Redirect_Manual.pdf.
  4. Advertisement

Ask a Question

200 characters left

Include your email address to get a message when this question is answered.

Submit

Advertisement

Thanks for submitting a tip for review!

References

About This Article

Thanks to all authors for creating a page that has been read 31,754 times.

Is this article up to date?

Installing and Using functions as Excel Formulas

Worksheet Cell Formulas

Before starting

  • Check that the Com Port settings match on both the PC and attached device.
  • Connect a terminal emulator or similar device to the PC’s COM Port.
  • See screenshot in folder for Com Port settings check on host PC.

Installing VBA

  1. Download SERIAL_PORT_EXTRA_SIMPLE_VBAn.bas
  2. Open a new Excel document
  3. Enter the VBA Environment (Alt-F11)
  4. From VBA Environment, view the Project Explorer (Control-R)
  5. From Project Explorer, right-hand click and select Import File.
  6. Import the file SERIAL_PORT_EXTRA_SIMPLE_VBAn.bas
  7. Check that a new module SERIAL_PORT_VBA_SIMPLE is created and visible in the Modules folder.
  8. Check/Edit COM_PORT_NUMBER value at start of module SERIAL_PORT_VBA_SIMPLE
  9. In function READ_COM_PORT, remove the comment mark before Application.Volatile
  10. Close and return to Excel (Alt-Q)
  11. IMPORTANT — save document as type Macro-Enabled with a file name of your choice.

Excel Formula Testing
Start Com Port

  • In Cell A3, type the formula =start_com_port() and hit return
  • Check that TRUE is now shown in cell A3
  • TRUE confirms that the port has started.
  • FALSE means port not started, i.e. not found, already in use etc.

Send to Com Port

  • Enter some short text in Cell B5 — e.g. TEST123

  • In Cell A5, type the formula =send_com_port(B5) and hit return

  • Check that TRUE is now shown in cell A5

  • Check that TEST123 appears on your device

  • Change the text in Cell B5 — e.g. QWERTY and hit return

  • Check that TRUE is still shown in cell A5

  • Check that QWERTY appears on your device

  • This confirms that send_com_port is working.

Read from Com Port

  • In Cell B7, type the formula =read_com_port() and hit return
  • Enter some text on your device — e.g. HELLO
  • Change any other cell or press F9 key on your PC
  • Check that HELLO appears in Cell B7
  • Change any other cell or press F9 key on your PC for a second time
  • Check that Cell B7 returns to blank (no new data to read)
  • Enter some new text on your device — e.g. AGAIN
  • Change any other cell or press F9 key on your PC for a third time
  • Check that AGAIN appears in Cell B7
  • This confirms that read_com_port and Application.Volatile are working.

Stop Com Port

  • In Cell A9, type the formula =stop_com_port() and hit return
  • Check that TRUE is now shown in cell A9
  • TRUE confirms that the port has stopped.
  • Change any other cell or press F9 key on your PC
  • Check that FALSE appears in Cell A5 (send_com_port has failed as expected)
  • This confirms that stop_com_port is working.

Serial Devices

Passive Devices

These devices do not need a command to be sent before replying.
Reads should function from Excel with no further action required.
Send functions are usually not required, or required once only.

Active Devices

These devices do need a command to be sent before replying.

A read delay will normally be required to allow sufficient time for the :-

a) device to process the read command
b) serial data to be transmitted back

Remove the comment mark from Kernel_Sleep_Milliseconds in function read_com_port

Note that Functions may still be used in VBA routines as required.

The purpose of this article is to demonstrate how you can perform serial port communication in the VBA (Visual Basic Applications – script editor included in any typical Microsoft Excel distribution) but without using the MSComm control or any other third party add-on or ActiveX.

Serial Port Communication in Excel (VBA)

1. Introduction

The great advantage of this method, which uses API functions to call the serial port directly, is that you do not really need to install anything on your PC, apart from the Excel itself (which, let’s admit it, already exists on most PCs). Other methods always require the installation of an ActiveX, or at least the registration of an “.ocx” file, like for instance the MSComm control.   In order to get access to the VBA script editor, you first need to make the corresponding buttons available on the Excel toolbars (they are not there by default). These buttons will enable you to launch the editor in which you will be able to write the Visual Basic Code and will also allow you to directly create windows-style controls like textboxes, push buttons, labels, radio buttons, checkboxes etc. In order to make these available on the main toolbar, you need to activate the Control Toolbox button, from the small toolbar activated at the previous step. As a goal for this article, I would intend to demonstrate how you open the serial port, how you write a few bytes, how you read some bytes, and then how you close the serial port. In order to do all these, you will need to create four windows-style buttons, each of them performing one of the tasks that I have enumerated before. Click on the Command Button control from the recently activated toolbar, and then draw 4 similar buttons anywhere on the surface of your excel worksheet (figure 1).

Figure 1: Command button for a version of excel

Once you did that, click on the Visual Basic Editor button from the toolbar that you activated in the very first place. This will open the environment in which you can write the code for the Visual Basic scripts that will perform the operations you wanted. Once there, make sure that you double click on the Sheet1(Sheet1) entry. The effect of this action will be the opening in the main window of this environment of the actual sheet in which you will write the VB code. Once you reached this stage, copy paste the code below in there. This Visual Basic Code “defines” the constants, structures and API functions that you will use later to perform the intended operations. The comments in the code are self

Source :  Serial Port Communication in Excel (VBA)

  • Remove From My Forums
  • Question

  • Question, I have a need to send a text string «Measure» to a device on com1 baud(4800,7,E,2) and capture the response into cell A1. Then pause for 3 seconds, and repeat for a total of 10 times. Each time populating the next empty row. A1,A2,A3,A4?

    Is this possible? Thank you


    heads up

Answers

  • Hi texastwostep,

    You need to create a SerialPort object in the VBA code, then use the write method to write the string into the COM port. Try it:

    How to: Send Strings to Serial Ports in Visual Basic

    Step by step example found in this article, this may help you a lot:

    Serial Port Communication in Excel (VBA)


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click
    HERE to participate the survey.

    • Marked as answer by

      Wednesday, April 16, 2014 12:16 PM

Понравилась статья? Поделить с друзьями:
  • Excel vba com port
  • Excel vba columns несколько столбцов
  • Excel vba columns value
  • Excel vba cells font color
  • Excel vba columns numbers