Forum Discussion

serialcomms's avatar
serialcomms
Copper Contributor
Aug 17, 2022

Serial COM Ports in Excel VBA

Hello all,

 

I've uploaded a new set of VBA functions for anyone still interested in using Serial COM Ports with Excel. 

 

https://github.com/Serialcomms/Serial-Ports-in-VBA-new-for-2022

 

Let us know if they work for you thanks. 

 

J.

19 Replies

  • Matthimaster's avatar
    Matthimaster
    Occasional Reader

    Hi,

    Thanks for creating this library! I’m running into a problem when reconnecting an Arduino device.

    I can successfully open the COM port from VBA, send a command to the Arduino, and receive the expected acknowledge. So the initial communication works perfectly.

    However, when I unplug the Arduino’s USB cable and plug it back in, the behavior changes. VBA can still send data to the Arduino, but the Arduino can no longer send anything back to VBA. My assumption is that the STOP_COM_PORT function does not fully close and release the COM port handle, so after reconnecting the USB device, VBA is still holding on to an old/invalid port instance.

    Could you advise how to properly and fully close the COM port so that communication works again after unplugging and reconnecting the Arduino?

    Thanks in advance!

    • Matthimaster's avatar
      Matthimaster
      Occasional Reader

      Hi, 

      I fixxed the issue!

      It had nothing to do with the COM port handle, but with my arduino that had some issues with the restart. Now the vba code sends a DTR and RTS code through the SIGNAL_COM_PORT function so the arduino resets and starts working properly again.

  • 0000_AR's avatar
    0000_AR
    Copper Contributor
    Is there option for VBA function to connect through TCP/IP with port number serial to excel?
    My microcontroller connect using LAN cable which provide serial connection through TCP/IP.
  • PhilRob81's avatar
    PhilRob81
    Copper Contributor

    serialcomms hello, I am using excel 2106 and I used your module which is much appreciated! My problem is I can connect to the port with the correct baud rate and all but when I send a command then request to receive what should be displayed I just get a mirror of what I sent.

    example sent: (EX)

    received :  (EX)

    This should return a software and firmware ascii text. I tried using \r\c or one of each at the end of the sent string and I just revive that added text back.

    please help and thanks!

     

  • LeoKing's avatar
    LeoKing
    Copper Contributor

    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!

    • serialcomms's avatar
      serialcomms
      Copper Contributor

      LeoKing 

       

      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_M395's avatar
        Andrew_M395
        Copper Contributor
        It 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
  • TangJop's avatar
    TangJop
    Copper Contributor

    serialcomms Hello,

    One additional question: I need to identify a device connected to a given port. How to get some device info (like its name, type or anything significant) ?

     

    Thanks in advance for your feedback.

    • LuisS90's avatar
      LuisS90
      Copper Contributor

      TangJop 

      Maybe you should try this:

       

      '--------------------------------------------------------------------------------------------------
      'Loops through all the USB controllers and devices (sticks, hubs, etc.) and retrieves information.
      'The code uses a WMI script in order to access the Win32_USBControllerDevice class.

      'Written By: Christos Samaras
      'Date: 13/01/2014
      'E-mail: email address removed for privacy reasons
      'Site: https://www.myengineeringworld.net
      'https://myengineeringworld.net/2014/01/retrieve-usb-device-information-vba-wmi.html
      '--------------------------------------------------------------------------------------------------

      • TangJop320's avatar
        TangJop320
        Copper Contributor
        Hello Luis, sorry for this VERY long delay. Again thx for your support. This is very useful and efficient. Bthw I tried the code and it works (I only needed to replace your 'shUSB' with my 'Sheets("MySheetName")'. But it take a too long time before I get my USB ports. I get a full set of info, but I need only 10% of it. How to restrict the result to the lines contaning '(COMx' only ? Probably by modifing the SQL request probably, but I need inputs about it. Thx in advance for your help.
  • TangJop's avatar
    TangJop
    Copper Contributor

    serialcomms 

    No options : THANK YOU VERY MUCH !!!!!!!! I spent hours for making Open/Input/Get ... working with nothing else than a void answer. Now it works extremely well. No doubts : it's a high contribution to the  professional Excel VB developers community. Warm congrats again

  • peiyezhu's avatar
    peiyezhu
    Bronze Contributor
    great functions!
    all by win32 api really complex.
    I used to work with Micosoft comm Control6.0 many years ago.
  • GaberM's avatar
    GaberM
    Copper Contributor
    Thank you for these VBA functions. I have been looking for similar solution for a long time. For the time being it is working without any problems.