Jump to content

CommAPI - Serial and parallel communication with Windows API


therealhanuta
 Share

Recommended Posts

  • 2 months later...
  • 10 months later...

So far this works with the latest Windows 10 (1909)

Does anyone know if this UDF supports reading a Comport that is already open by another program? I have seen references to it on the internet when you can eavesdrop on a serial port that is already open. Is this doable with this UDF?  

Anther thing I am struggling with are the return codes. I call _CommAPI_OpenCOMPort() on a non-existent port and it returns a -1 which, according to the documentation, then requires a call to _WinAPI_GetLastError() which in turn return 2 which is invalid return type. In looking through the return codes I don't see any that would signify things like "port does not exist", "port already opened" or things like that. Aren't those errors available?

Edited by Trax
Link to comment
Share on other sites

  • 3 months later...

Hi, I run this script:

#include <CommInterface.au3>
#include <MsgBoxConstants.au3>

Local Const $iPort = 5
Local Const $iBaud = 115200
Local Const $iParity = 0
Local Const $iByteSize = 8
Local Const $iStopBits = 1

Local $hFile =_CommAPI_OpenCOMPort($iPort, $iBaud, $iParity, $iByteSize, $iStopBits)
_CommAPI_ClearCommError($hFile)
_CommAPI_PurgeComm($hFile)

_CommAPI_TransmitString($hFile, "*IDN?") ;Identify your-self

Local $sResult =_CommAPI_ReceiveString($hFile, 5000);Recieve string

_CommAPI_ClosePort($hFile)

MsgBox($MB_SYSTEMMODAL, "Title", $sResult, 5)

But bump into this error:

C:\Program Files (x86)\AutoIt3\Include\CommInterface.au3 (1) : ==> Unable to parse line.: 
<!DOCTYPE html> 
<^ ERROR
>Exit code: 1    Time: 0.3675

I downloaded all 6 files from https://www.autoitscript.com/wiki/CommAPI and put them in the "Include" folder under AutoIt directory.

How can I workaround on this error? My system is 64 bit though.

Link to comment
Share on other sites

  • 3 months later...
On 6/12/2020 at 4:23 AM, guner7 said:

Hi, I run this script:

#include <CommInterface.au3>
#include <MsgBoxConstants.au3>

Local Const $iPort = 5
Local Const $iBaud = 115200
Local Const $iParity = 0
Local Const $iByteSize = 8
Local Const $iStopBits = 1

Local $hFile =_CommAPI_OpenCOMPort($iPort, $iBaud, $iParity, $iByteSize, $iStopBits)
_CommAPI_ClearCommError($hFile)
_CommAPI_PurgeComm($hFile)

_CommAPI_TransmitString($hFile, "*IDN?") ;Identify your-self

Local $sResult =_CommAPI_ReceiveString($hFile, 5000);Recieve string

_CommAPI_ClosePort($hFile)

MsgBox($MB_SYSTEMMODAL, "Title", $sResult, 5)

But bump into this error:

C:\Program Files (x86)\AutoIt3\Include\CommInterface.au3 (1) : ==> Unable to parse line.: 
<!DOCTYPE html> 
<^ ERROR
>Exit code: 1    Time: 0.3675

I downloaded all 6 files from https://www.autoitscript.com/wiki/CommAPI and put them in the "Include" folder under AutoIt directory.

How can I workaround on this error? My system is 64 bit though.

You have to open the link in the browser, copy the content and save in a file in your computer. That happed probably because you clicked on "Save link as", so the browser save the html page, not the file.

Link to comment
Share on other sites

_CommAPI_GetCOMPorts()

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • 8 months later...

According to what I read in the "Features" session, this UDF is capable of LPT ports management.

"Possibility of parallel communication (parallel port, LPT port)"

Can someone give an example...I cannot figure it out how can i connect on a LPT port :(

Thank you so much!

Link to comment
Share on other sites

Hello @manutz88 It seems to be is not implemented. You would need to modify _CommAPI_CreateModeString function to support LPT string.

 

Saludos

Link to comment
Share on other sites

  • 1 year later...

Hello. Does anyone know why, in the _CommAPI_GetCOMPorts function, in the following line of code:

Local $oItems = $oWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(COM%)'", "WQL", 48)

the number 48 = 16 (Return Immediately) + 32 (ForwardOnly) is passed as a parameter? If I pass 0, the call doesn't return until the query completes (I think is more helpfull), and I can also get the number of items returned without ForwardOnly ($oItems.Count exists and doesn't return an error like passing the Forward Only parameter does).

I attached my modified version (I change function name so I can use both functions) for anyone interested. The differences:

  • autostart of WMI service if not running
  • detection of LPT and COM ports
  • can filter out LPT ports
  • can filter out ports without port number
  • can return array of ports detected as port numbers, port names (COMx/LPTx), or bi-dimensional array with port number, port type (COM or LPT), hardware ID, port description

 

_CommAPI_GetPorts.au3

Link to comment
Share on other sites

  • 2 months later...

Hello,
Is there a way to work with events instead of pulling the serial port? So every time one byte arrives a function can receive that byte, this way I can implement a circular buffer for a more advanced communication protocol.
Thanks

Link to comment
Share on other sites

55 minutes ago, Chuckero said:

Is there a way to work with events

look at one of the first posts ( https://www.autoitscript.com/forum/topic/155674-commapi-serial-and-parallel-communication-with-windows-api/?do=findComment&comment=1126366 ) and you'll see that your question makes little sense. I don't use this but looking at the code, all you have to do is to code your own events on reception. You'd have to pull from _CommAPI_ReceiveData() every so often ( with AdLib or timer or your imagination ) and trigger your event for the rest of the script. The data will be there but you'll have to read to be aware of the data been there. Should not be a big deal.

 

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 2 weeks later...
On 6/16/2023 at 10:43 AM, argumentum said:

look at one of the first posts ( https://www.autoitscript.com/forum/topic/155674-commapi-serial-and-parallel-communication-with-windows-api/?do=findComment&comment=1126366 ) and you'll see that your question makes little sense. I don't use this but looking at the code, all you have to do is to code your own events on reception. You'd have to pull from _CommAPI_ReceiveData() every so often ( with AdLib or timer or your imagination ) and trigger your event for the rest of the script. The data will be there but you'll have to read to be aware of the data been there. Should not be a big deal.

 

I understood. So, there is no way to work with events, I have to work with pulling the serial port at a high frequency, or at least, high enough to not let the input buffer fill up.

 

Link to comment
Share on other sites

2 hours ago, Chuckero said:

So, there is no way to work with events, I have to work with pulling the serial port at a high frequency, or at least, high enough to not let the input buffer fill up.

You can always fork ( look in my signature for examples ). That way, one script as the serial com handler and the other, the GUI that receives the data from the other via IPC. Therefore, even thought AutoIt is slow to handle so may things in a single thread, with this approach you'd have as many threads/cores working for you as you feel you need to have, to make it in time to handle anything. Or most anything, again, gotta be creative :)   

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • 4 months later...

Have you tried Service UDF?

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

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

×
×
  • Create New...