CommAPI Examples: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
(second example)
(third example)
Line 91: Line 91:


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)
_CommAPI_ClosePort($hFile)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
MsgBox(64, "Result", $sResult)
EndFunc
</syntaxhighlight>
== Command Line ==
<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()
If $CmdLine[0] <> 2 Then
MsgBox(32, "Syntax", @ScriptName & ' "ModeString" "Command"')
Return
EndIf
Local $hFile = _CommAPI_OpenPort($CmdLine[1])
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, $CmdLine[2])
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
Local $sResult = _CommAPI_ReceiveData($hFile, 5000)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)
If @error Then Return SetError(@error, @extended, @ScriptLineNumber)



Revision as of 14:49, 3 February 2014

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

Command Line

#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()
	If $CmdLine[0] <> 2 Then
		MsgBox(32, "Syntax", @ScriptName & ' "ModeString" "Command"')
		Return
	EndIf

	Local $hFile = _CommAPI_OpenPort($CmdLine[1])
	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, $CmdLine[2])
	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