Jump to content

CommAPI - Serial and parallel communication with Windows API

Recommended Posts

Hi, yes you can!

File: CommUtilities.au3

Func: _CommAPI_CreateModeString

String: Case 11 To 256000

Change 256000 value to 3000000

Its works! But no more 3000000 baud.

Share this post

Link to post
Share on other sites

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

Share this post

Link to post
Share on other sites

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_TransmitString($hFile, "*IDN?") ;Identify your-self

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


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> 
>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.

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By cheeroke
      Hi all,
      I got this code and would like to be able to change Baud Rate and instead of sending character by character i would like to be able (if possible) to send whole string. But i don't know how to change it.
      I am taking input from file and processing whole line (this is done in FilesHandling.au3).
      To execute this i am just calling SendData("FileName", int) in "main" script.
      Any help very appreciated.
      #include <WinAPI.au3> #include <Array.au3> #include "FilesHandling.au3" ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM19", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h[0] Func SendData($FileName, $LineNumber) ;string to be send $c = readFile($FileName, $LineNumber) $cLenght = StringLen($c) $aArray = StringSplit($c, "") ;_ArrayDisplay($aArray, "", Default, 64) For $i = 1 To $cLenght writeChar($handle, $aArray[$i], $cLenght) Next ;move to next line writeChar($handle, @CR,1) EndFunc ;write a single char func writeChar($handle,$c,) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,$lpNumberOfBytesWritten) if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc  
    • By MazeM
      here's another UDF for the serial port. It is very similar to CommAPI using kernel32.dll, but all code is packed into a single file without any dependencies, not even using WinAPI.au3. It differs from existing UDF that it doesn't allow a timeout when reading, instead it always returns immediately, either with the requested amount ob bytes read or with a failure status. And of course there is a function provided to query the amount of available bytes in the receive buffer. The reason behind this design decision: You can do 1000 other things in the main loop while checking from time to time if enough data bytes arrived. There's no point to block the program waiting for the serial port.
      It is currently a work-in-progress, as I didn't test all functions yet. The code was developed and tested on Windows 7 64 bit.  The ComUDF-Tests.au3 shows some tests and basic usage of the UDF. Maybe there's no reason to use this UDF, given the existence of the others UDFs, but I did it to get to know DllCall better - I use structs no only to pass but also to get data back (I don't use the array returned by DllCall to read that data, unless required). You're welcome to test it on older and newer Windows versions.
      Here's a list of the implemented functions:
      ; _ComListPorts ; _ComOpenPort ; _ComSetTimeouts ; _ComClosePort ; ; _ComSetBreak ; _ComClearBreak ; _ComGetInputcount ; _ComGetOutputcount ; _ComClearOutputBuffer ; _ComClearInputBuffer ; ; _ComSendByte ; _ComReadByte ; _ComSendBinary ; _ComReadBinary ; ; _ComSendChar ; _ComReadChar ; _ComSendCharArray ; _ComReadCharArray ; _ComSendString ; _ComReadString ; ; __ComClearCommError ; __PurgeComm Maze
    • By OldNoob
      I'm attempting to call the winapi function EnumSystemFirmwareTables using DllCall "Kernel32.dll" without success. I am a total noob when it comes to this and could use some direction. Based on the documentation "Dealing with Dlls in AutoIt" by Andreas Karlsson, I have tried using the following code to obtain the buffersize of the Firmware Table Buffer.
      Thanks in advance for any help
      #include <WinAPI.au3> MsgBox(0, "ESFT BufferSize", "BufferSize = " & _EnumSystemFirmwareTables()) Func _EnumSystemFirmwareTables() $aRet = DllCall ("Kernel32.dll", "UINT", "EnumSystemFirmwareTables", "DWORD", "ACPI", "PVOID" ,Null , "DWORD" ,Null) if @error Then MsgBox (0,"Error","An error ocurred with the DLLCALL, error returned = " & @error &@CRLF & "GetLastError = " & _WinAPI_GetLastError ( ),0) Exit else Return $aRet endif EndFunc  
  • Create New...