CommAPI Examples

From AutoIt Wiki
Revision as of 14:16, 3 February 2014 by Therealhanuta (talk | contribs) (second example)
Jump to navigation Jump to search

This site shows you some examples for CommAPI.

First example

#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()
	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, @extended, @ScriptLineNumber)

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

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

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

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

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

	MsgBox(64, "Result", $sResult)
EndFunc

INI file

#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()
	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, @extended)

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

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

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

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

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

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

	MsgBox(64, "Result", $sResult)
EndFunc