CommAPI Examples: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
(clean using of _CommAPI_PurgeComm)
(update existing examples and add new examples)
Line 1: Line 1:
[[Category:Samples]]This site shows you some examples for [[CommAPI]].
[[Category:Samples]]
== Introduction ==
This site shows you some examples for [[CommAPI]].
You will also need the following snippet for all examples:


== First example ==
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include "CommInterface.au3"
#include "CommInterface.au3"
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7


Main()
Main()


Func Main()
Func Main()
Local $iErrorLine = Example()
Local $sResult = Example()
If @extended Then
Switch @error
MsgBox(32, "Error", _WinAPI_GetLastErrorMessage())
Case 0
ElseIf @error Then
MsgBox(64, "Result", $sResult)
MsgBox(32, "Error", "Error in line " & $iErrorLine)
Case -1
EndIf
MsgBox(32, "Error", _WinAPI_GetLastErrorMessage())
Case -2
MsgBox(32, "Timeout", $sResult)
Case Else
MsgBox(32, "Error", "Error " & @error & " in line " & @extended)
EndSwitch
EndFunc
EndFunc
</syntaxhighlight>


== First example ==
<syntaxhighlight lang="autoit">
Func Example()
Func Example()
Local Const $iPort = 1
Local Const $iPort = 1
Line 25: Line 36:


Local $hFile = _CommAPI_OpenCOMPort($iPort, $iBaud, $iParity, $iByteSize, $iStopBits)
Local $hFile = _CommAPI_OpenCOMPort($iPort, $iBaud, $iParity, $iByteSize, $iStopBits)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber)


_CommAPI_ClearCommError($hFile)
_CommAPI_ClearCommError($hFile)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber)


_CommAPI_PurgeComm($hFile)
_CommAPI_PurgeComm($hFile)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber)


_CommAPI_TransmitData($hFile, $sCommand)
_CommAPI_TransmitData($hFile, $sCommand)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber)


Local $sResult = _CommAPI_ReceiveData($hFile, 5000)
Local $sResult = _CommAPI_ReceiveData($hFile, 5000)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)


_CommAPI_ClosePort($hFile)
_CommAPI_ClosePort($hFile)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)


MsgBox(64, "Result", $sResult)
Return $sResult
EndFunc
EndFunc
</syntaxhighlight>
</syntaxhighlight>
Line 48: Line 59:
== INI file==
== INI file==
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include "CommInterface.au3"
Main()
Func Main()
Local $iErrorLine = Example()
If @extended Then
MsgBox(32, "Error", _WinAPI_GetLastErrorMessage())
ElseIf @error Then
MsgBox(32, "Error", "Error in line " & $iErrorLine)
EndIf
EndFunc
Func Example()
Func Example()
Local Const $sFileINI = @ScriptDir & "\Example.ini"
Local Const $sFileINI = @ScriptDir & "\Example.ini"
Line 76: Line 74:
IniRead($sFileINI, "COM", "RTS", 1), _
IniRead($sFileINI, "COM", "RTS", 1), _
IniRead($sFileINI, "COM", "IDSR", 0))
IniRead($sFileINI, "COM", "IDSR", 0))
If @error Then Return SetError(@error, @extended)
If @error Then Return SetError(@error, @ScriptLineNumber)


Local $hFile = _CommAPI_OpenPort($sMode)
Local $hFile = _CommAPI_OpenPort($sMode)
If @error Then Return SetError(@error, @extended)
If @error Then Return SetError(@error, @ScriptLineNumber)


_CommAPI_ClearCommError($hFile)
_CommAPI_ClearCommError($hFile)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber)


_CommAPI_PurgeComm($hFile)
_CommAPI_PurgeComm($hFile)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber)


_CommAPI_TransmitData($hFile, IniRead($sFileINI, "COM", "Command", "Command" & @CRLF))
_CommAPI_TransmitData($hFile, IniRead($sFileINI, "COM", "Command", "Command" & @CRLF))
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber)


Local $sResult = _CommAPI_ReceiveData($hFile, IniRead($sFileINI, "Timeout", "IDSR", 5000))
Local $sResult = _CommAPI_ReceiveData($hFile, IniRead($sFileINI, "Timeout", "IDSR", 5000))
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)


_CommAPI_ClosePort($hFile)
_CommAPI_ClosePort($hFile)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)


