CommAPIHelper.au3

From AutoIt Wiki
Revision as of 10:00, 7 March 2014 by Therealhanuta (talk | contribs) (Replace hex values with Constants)
Jump to navigation Jump to search
; #INDEX# =======================================================================================================================
; Name ..........: CommAPIHelper.au3
; Title .........: Communications Functions of Windows API
; Description ...: Communications Functions of Windows API calls that have been translated to AutoIt functions.
; Version Date ..: 2014-03-07
; 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 "CommAPI.au3"
#NoAutoIt3Execute
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_ChangeCommStateElement
; Description ...: Change one control setting for an element of a specified communications device.
; Syntax ........: _CommAPI_ChangeCommStateElement(Const $hFile, Const $sElement, Const $vValue)
; Parameters ....: $hFile               - [in] A handle to the communications device.
;                  $sElement            - [in] A element name of DCB structure.
;                  $vValue              - [in] A new value for the element.
; Return values .: Success - Previous data in the element of the struct.
;                  Failure - 0
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_SetCommStateElement, $tagDCB
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363214(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_ChangeCommStateElement(Const $hFile, Const $sElement, Const $vValue)
	Local $tDCB = DllStructCreate($tagDCB)
	If Not _CommAPI_GetCommState($hFile, $tDCB) Then Return SetError(@error, @extended, 0)
	Local $vResult = _CommAPI_SetCommStateElement($tDCB, $sElement, $vValue)
	If Not _CommAPI_SetCommState($hFile, $tDCB) Then Return SetError(@error, @extended, 0)
	Return $vResult
EndFunc   ;==>_CommAPI_ChangeCommStateElement

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_ChangeCommTimeoutsElement
; Description ...: Change one time-out parameter for an element off a specified communications device.
; Syntax ........: _CommAPI_ChangeCommTimeoutsElement(Const $hFile, Const $sElement, Const $vValue)
; Parameters ....: $hFile               - [in] A handle to the communications device.
;                  $sElement            - [in] A element name of COMMTIMEOUTS structure.
;                  $vValue              - [in] A new value for the element.
; Return values .: Success - Previous data in the element of the struct.
;                  Failure - 0
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_SetCommTimeoutsElement, $tagCOMMTIMEOUTS
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363190(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_ChangeCommTimeoutsElement(Const $hFile, Const $sElement, Const $vValue)
	Local $tCommTimeouts = DllStructCreate($tagCOMMTIMEOUTS)
	If Not _CommAPI_GetCommTimeouts($hFile, $tCommTimeouts) Then Return SetError(@error, @extended, 0)
	Local $vResult = _CommAPI_SetCommTimeoutsElement($tCommTimeouts, $sElement, $vValue)
	If Not _CommAPI_SetCommTimeouts($hFile, $tCommTimeouts) Then Return SetError(@error, @extended, 0)
	Return $vResult
EndFunc   ;==>_CommAPI_ChangeCommTimeoutsElement

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_GetCommStateElement
; Description ...: Retrieves one control setting for an element of a specified communications device.
; Syntax ........: _CommAPI_GetCommStateElement(Const $tDCB, Const $sElement)
; Parameters ....: $tDCB                - [in] A DCB structure.
;                  $sElement            - [in] A element name of DCB structure.
; Return values .: Success - Data in the element of the struct.
;                  Failure - 0
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_GetCommState, _CommAPI_SetCommStateElement, $tagDCB
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363214(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_GetCommStateElement(Const $tDCB, Const $sElement)
	Switch $sElement
		Case "fBinary"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 0))
		Case "fParity"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 1))
		Case "fOutxCTSFlow"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 2))
		Case "fOutxDSRFlow"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 3))
		Case "fDTRControl"
			Return BitAND(0x3, BitShift(DllStructGetData($tDCB, "fBitFields"), 4))
		Case "fDsrSensitivity"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 6))
		Case "fTXContinueOnXoff"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 7))
		Case "fOutX"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 8))
		Case "fInX"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 9))
		Case "fErrorChar"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 10))
		Case "fNull"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 11))
		Case "fRTSControl"
			Return BitAND(0x3, BitShift(DllStructGetData($tDCB, "fBitFields"), 12))
		Case "fAbortOnError"
			Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 14))
		Case "Dymmy2"
			Return BitAND(0x1FFFF, BitShift(DllStructGetData($tDCB, "fBitFields"), 15))
		Case Else
			Return DllStructGetData($tDCB, $sElement)
	EndSwitch
