Autoit3 Library – CommAPI
Informatie (ENG)
CommAPI translates the communications functions of Windows API to AutoIt functions.
Features
- No need to install DLL’s
- Using Windows API calls (kernel32.dll)
- Possibility of serial communication (serial port, COM port, RS-232)
- Possibility of parallel communication (parallel port, LPT port)
- No use of global variables
- Uniform namespace _CommAPI_XXX
- Modular organization into files
- A lot of additional utility and helper functions
History
- 2008-09-12 Creation of cfx.au3 (V1.0)
- 2011-02-18 Conversation of cfx.au3 into cfxUDF.au3 (V2.0)
- 2011-02-28 Update V2.0 to V2.1
- 2011-04-27 Update V2.1 to V2.2 (german forum)
- 2013-07-10 Modify V2.1 to V2.1mod
- 2013-10-24 Creation of CommAPI.au3
Changelog
- 2014-01-23
- Small update of method _CommAPI_CreateModeString
- 2014-02-03
- Change timeout handling (Attention: not backward compatible)
- Small update of method _CommAPI_PurgeComm
- 2014-03-07
- Rename CommAPIStructures.au3 to CommAPIConstants.au3
- Replace hex values with Constants
- 2014-03-27
- Update of call _WinAPI_CreateFile
- Updated @error handling
- New parameter MaxLen for function _CommAPI_ReceiveData
- New function _CommAPI_ReceiveLine
- Update existing examples and add new examples
- 2014-03-31
- Add constants
- Fix _CommAPI_WaitCommEvent
- 2014-04-03
- Fix reading error Chr(128) to Chr(255)
- Fix error in _CommAPI_TransmitData
- 2014-04-04
- Replace ReceiveData & ReceiveLine with ReceiveBinary & ReceiveString
- 2014-04-07
- replace _CommAPI_TransmitData with _CommAPI_TransmitBinary & _CommAPI_TransmitString
- Create CommObsolete.au3 for backward compatibility
- 2014-04-08
- Fix error in _CommAPI_ReceiveBinary
Voorbeeld scripts
Introduction
This site shows you some examples for CommAPI. You will also need the following snippet for all examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include "CommInterface.au3" #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7 Main() Func Main() Local $sResult = Example() Switch @error Case 0 MsgBox(64, "Result", $sResult) Case -1 MsgBox(32, "Error", _WinAPI_GetLastErrorMessage()) Case -2 MsgBox(32, "Timeout", $sResult) Case Else MsgBox(32, "Error", "Error " & @error & " in line " & @extended) EndSwitch EndFunc |
First example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Func Example() Local Const $iPort = 1 Local Const $iBaud = 9600 Local Const $iParity = 0 Local Const $iByteSize = 8 Local Const $iStopBits = 1 Local Const $sCommand = "Command" & @CRLF Local $hFile = _CommAPI_OpenCOMPort($iPort, $iBaud, $iParity, $iByteSize, $iStopBits) If @error Then Return SetError(@error, @ScriptLineNumber) _CommAPI_ClearCommError($hFile) If @error Then Return SetError(@error, @ScriptLineNumber) _CommAPI_PurgeComm($hFile) If @error Then Return SetError(@error, @ScriptLineNumber) _CommAPI_TransmitString($hFile, $sCommand) If @error Then Return SetError(@error, @ScriptLineNumber) Local $sResult = _CommAPI_ReceiveString($hFile, 5000) If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) _CommAPI_ClosePort($hFile) If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) Return $sResult EndFunc |
INI file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
Func Example() Local Const $sFileINI = @ScriptDir & "\Example.ini" Local $sMode = _CommAPI_CreateModeString( _ IniRead($sFileINI, "COM", "SerialPort", 1), _ IniRead($sFileINI, "COM", "BaudRate", 9600), _ IniRead($sFileINI, "COM", "Parity", 0), _ IniRead($sFileINI, "COM", "ByteSize", 8), _ IniRead($sFileINI, "COM", "StopBits", 1), _ IniRead($sFileINI, "COM", "XON", 0), _ IniRead($sFileINI, "COM", "DSR", 0), _ IniRead($sFileINI, "COM", "CTS", 1), _ IniRead($sFileINI, "COM", "DTR", 1), _ IniRead($sFileINI, "COM", "RTS", 1), _ IniRead($sFileINI, "COM", "IDSR", 0)) If @error Then Return SetError(@error, @ScriptLineNumber) Local $hFile = _CommAPI_OpenPort($sMode) If @error Then Return SetError(@error, @ScriptLineNumber) _CommAPI_ClearCommError($hFile) If @error Then Return SetError(@error, @ScriptLineNumber) _CommAPI_PurgeComm($hFile) If @error Then Return SetError(@error, @ScriptLineNumber) _CommAPI_TransmitString($hFile, IniRead($sFileINI, "COM", "Command", "Command" & @CRLF)) If @error Then Return SetError(@error, @ScriptLineNumber) Local $sResult = _CommAPI_ReceiveString($hFile, IniRead($sFileINI, "Timeout", "IDSR", 5000)) If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) _CommAPI_ClosePort($hFile) If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) Return $sResult EndFunc |
Command Line
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Func Example() If $CmdLine[0] <> 2 Then MsgBox(32, "Syntax", @ScriptName & ' "ModeString" "Command"') Exit EndIf Local $hFile = _CommAPI_OpenPort($CmdLine[1]) If @error Then Return SetError(@error, @ScriptLineNumber) _CommAPI_ClearCommError($hFile) If @error Then Return SetError(@error, @ScriptLineNumber) _CommAPI_PurgeComm($hFile) If @error Then Return SetError(@error, @ScriptLineNumber) _CommAPI_TransmitString($hFile, $CmdLine[2]) If @error Then Return SetError(@error, @ScriptLineNumber) Local $sResult = _CommAPI_ReceiveString($hFile, 5000) If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) _CommAPI_ClosePort($hFile) If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) Return $sResult EndFunc |
1 2 3 4 5 |
@echo off cls chcp 1252>nul cd /d "%~dp0" start Example.exe "COM1: BAUD=9600 PARITY=N DATA=8 STOP=1 XON=OFF ODSR=OFF OCTS=ON DTR=ON RTS=ON IDSR=OFF" "Command" |
Separator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Func Example() Local $hFile = _CommAPI_OpenCOMPort(1, 9600, 0, 8, 1) If @error Then Return SetError(@error, @ScriptLineNumber) Local $sResult = "" While True $sResult = _CommAPI_ReceiveString($hFile, 5000, 0, "<CR>") If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) MsgBox(64, "Line", $sResult) WEnd _CommAPI_ClosePort($hFile) If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) Return $sResult EndFunc |
Slow Devices
For some slow devices the default timeout is too small of one millisecond.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Func Example() Local $hFile = _CommAPI_OpenCOMPort(1, 9600, 0, 8, 1) If @error Then Return SetError(@error, @ScriptLineNumber) _CommAPI_ChangeCommTimeoutsElement($hFile, "ReadTotalTimeoutMultiplier", 100) If @error Then Return SetError(@error, @ScriptLineNumber) Local $sResult = _CommAPI_ReceiveString($hFile, 5000) If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) _CommAPI_ClosePort($hFile) If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) Return $sResult EndFunc |
[#/autoit3/libraries/commapi” ]