CommAPI Examples: Difference between revisions
Jump to navigation
Jump to search
(→Command Line: add windows batch file example) |
mNo edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[Category:Samples]]This site shows you some examples for [[CommAPI]]. | [[Category:CommAPI]][[Category:Samples]] | ||
== Introduction == | |||
This site shows you some examples for [[CommAPI]]. | |||
You will also need the following snippet for all examples: | |||
<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 $ | Local $sResult = Example() | ||
Switch @error | |||
MsgBox(32, "Error", _WinAPI_GetLastErrorMessage()) | Case 0 | ||
MsgBox(64, "Result", $sResult) | |||
MsgBox(32, "Error", "Error in line " & | 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 | 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 | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
_CommAPI_ClearCommError($hFile) | _CommAPI_ClearCommError($hFile) | ||
If @error Then Return SetError(@error | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
_CommAPI_PurgeComm($hFile | _CommAPI_PurgeComm($hFile) | ||
If @error Then Return SetError(@error | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
_CommAPI_TransmitString($hFile, $sCommand) | |||
If @error Then Return SetError(@error | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
Local $sResult = | Local $sResult = _CommAPI_ReceiveString($hFile, 5000) | ||
If @error Then Return SetError(@error, @ | If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) | ||
_CommAPI_ClosePort($hFile) | _CommAPI_ClosePort($hFile) | ||
If @error Then Return SetError(@error, @ | If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) | ||
Return $sResult | |||
EndFunc | EndFunc | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 48: | Line 59: | ||
== INI file== | == INI file== | ||
<syntaxhighlight lang="autoit"> | <syntaxhighlight lang="autoit"> | ||
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, @ | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
Local $hFile = _CommAPI_OpenPort($sMode) | Local $hFile = _CommAPI_OpenPort($sMode) | ||
If @error Then Return SetError(@error, @ | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
_CommAPI_ClearCommError($hFile) | _CommAPI_ClearCommError($hFile) | ||
If @error Then Return SetError(@error | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
_CommAPI_PurgeComm($hFile | _CommAPI_PurgeComm($hFile) | ||
If @error Then Return SetError(@error | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
_CommAPI_TransmitString($hFile, IniRead($sFileINI, "COM", "Command", "Command" & @CRLF)) | |||
If @error Then Return SetError(@error | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
Local $sResult = | Local $sResult = _CommAPI_ReceiveString($hFile, IniRead($sFileINI, "Timeout", "IDSR", 5000)) | ||
If @error Then Return SetError(@error, @ | If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) | ||
_CommAPI_ClosePort($hFile) | _CommAPI_ClosePort($hFile) | ||
If @error Then Return SetError(@error, @ | If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) | ||
Return $sResult | |||
EndFunc | EndFunc | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 102: | Line 100: | ||
== Command Line == | == Command Line == | ||
<syntaxhighlight lang="autoit"> | <syntaxhighlight lang="autoit"> | ||
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"') | ||
Exit | |||
EndIf | EndIf | ||
Local $hFile = _CommAPI_OpenPort($CmdLine[1]) | Local $hFile = _CommAPI_OpenPort($CmdLine[1]) | ||
If @error Then Return SetError(@error, @ | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
_CommAPI_ClearCommError($hFile) | _CommAPI_ClearCommError($hFile) | ||
If @error Then Return SetError(@error | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
_CommAPI_PurgeComm($hFile | _CommAPI_PurgeComm($hFile) | ||
If @error Then Return SetError(@error | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
_CommAPI_TransmitString($hFile, $CmdLine[2]) | |||
If @error Then Return SetError(@error | If @error Then Return SetError(@error, @ScriptLineNumber) | ||
Local $sResult = | Local $sResult = _CommAPI_ReceiveString($hFile, 5000) | ||
If @error Then Return SetError(@error, @ | If @error Then Return SetError(@error, @ScriptLineNumber, $sResult) | ||
_CommAPI_ClosePort($hFile) | _CommAPI_ClosePort($hFile) | ||
If @error Then Return SetError(@error, @ | If @error Then Return SetError(@error, @ScriptLineNumber, $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 " | 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 = "" | |||
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 | |||
</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_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 | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 11:02, 7 April 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_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
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
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
@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 = ""
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.
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