CommAPI Examples: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
Line 1: | Line 1: | ||
[[Category:CommAPI]][[Category:Samples]]== Introduction == | [[Category:CommAPI]][[Category:Samples]] | ||
== Introduction == | |||
This site shows you some examples for [[CommAPI]]. | This site shows you some examples for [[CommAPI]]. | ||
You will also need the following snippet for all examples: | You will also need the following snippet for all examples: |
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