MsgBox(64, "Result", $sResult)
Return $sResult
EndFunc
EndFunc
</syntaxhighlight>
</syntaxhighlight>
Line 102: Line 100:
== Command Line ==
== Command Line ==
<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
#include "CommInterface.au3"
Main()
Func Main()
Local $iErrorLine = Example()
If @extended Then
MsgBox(32, "Error", _WinAPI_GetLastErrorMessage())
ElseIf @error Then
MsgBox(32, "Error", "Error in line " & $iErrorLine)
EndIf
EndFunc
Func Example()
Func Example()
If $CmdLine[0] <> 2 Then
If $CmdLine[0] <> 2 Then
MsgBox(32, "Syntax", @ScriptName & ' "ModeString" "Command"')
MsgBox(32, "Syntax", @ScriptName & ' "ModeString" "Command"')
Return
Exit
EndIf
EndIf


Local $hFile = _CommAPI_OpenPort($CmdLine[1])
Local $hFile = _CommAPI_OpenPort($CmdLine[1])
If @error Then Return SetError(@error, @extended)
If @error Then Return SetError(@error, @ScriptLineNumber)


_CommAPI_ClearCommError($hFile)
_CommAPI_ClearCommError($hFile)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber)


_CommAPI_PurgeComm($hFile)
_CommAPI_PurgeComm($hFile)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber)


_CommAPI_TransmitData($hFile, $CmdLine[2])
_CommAPI_TransmitData($hFile, $CmdLine[2])
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber)


Local $sResult = _CommAPI_ReceiveData($hFile, 5000)
Local $sResult = _CommAPI_ReceiveData($hFile, 5000)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)


_CommAPI_ClosePort($hFile)
_CommAPI_ClosePort($hFile)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)


MsgBox(64, "Result", $sResult)
Return $sResult
EndFunc
EndFunc
</syntaxhighlight>
</syntaxhighlight>
Line 148: Line 133:
chcp 1252>nul
chcp 1252>nul
cd /d "%~dp0"
cd /d "%~dp0"
start Example.exe "COM7: BAUD=9600 PARITY=N DATA=8 STOP=1 XON=OFF ODSR=OFF OCTS=ON DTR=ON RTS=ON IDSR=OFF" "Command"
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"
</syntaxhighlight>
 
== Separator ==
<syntaxhighlight lang="autoit">
Func Example()
Local $hFile = _CommAPI_OpenCOMPort(1, 9600, 0, 8, 1)
If @error Then Return SetError(@error, @ScriptLineNumber)
 
Local $sResult = _CommAPI_ReceiveLine($hFile, "<CR>", 5000)
If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)
 
_CommAPI_ClosePort($hFile)
If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)
 
Return $sResult
EndFunc
</syntaxhighlight>
 
== Slow Devices ==
For some slow devices the default timeout is too small of one millisecond.
<syntaxhighlight lang="autoit">
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_ReceiveData($hFile, 5000)
If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)
 
_CommAPI_ClosePort($hFile)
If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)
 
Return $sResult
EndFunc
</syntaxhighlight>
</syntaxhighlight>

Revision as of 15:17, 27 March 2014

Introduction

This site shows you some examples for CommAPI. You will also need the following snippet for all examples:

#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

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_TransmitData($hFile, $sCommand)
	If @error Then Return SetError(@error, @ScriptLineNumber)

	Local $sResult = _CommAPI_ReceiveData($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

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_TransmitData($hFile, IniRead($sFileINI, "COM", "Command", "Command" & @CRLF))
	If @error Then Return SetError(@error, @ScriptLineNumber)

	Local $sResult = _CommAPI_ReceiveData($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

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_TransmitData($hFile, $CmdLine[2])
	If @error Then Return SetError(@error, @ScriptLineNumber)

	Local $sResult = _CommAPI_ReceiveData($hFile, 5000)
	If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)

	_CommAPI_ClosePort($hFile)
	If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)

	Return $sResult
EndFunc
@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

Func Example()
	Local $hFile = _CommAPI_OpenCOMPort(1, 9600, 0, 8, 1)
	If @error Then Return SetError(@error, @ScriptLineNumber)

	Local $sResult = _CommAPI_ReceiveLine($hFile, "<CR>", 5000)
	If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)

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

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_ReceiveData($hFile, 5000)
	If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)

	_CommAPI_ClosePort($hFile)
	If @error Then Return SetError(@error, @ScriptLineNumber, $sResult)

	Return $sResult
EndFunc