Jump to content

Close COM port


Champak
 Share

Recommended Posts

  • 6 months later...

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

: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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...