CommInterface.au3: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
m (add to Category:Script)
(better timeout handling)
Line 4: Line 4:
; Title .........: Communications Functions of Windows API
; Title .........: Communications Functions of Windows API
; Description ...: Communications Functions of Windows API calls that have been translated to AutoIt functions.
; Description ...: Communications Functions of Windows API calls that have been translated to AutoIt functions.
; Version Date ..: 2013-10-24
; Version Date ..: 2014-02-03
; AutoIt Version : 3.3.8.1
; AutoIt Version : 3.3.8.1
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363194(v=vs.85).aspx
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363194(v=vs.85).aspx
Line 45: Line 45:
; Description ...: Opens a COM Port.
; Description ...: Opens a COM Port.
; Syntax ........: _CommAPI_OpenCOMPort(Const $iPort[, $iBaudRate = Default[, $iParity = Default[, $iByteSize = Default[,
; Syntax ........: _CommAPI_OpenCOMPort(Const $iPort[, $iBaudRate = Default[, $iParity = Default[, $iByteSize = Default[,
;                  $iStopBits = Default[, $iReadTotalTimeout = 100]]]]])
;                  $iStopBits = Default]]]])
; Parameters ....: $iPort              - [in] A port number.
; Parameters ....: $iPort              - [in] A port number.
;                  $iBaudRate          - [in] The baud rate at which the communications device operates.
;                  $iBaudRate          - [in] The baud rate at which the communications device operates.
Line 51: Line 51:
;                  $iByteSize          - [in] Specifies the number of data bits in a character.
;                  $iByteSize          - [in] Specifies the number of data bits in a character.
;                  $iStopBits          - [in] Specifies the number of stop bits that define the end of a character: 1, 1.5, or 2.
;                  $iStopBits          - [in] Specifies the number of stop bits that define the end of a character: 1, 1.5, or 2.
;                  $iReadTotalTimeout  - [in] An integer value for total read timeout in milliseconds.
; Return values .: Success - The open handle to a specified communications device.
; Return values .: Success - The open handle to a specified communications device.
;                  Failure - 0
;                  Failure - 0
Line 61: Line 60:
; Example .......: No
; Example .......: No
; ===============================================================================================================================
; ===============================================================================================================================
Func _CommAPI_OpenCOMPort(Const $iPort, Const $iBaudRate = Default, Const $iParity = Default, Const $iByteSize = Default, Const $iStopBits = Default, Const $iReadTotalTimeout = 100)
Func _CommAPI_OpenCOMPort(Const $iPort, Const $iBaudRate = Default, Const $iParity = Default, Const $iByteSize = Default, Const $iStopBits = Default)
Local $sMode = _CommAPI_CreateModeString($iPort, $iBaudRate, $iParity, $iByteSize, $iStopBits)
Local $sMode = _CommAPI_CreateModeString($iPort, $iBaudRate, $iParity, $iByteSize, $iStopBits)
Local $hFile = _CommAPI_OpenPort($sMode, $iReadTotalTimeout)
Local $hFile = _CommAPI_OpenPort($sMode)
If @error Then Return SetError(@error, @extended, 0)
If @error Then Return SetError(@error, @extended, 0)
Return $hFile
Return $hFile
Line 71: Line 70:
; Name ..........: _CommAPI_OpenPort
; Name ..........: _CommAPI_OpenPort
; Description ...: Opens a specified communications device.
; Description ...: Opens a specified communications device.
; Syntax ........: _CommAPI_OpenPort(Const $sMode[, $iReadTotalTimeout = 100])
; Syntax ........: _CommAPI_OpenPort(Const $sMode)
; Parameters ....: $sMode              - [in] A device-definition string.
; Parameters ....: $sMode              - [in] A device-definition string.
;                  $iReadTotalTimeout  - [in] An integer value for total read timeout in milliseconds.
; Return values .: Success - The open handle to a specified communications device.
; Return values .: Success - The open handle to a specified communications device.
;                  Failure - 0
;                  Failure - 0
Line 83: Line 81:
; Example .......: No
; Example .......: No
; ===============================================================================================================================
; ===============================================================================================================================
Func _CommAPI_OpenPort(Const $sMode, Const $iReadTotalTimeout = 100)
Func _CommAPI_OpenPort(Const $sMode)
Local $sFileName = "\\.\" & StringLeft($sMode, StringInStr($sMode, ":") - 1)
Local $sFileName = "\\.\" & StringLeft($sMode, StringInStr($sMode, ":") - 1)
Local $hFile = _WinAPI_CreateFile($sFileName, 2, 6)
Local $hFile = _WinAPI_CreateFile($sFileName, 2, 6)
Line 92: Line 90:
Local $tCommTimeouts = DllStructCreate($tagCOMMTIMEOUTS)
Local $tCommTimeouts = DllStructCreate($tagCOMMTIMEOUTS)


