Pass reference of COM port to function call

vbauser00

New Member
Joined
Sep 16, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
How would I pass a reference to an open COM port to a function call?

I'm using this syntax:

Open "COM1:115200,N,8,1" For Binary Access Write As #1
at the beginning of my subroutine.

With the "#1" identifier still open I would like to pass to a function.

I prefer not to close the COM port with "Close #1" to open back up inside a function call as a passing parameter.

Thank you.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Perhaps something like ...

VBA Code:
Sub OpenCom1()

    Dim COM1Handle As Long

    COM1Handle = FreeFile
    Open "COM1:115200,N,8,1" For Binary Access Write As #COM1Handle

        WriteCom1 COM1Handle                 ' <<<<< invoke another procedure and passing an argument
    
    Close #COM1Handle
End Sub


Sub WriteCom1(ByVal argComHandle As Long)    '  <<<< procedure to be invoked

    Dim buffer As Byte

    Put #argComHandle, , buffer

End Sub
 
Upvote 0
Thank you for your help.

Initially the commands I was writing to the COM port wasn't working inside a function call when I opened/closed the port outside of the function call. That is the reason I submitted a question to this forum.
Yesterday, it appeared to be working. Perhaps something else was in my code was incorrect initially.

If I see any issues then I'll give your suggestion a try. Again - thank you for your reply.
 
Upvote 0
You are welcome & thanks for the follow-up.
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,973
Members
449,200
Latest member
Jamil ahmed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top