Jump to content
Sign in to follow this  
Champak

Close COM port

Recommended Posts

Champak

Is there a way to close a specific COM port that is in use...not in use by my script? I've looked at the commg udf, but see nothing of use for me.

Share this post


Link to post
Share on other sites
Blue_Drache

As martin is actively developing this, I think I'd have to defer to him and possibly ask in his if this is a feature he could add.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites
atzoref

I have the same question,

How can I Open/Close a specific ComPort?

Share this post


Link to post
Share on other sites
FireFox

Hi,

Code from MKISH :

#include <WinAPI.au3>
$RESULT = _WinAPI_CreateFile(".COM3", 2, 2)
If $RESULT = 0 then
msgbox(16, "", "Port not available, please plug in your device")
EndIf

This is for opening the COM port 3, maybe to close a port you have to open and close it.

Hope that helps.

Br, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
stormbreaker

Some extension to above:

#include <WinAPI.au3>
$RESULT = _WinAPI_CreateFile(".COM3", 2, 2)
msgbox(64, "", "Port opened.")
_WinAPI_CloseHandle($RESULT)
msgbox(64, "", "Handle closed.")

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Share this post


Link to post
Share on other sites
AlainB

Hi,

It is not working for me, I mean for the closing of the port. For the opening, It is working well.

What could be the problem?

Alain

#include <WinAPI.au3>

Global $Port = "COM1"

_OpenAndClosePort()

Func _OpenAndClosePort()

$Open = _WinAPI_CreateFile($Port, 2, 2)

If $Open = False then
msgbox(0, "", "The port is not open" ,3)
Exit
EndIf

msgbox(0, "", "The port is open",2)

$Closed =_WinAPI_CloseHandle($Port)

If $Closed = False then
msgbox(0, "", "The port is not closed",3)
Exit
EndIf

msgbox(0, "", "The port is closed",3)

EndFunc
Edited by AlainB

Share this post


Link to post
Share on other sites
stormbreaker

:tv_horror: _WinAPI_CloseFile() closes a handle created with _WinAPI_CreateFile() (or so). So, you need to do something like:

_WinAPI_CloseFile($Open)

AND NOT: _WinAPI_CloseFile($Port)

:graduated: Hope, all's clear now...


----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Share this post


Link to post
Share on other sites
AlainB

Thank you MKISH, very informative post, as usual. :idiot:

It is working now. Here is the edited code:

#include <WinAPI.au3>

Global $Port = "COM1"

_OpenAndClosePort()

Func _OpenAndClosePort()
Local $Port1 = _WinAPI_CreateFile($Port, 2, 2)

If $Port1 = False then
msgbox(0, "", "The port is not open" ,3)
exit
EndIf

msgbox(0, "", "The port is open",2)

Local $Closed =_WinAPI_CloseHandle($Port1)

if $Closed = False then
msgbox(0, "", "The port is not closed",3)
exit
EndIf

msgbox(0, "", "The port is closed",3)
EndFunc

(On edit: It is working for me with _WinAPI_CloseHandle but not with _WinAPI_CloseFile. Is this a typo? It gives the error Unknown function name.)

Edited by AlainB

Share this post


Link to post
Share on other sites
stormbreaker

The error speaks for itself: There is no such function defined in WinAPI UDF (or in Windows probably)


----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Share this post


Link to post
Share on other sites
AlainB

Yes, it is true... when you have acquired a sufficient knowledge on how Autoit is working. The concept of including UDF in a script and what it is doing is not necessary very obvious for a newbee. If one know, he could go under the "Include" sub directory of autoit and see that there is a file called WINAPI.AU3 among many other ones. Opening this file, he will see that the content is a set of functions that may be used when the file is included in a script. When you know all that then yes...the error speaks for itself.

Just taking this occasion to try to explain a bit what are included files for others newbees like me. All this is probably in a help file somewhere anyway.

:ILA2:

Edited by AlainB

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×