_CommAPI_BuildCommDCBAndTimeouts($sMode, $tDCB, $tCommTimeouts)
_CommAPI_BuildCommDCB($sMode, $tDCB)
If @error Then Return SetError(@error, @extended, 0)
If @error Then Return SetError(@error, @extended, 0)


_CommAPI_SetCommTimeoutsElement($tCommTimeouts, "ReadTotalTimeoutConstant", $iReadTotalTimeout)
_CommAPI_SetCommTimeoutsElement($tCommTimeouts, "ReadTotalTimeoutMultiplier", 1)
If @error Then Return SetError(@error, @extended, 0)
 
_CommAPI_SetCommTimeoutsElement($tCommTimeouts, "WriteTotalTimeoutMultiplier", 1)
If @error Then Return SetError(@error, @extended, 0)
If @error Then Return SetError(@error, @extended, 0)


Line 107: Line 108:
; Name ..........: _CommAPI_ReceiveData
; Name ..........: _CommAPI_ReceiveData
; Description ...: Receives data (RxD/RX/RD) to a specified communications device.
; Description ...: Receives data (RxD/RX/RD) to a specified communications device.
; Syntax ........: _CommAPI_ReceiveData(Const $hFile[, $iMinBufferSize = 1[, $iMaxWaitTime = 100]])
; Syntax ........: _CommAPI_ReceiveData(Const $hFile[, $iTimeout = 0])
; Parameters ....: $hFile              - [in] A handle to the communications device.
; Parameters ....: $hFile              - [in] A handle to the communications device.
;                  $iTimeout            - [in] An integer value for total read timeout in milliseconds.
; Return values .: Success - Received string
; Return values .: Success - Received string
;                  Failure - Empty string
;                  Failure - Empty string
Line 118: Line 120:
; Example .......: No
; Example .......: No
; ===============================================================================================================================
; ===============================================================================================================================
Func _CommAPI_ReceiveData(Const $hFile)
Func _CommAPI_ReceiveData(Const $hFile, Const $iTimeout = 0)
Local $tBuffer = DllStructCreate("char")
Local $tBuffer = DllStructCreate("char")
Local $hTimer = TimerInit()
Local $iWritten = 0
Local $iWritten = 0
Local $sResult = ""
Local $sResult = ""
Line 126: Line 129:
If @error Then Return SetError(@error, 0, $sResult)
If @error Then Return SetError(@error, 0, $sResult)
If $iWritten Then $sResult &= DllStructGetData($tBuffer, 1)
If $iWritten Then $sResult &= DllStructGetData($tBuffer, 1)
Until Not $iWritten
Until Not $iWritten And ( $sResult Or $iTimeout < TimerDiff($hTimer) )
Return $sResult
Return $sResult
EndFunc  ;==>_CommAPI_ReceiveData
EndFunc  ;==>_CommAPI_ReceiveData

Revision as of 12:59, 3 February 2014

; #INDEX# =======================================================================================================================
; Name ..........: CommInterface.au3
; Title .........: Communications Functions of Windows API
; Description ...: Communications Functions of Windows API calls that have been translated to AutoIt functions.
; Version Date ..: 2014-02-03
; AutoIt Version : 3.3.8.1
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363194(v=vs.85).aspx
; Tag(s) ........: RS-232, serial port, COM port
; Author(s) .....:
; Dll(s) ........: kernel32.dll
; Error handling : Everytime @extended is set, it is filled with @ScriptLineNumber of the error in CommAPI.au3.
;                  Everytime @extended is set, you can call _WinAPI_GetLastError or _WinAPI_GetLastErrorMessage.
; ===============================================================================================================================

#include-once
#include "CommAPIHelper.au3"
#include "CommUtilities.au3"
#include <WinAPI.au3>
#NoAutoIt3Execute
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_ClosePort
; Description ...: CLoses a specified communications device.
; Syntax ........: _CommAPI_ClosePort(Const $hFile)
; Parameters ....: $hFile               - [in] A handle to the communications device.
; Return values .: Success - True
;                  Failure - False
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_OpenPort
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_ClosePort(Const $hFile)
	Local $fResult = _WinAPI_CloseHandle($hFile)
	If @error Then Return SetError(@error, 0, False)
	Return $fResult