EndFunc   ;==>_CommAPI_GetCommStateElement

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_GetCommTimeoutsElement
; Description ...: Retrieves one time-out parameter for an element of a specified communications device.
; Syntax ........: _CommAPI_GetCommTimeoutsElement(Const $tCommTimeouts, Const $sElement)
; Parameters ....: $tCommTimeouts       - [in] A COMMTIMEOUTS structure.
;                  $sElement            - [in] A element name of DCB structure.
; Return values .: Success - Data in the element of the struct.
;                  Failure - 0
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_GetCommTimeouts, _CommAPI_SetCommTimeoutsElement, $tagCOMMTIMEOUTS
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363190(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_GetCommTimeoutsElement(Const $tCommTimeouts, Const $sElement)
	Return DllStructGetData($tCommTimeouts, $sElement)
EndFunc   ;==>_CommAPI_GetCommTimeoutsElement

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_IsOnCTS
; Description ...: The CTS (clear-to-send) signal is on.
; Syntax ........: _CommAPI_IsOnCTS(Const $hFile)
; Parameters ....: $hFile               - [in] A handle to the communications device.
; Return values .: Success - True
;                  Failure - False
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_GetCommModemStatus
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363258(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_IsOnCTS(Const $hFile)
	Local $iModemStatus = _CommAPI_GetCommModemStatus($hFile)
	If @error Then Return SetError(@error, @extended, False)
	If BitAND($iModemStatus, $MS_CTS_ON) Then Return True
	Return False
EndFunc   ;==>_CommAPI_IsOnCTS

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_IsOnDSR
; Description ...: The DSR (data-set-ready) signal is on.
; Syntax ........: _CommAPI_IsOnDSR(Const $hFile)
; Parameters ....: $hFile               - [in] A handle to the communications device.
; Return values .: Success - True
;                  Failure - False
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_GetCommModemStatus
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363258(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_IsOnDSR(Const $hFile)
	Local $iModemStatus = _CommAPI_GetCommModemStatus($hFile)
	If @error Then Return SetError(@error, @extended, False)
	If BitAND($iModemStatus, $MS_DSR_ON) Then Return True
	Return False
EndFunc   ;==>_CommAPI_IsOnDSR

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_IsOnRI
; Description ...: The RI (ring indicator) signal is on.
; Syntax ........: _CommAPI_IsOnRI(Const $hFile)
; Parameters ....: $hFile               - [in] A handle to the communications device.
; Return values .: Success - True
;                  Failure - False
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_GetCommModemStatus
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363258(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_IsOnRI(Const $hFile)
	Local $iModemStatus = _CommAPI_GetCommModemStatus($hFile)
	If @error Then Return SetError(@error, @extended, False)
	If BitAND($iModemStatus, $MS_RING_ON) Then Return True
	Return False
EndFunc   ;==>_CommAPI_IsOnRI

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_IsOnDCD
; Description ...: The DCD/CD/RLSD (Data Carrier Detect/Carrier Detect/receive-line-signal-detect) signal is on.
; Syntax ........: _CommAPI_IsOnDCD(Const $hFile)
; Parameters ....: $hFile               - [in] A handle to the communications device.
; Return values .: Success - True
;                  Failure - False
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_GetCommModemStatus
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363258(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_IsOnDCD(Const $hFile)
	Local $iModemStatus = _CommAPI_GetCommModemStatus($hFile)
	If @error Then Return SetError(@error, @extended, False)
	If BitAND($iModemStatus, $MS_RLSD_ON) Then Return True
	Return False
EndFunc   ;==>_CommAPI_IsOnDCD

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_SetCommStateElement
; Description ...: Set one control setting for an element of a specified communications device.
; Syntax ........: _CommAPI_SetCommStateElement(ByRef $tDCB, Const $sElement, Const $vValue)
; Parameters ....: $tDCB                - [in/out] A DCB structure.
;                  $sElement            - [in] A element name of DCB structure.
;                  $vValue              - [in] A new value for the element.
; Return values .: Success - Previous data in the element of the struct.
;                  Failure - 0
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_SetCommState, _CommAPI_GetCommStateElement, $tagDCB
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363214(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_SetCommStateElement(ByRef $tDCB, Const $sElement, Const $vValue)
	Local $vResult = 0
	Switch $sElement
		Case "fBinary", "fParity", "fOutxCTSFlow", "fOutxDSRFlow", "fDTRControl", "fDsrSensitivity", "fTXContinueOnXoff", "fOutX", "fInX", "fErrorChar", "fNull", "fRTSControl", "fAbortOnError", "Dymmy2"
			$vResult = DllStructGetData($tDCB, "fBitFields")
			If @error Then Return SetError(@error)
			Switch $sElement
				Case "fBinary"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -0), BitAND($vResult, 0xFFFFFFFE))
				Case "fParity"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -1), BitAND($vResult, 0xFFFFFFFD))
				Case "fOutxCTSFlow"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -2), BitAND($vResult, 0xFFFFFFFB))
				Case "fOutxDSRFlow"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -3), BitAND($vResult, 0xFFFFFFF7))
				Case "fDTRControl"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x3), -4), BitAND($vResult, 0xFFFFFFCF))
				Case "fDsrSensitivity"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -6), BitAND($vResult, 0xFFFFFFBF))
				Case "fTXContinueOnXoff"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -7), BitAND($vResult, 0xFFFFFF7F))
				Case "fOutX"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -8), BitAND($vResult, 0xFFFFFEFF))
				Case "fInX"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -9), BitAND($vResult, 0xFFFFFDFF))
				Case "fErrorChar"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -10), BitAND($vResult, 0xFFFFFBFF))
				Case "fNull"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -11), BitAND($vResult, 0xFFFFF7FF))
				Case "fRTSControl"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x3), -12), BitAND($vResult, 0xFFFFCFFF))
				Case "fAbortOnError"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -14), BitAND($vResult, 0xFFFFBFFF))
				Case "Dymmy2"
					$vResult = BitXOR(BitShift(BitAND($vValue, 0x1FFFF), -15), BitAND($vResult, 0x7FFF))
			EndSwitch
			$vResult = DllStructSetData($tDCB, "fBitFields", $vResult)
			If @error Then Return SetError(@error)
		Case Else
			$vResult = DllStructSetData($tDCB, $sElement, $vValue)
			If @error Then Return SetError(@error)
	EndSwitch
	Return $vResult
