CommAPI Examples

From AutoIt Wiki
Revision as of 13:56, 3 February 2014 by Therealhanuta (talk | contribs) (first example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 = 7
	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