EndFunc   ;==>_CommAPI_ClosePort

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_OpenCOMPort
; Description ...: Opens a COM Port.
; Syntax ........: _CommAPI_OpenCOMPort(Const $iPort[, $iBaudRate = Default[, $iParity = Default[, $iByteSize = Default[,
;                  $iStopBits = Default]]]])
; Parameters ....: $iPort               - [in] A port number.
;                  $iBaudRate           - [in] The baud rate at which the communications device operates.
;                  $iParity             - [in] The parity scheme to be used.
;                  $iByteSize           - [in] Specifies the number of data bits in a character.
;                  $iStopBits           - [in] Specifies the number of stop bits that define the end of a character: 1, 1.5, or 2.
; Return values .: Success - The open handle to a specified communications device.
;                  Failure - 0
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_OpenPort, _CommAPI_ClosePort, _CommAPI_CreateModeString
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363214(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_OpenCOMPort(Const $iPort, Const $iBaudRate = Default, Const $iParity = Default, Const $iByteSize = Default, Const $iStopBits = Default)
	Local $sMode = _CommAPI_CreateModeString($iPort, $iBaudRate, $iParity, $iByteSize, $iStopBits)
	Local $hFile = _CommAPI_OpenPort($sMode)
	If @error Then Return SetError(@error, @extended, 0)
	Return $hFile
EndFunc   ;==>_CommAPI_OpenCOMPort

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_OpenPort
; Description ...: Opens a specified communications device.
; Syntax ........: _CommAPI_OpenPort(Const $sMode)
; Parameters ....: $sMode               - [in] A device-definition string.
; Return values .: Success - The open handle to a specified communications device.
;                  Failure - 0
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_OpenCOMPort, _CommAPI_ClosePort, _CommAPI_SetCommState
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363214(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_OpenPort(Const $sMode)
	Local $sFileName = "\\.\" & StringLeft($sMode, StringInStr($sMode, ":") - 1)
	Local $hFile = _WinAPI_CreateFile($sFileName, 2, 6)
	If @error Then Return SetError(@error, @ScriptLineNumber, 0)
	If $hFile <= 0 Then Return SetError(-1, @ScriptLineNumber, 0)

	Local $tDCB = DllStructCreate($tagDCB)
	Local $tCommTimeouts = DllStructCreate($tagCOMMTIMEOUTS)

	_CommAPI_BuildCommDCB($sMode, $tDCB)
	If @error Then Return SetError(@error, @extended, 0)

	_CommAPI_SetCommTimeoutsElement($tCommTimeouts, "ReadTotalTimeoutMultiplier", 1)
	If @error Then Return SetError(@error, @extended, 0)

	_CommAPI_SetCommTimeoutsElement($tCommTimeouts, "WriteTotalTimeoutMultiplier", 1)
	If @error Then Return SetError(@error, @extended, 0)

	If Not _CommAPI_SetCommState($hFile, $tDCB) Then Return SetError(@error, @extended, 0)
	If Not _CommAPI_SetCommTimeouts($hFile, $tCommTimeouts) Then Return SetError(@error, @extended, 0)

	Return $hFile
EndFunc   ;==>_CommAPI_OpenPort

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_ReceiveData
; Description ...: Receives data (RxD/RX/RD) to a specified communications device.
; Syntax ........: _CommAPI_ReceiveData(Const $hFile[, $iTimeout = 0])
; Parameters ....: $hFile               - [in] A handle to the communications device.
;                  $iTimeout            - [in] An integer value for total read timeout in milliseconds.
; Return values .: Success - Received string
;                  Failure - Empty string
; Author ........:
; Modified ......:
; Remarks .......: If the result contains Chr(0), you should convert the result with function Binary().
; Related .......: _CommAPI_TransmitData
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_ReceiveData(Const $hFile, Const $iTimeout = 0)
	Local $tBuffer = DllStructCreate("char")
	Local $hTimer = TimerInit()
	Local $iWritten = 0
	Local $sResult = ""
	Do
		_WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $iWritten)
		If @error Then Return SetError(@error, 0, $sResult)
		If $iWritten Then $sResult &= DllStructGetData($tBuffer, 1)
	Until Not $iWritten And ( $sResult Or $iTimeout < TimerDiff($hTimer) )
	Return $sResult
EndFunc   ;==>_CommAPI_ReceiveData

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_TransmitData
; Description ...: Transmits data (TxD/TX/TD) to a specified communications device.
; Syntax ........: _CommAPI_TransmitData(Const $hFile, Const $sData)
; Parameters ....: $hFile               - [in] A handle to the communications device.
;                  $sData               - [in] A string value to transmit.
; Return values .: Success - The number of bytes written.
;                  Failure - 0
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_ReceiveData
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_TransmitData(Const $hFile, Const $sData)
	Local $iWritten = 0
	Local $tBuffer = DllStructCreate("byte[" & StringLen($sData) & "]")
	DllStructSetData($tBuffer, 1, $sData)
	If @error Then Return SetError(@error)
	_WinAPI_WriteFile($hFile, DllStructGetPtr($tBuffer), StringLen($sData), $iWritten)
	If @error Then Return SetError(@error)
	Return $iWritten
EndFunc   ;==>_CommAPI_TransmitData