EndFunc   ;==>_CommAPI_SetCommStateElement

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_SetCommTimeoutsElement
; Description ...: Sets one time-out parameter for an element off a specified communications device.
; Syntax ........: _CommAPI_SetCommTimeoutsElement(Byref $tCommTimeouts, Const $sElement, Const $vValue)
; Parameters ....: $tCommTimeouts       - [in/out] A COMMTIMEOUTS structure.
;                  $sElement            - [in] A element name of COMMTIMEOUTS structure.
;                  $vValue              - [in] A new value for the element.
; Return values .: Success - Previous data in the element of the struct.
;                  Failure - 0
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_ChangeCommTimeoutsElement, _CommAPI_SetCommTimeouts, _CommAPI_GetCommTimeoutsElement, $tagCOMMTIMEOUTS
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363190(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_SetCommTimeoutsElement(ByRef $tCommTimeouts, Const $sElement, Const $vValue)
	Local $vResult = DllStructSetData($tCommTimeouts, $sElement, $vValue)
	If @error Then Return SetError(@error)
	Return $vResult
EndFunc   ;==>_CommAPI_SetCommTimeoutsElement

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_SetOnDTR
; Description ...: Sends or clears the DTR (data-terminal-ready) signal.
; Syntax ........: _CommAPI_SetOnDTR(Const $hFile, Const $iDTR)
; Parameters ....: $hFile               - [in] A handle to the communications device.
;                  $iDTR                - [in] A boolean value: True  - Sends the DTR (data-terminal-ready) signal.
;                                                               False - Clears the DTR (data-terminal-ready) signal.
; Return values .: Success - True
;                  Failure - False
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_EscapeCommFunction
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363254(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_SetOnDTR(Const $hFile, Const $iDTR)
	Local $iFunction = 0
	Switch StringUpper($iDTR)
		Case "0", "OFF", "FALSE", String($CLRDTR)
			$iFunction = $CLRDTR
		Case "1", "ON", "TRUE", String($SETDTR)
			$iFunction = $SETDTR
		Case Else
			Return False
	EndSwitch
	_CommAPI_EscapeCommFunction($hFile, $iFunction)
	If @error Then Return SetError(@error, @extended, False)
	Return True
EndFunc   ;==>_CommAPI_SetOnDTR

; #FUNCTION# ====================================================================================================================
; Name ..........: _CommAPI_SetOnRTS
; Description ...: Sends or clears the RTS (request-to-send) signal.
; Syntax ........: _CommAPI_SetOnRTS(Const $hFile, Const $iRTS)
; Parameters ....: $hFile               - [in] A handle to the communications device.
;                  $iRTS                - [in] A boolean value: True  - Sends the RTS (request-to-send) signal.
;                                                               False - Clears the RTS (request-to-send) signal.
; Return values .: Success - True
;                  Failure - False
; Author ........:
; Modified ......:
; Remarks .......:
; Related .......: _CommAPI_EscapeCommFunction
; Link ..........: http://msdn.microsoft.com/en-us/library/aa363254(v=vs.85).aspx
; Example .......: No
; ===============================================================================================================================
Func _CommAPI_SetOnRTS(Const $hFile, Const $iRTS)
	Local $iFunction = 0
	Switch StringUpper($iRTS)
		Case "0", "OFF", "FALSE", String($CLRRTS)
			$iFunction = $CLRRTS
		Case "1", "ON", "TRUE", String($SETRTS)
			$iFunction = $SETRTS
		Case Else
			Return False
	EndSwitch
	_CommAPI_EscapeCommFunction($hFile, $iFunction)
	If @error Then Return SetError(@error, @extended, False)
	Return True
EndFunc   ;==>_CommAPI_SetOnRTS