CommAPIHelper.au3: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
(Replace hex values with Constants)
(updated @error handling)
Line 10: Line 10:
; Author(s) .....:
; Author(s) .....:
; Dll(s) ........: kernel32.dll
; Dll(s) ........: kernel32.dll
; Error handling : Everytime @extended is set, it is filled with @ScriptLineNumber of the error in CommAPI.au3.
; Error handling : Everytime @extended is set, it is filled with @ScriptLineNumber of the error.
;                  Everytime @extended is set, you can call _WinAPI_GetLastError or _WinAPI_GetLastErrorMessage.
; ===============================================================================================================================
; ===============================================================================================================================


Line 28: Line 27:
; Return values .: Success - Previous data in the element of the struct.
; Return values .: Success - Previous data in the element of the struct.
;                  Failure - 0
;                  Failure - 0
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 0
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 37: Line 41:
Func _CommAPI_ChangeCommStateElement(Const $hFile, Const $sElement, Const $vValue)
Func _CommAPI_ChangeCommStateElement(Const $hFile, Const $sElement, Const $vValue)
Local $tDCB = DllStructCreate($tagDCB)
Local $tDCB = DllStructCreate($tagDCB)
If Not _CommAPI_GetCommState($hFile, $tDCB) Then Return SetError(@error, @extended, 0)
_CommAPI_GetCommState($hFile, $tDCB)
If @error Then Return SetError(@error, @extended, 0)
Local $vResult = _CommAPI_SetCommStateElement($tDCB, $sElement, $vValue)
Local $vResult = _CommAPI_SetCommStateElement($tDCB, $sElement, $vValue)
If Not _CommAPI_SetCommState($hFile, $tDCB) Then Return SetError(@error, @extended, 0)
_CommAPI_SetCommState($hFile, $tDCB)
If @error Then Return SetError(@error, @extended, 0)
Return $vResult
Return $vResult
EndFunc  ;==>_CommAPI_ChangeCommStateElement
EndFunc  ;==>_CommAPI_ChangeCommStateElement
Line 52: Line 58:
; Return values .: Success - Previous data in the element of the struct.
; Return values .: Success - Previous data in the element of the struct.
;                  Failure - 0
;                  Failure - 0
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 0
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 61: Line 72:
Func _CommAPI_ChangeCommTimeoutsElement(Const $hFile, Const $sElement, Const $vValue)
Func _CommAPI_ChangeCommTimeoutsElement(Const $hFile, Const $sElement, Const $vValue)
Local $tCommTimeouts = DllStructCreate($tagCOMMTIMEOUTS)
Local $tCommTimeouts = DllStructCreate($tagCOMMTIMEOUTS)
If Not _CommAPI_GetCommTimeouts($hFile, $tCommTimeouts) Then Return SetError(@error, @extended, 0)
_CommAPI_GetCommTimeouts($hFile, $tCommTimeouts)
If @error Then Return SetError(@error, @extended, 0)
Local $vResult = _CommAPI_SetCommTimeoutsElement($tCommTimeouts, $sElement, $vValue)
Local $vResult = _CommAPI_SetCommTimeoutsElement($tCommTimeouts, $sElement, $vValue)
If Not _CommAPI_SetCommTimeouts($hFile, $tCommTimeouts) Then Return SetError(@error, @extended, 0)
_CommAPI_SetCommTimeouts($hFile, $tCommTimeouts)
If @error Then Return SetError(@error, @extended, 0)
Return $vResult
Return $vResult
EndFunc  ;==>_CommAPI_ChangeCommTimeoutsElement
EndFunc  ;==>_CommAPI_ChangeCommTimeoutsElement
Line 75: Line 88:
; Return values .: Success - Data in the element of the struct.
; Return values .: Success - Data in the element of the struct.
;                  Failure - 0
;                  Failure - 0
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 0
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 83: Line 101:
; ===============================================================================================================================
; ===============================================================================================================================
Func _CommAPI_GetCommStateElement(Const $tDCB, Const $sElement)
Func _CommAPI_GetCommStateElement(Const $tDCB, Const $sElement)
Local $vResult = 0
Local $iShift = 0
Local $bValid = 0x1
Switch $sElement
Switch $sElement
Case "fBinary"
Case "fBinary"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 0))
Case "fParity"
Case "fParity"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 1))
$iShift = 1
Case "fOutxCTSFlow"
Case "fOutxCTSFlow"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 2))
$iShift = 2
Case "fOutxDSRFlow"
Case "fOutxDSRFlow"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 3))
$iShift = 3
Case "fDTRControl"
Case "fDTRControl"
Return BitAND(0x3, BitShift(DllStructGetData($tDCB, "fBitFields"), 4))
$iShift = 4
$bValid = 0x3
Case "fDsrSensitivity"
Case "fDsrSensitivity"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 6))
$iShift = 6
Case "fTXContinueOnXoff"
Case "fTXContinueOnXoff"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 7))
$iShift = 7
Case "fOutX"
Case "fOutX"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 8))
$iShift = 8
Case "fInX"
Case "fInX"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 9))
$iShift = 9
Case "fErrorChar"
Case "fErrorChar"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 10))
$iShift = 10
Case "fNull"
Case "fNull"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 11))
$iShift = 11
Case "fRTSControl"
Case "fRTSControl"
Return BitAND(0x3, BitShift(DllStructGetData($tDCB, "fBitFields"), 12))
$iShift = 12
$bValid = 0x3
Case "fAbortOnError"
Case "fAbortOnError"
Return BitAND(0x1, BitShift(DllStructGetData($tDCB, "fBitFields"), 14))
$iShift = 14
Case "Dymmy2"
Case "Dymmy2"
Return BitAND(0x1FFFF, BitShift(DllStructGetData($tDCB, "fBitFields"), 15))
$iShift = 15
$bValid = 0x1FFFF
Case Else
Case Else
Return DllStructGetData($tDCB, $sElement)
$vResult = DllStructGetData($tDCB, $sElement)
If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
Return $vResult
EndSwitch
EndSwitch
$vResult = DllStructGetData($tDCB, "fBitFields")
If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
Return BitAND($bValid, BitShift($vResult, $iShift))
EndFunc  ;==>_CommAPI_GetCommStateElement
EndFunc  ;==>_CommAPI_GetCommStateElement


Line 125: Line 153:
; Return values .: Success - Data in the element of the struct.
; Return values .: Success - Data in the element of the struct.
;                  Failure - 0
;                  Failure - 0
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 0
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 133: Line 166:
; ===============================================================================================================================
; ===============================================================================================================================
Func _CommAPI_GetCommTimeoutsElement(Const $tCommTimeouts, Const $sElement)
Func _CommAPI_GetCommTimeoutsElement(Const $tCommTimeouts, Const $sElement)
Return DllStructGetData($tCommTimeouts, $sElement)
Local $vResult = 0
$vResult = DllStructGetData($tCommTimeouts, $sElement)
If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
Return $vResult
EndFunc  ;==>_CommAPI_GetCommTimeoutsElement
EndFunc  ;==>_CommAPI_GetCommTimeoutsElement


Line 143: Line 179:
; Return values .: Success - True
; Return values .: Success - True
;                  Failure - False
;                  Failure - False
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 164: Line 205:
; Return values .: Success - True
; Return values .: Success - True
;                  Failure - False
;                  Failure - False
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 185: Line 231:
; Return values .: Success - True
; Return values .: Success - True
;                  Failure - False
;                  Failure - False
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 206: Line 257:
; Return values .: Success - True
; Return values .: Success - True
;                  Failure - False
;                  Failure - False
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 229: Line 285:
; Return values .: Success - Previous data in the element of the struct.
; Return values .: Success - Previous data in the element of the struct.
;                  Failure - 0
;                  Failure - 0
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 0
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 238: Line 299:
Func _CommAPI_SetCommStateElement(ByRef $tDCB, Const $sElement, Const $vValue)
Func _CommAPI_SetCommStateElement(ByRef $tDCB, Const $sElement, Const $vValue)
Local $vResult = 0
Local $vResult = 0
Local $iShift = 0
Local $bValid = 0x1
Local $bMask = 0xFFFFFFFF
Switch $sElement
Switch $sElement
Case "fBinary", "fParity", "fOutxCTSFlow", "fOutxDSRFlow", "fDTRControl", "fDsrSensitivity", "fTXContinueOnXoff", "fOutX", "fInX", "fErrorChar", "fNull", "fRTSControl", "fAbortOnError", "Dymmy2"
Case "fBinary"
$vResult = DllStructGetData($tDCB, "fBitFields")
$bMask = 0xFFFFFFFE
If @error Then Return SetError(@error)
Case "fParity"
Switch $sElement
$iShift = -1
Case "fBinary"
$bMask = 0xFFFFFFFD
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -0), BitAND($vResult, 0xFFFFFFFE))
Case "fOutxCTSFlow"
Case "fParity"
$iShift = -2
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -1), BitAND($vResult, 0xFFFFFFFD))
$bMask = 0xFFFFFFFB
Case "fOutxCTSFlow"
Case "fOutxDSRFlow"
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -2), BitAND($vResult, 0xFFFFFFFB))
$iShift = -3
Case "fOutxDSRFlow"
$bMask = 0xFFFFFFF7
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -3), BitAND($vResult, 0xFFFFFFF7))
Case "fDTRControl"
Case "fDTRControl"
$iShift = -4
$vResult = BitXOR(BitShift(BitAND($vValue, 0x3), -4), BitAND($vResult, 0xFFFFFFCF))
$bValid = 0x3
Case "fDsrSensitivity"
$bMask = 0xFFFFFFCF
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -6), BitAND($vResult, 0xFFFFFFBF))
Case "fDsrSensitivity"
Case "fTXContinueOnXoff"
$iShift = -6
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -7), BitAND($vResult, 0xFFFFFF7F))
$bMask = 0xFFFFFFBF
Case "fOutX"
Case "fTXContinueOnXoff"
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -8), BitAND($vResult, 0xFFFFFEFF))
$iShift = -7
Case "fInX"
$bMask = 0xFFFFFF7F
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -9), BitAND($vResult, 0xFFFFFDFF))
Case "fOutX"
Case "fErrorChar"
$iShift = -8
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -10), BitAND($vResult, 0xFFFFFBFF))
$bMask = 0xFFFFFEFF
Case "fNull"
Case "fInX"
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -11), BitAND($vResult, 0xFFFFF7FF))
$iShift = -9
Case "fRTSControl"
$bMask = 0xFFFFFDFF
$vResult = BitXOR(BitShift(BitAND($vValue, 0x3), -12), BitAND($vResult, 0xFFFFCFFF))
Case "fErrorChar"
Case "fAbortOnError"
$iShift = -10
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1), -14), BitAND($vResult, 0xFFFFBFFF))
$bMask = 0xFFFFFBFF
Case "Dymmy2"
Case "fNull"
$vResult = BitXOR(BitShift(BitAND($vValue, 0x1FFFF), -15), BitAND($vResult, 0x7FFF))
$iShift = -11
EndSwitch
$bMask = 0xFFFFF7FF
$vResult = DllStructSetData($tDCB, "fBitFields", $vResult)
Case "fRTSControl"
If @error Then Return SetError(@error)
$iShift = -12
$bValid = 0x3
$bMask = 0xFFFFCFFF
Case "fAbortOnError"
$iShift = -14
$bMask = 0xFFFFBFFF
Case "Dymmy2"
$iShift = -15
$bValid = 0x1FFFF
$bMask = 0x7FFF
Case Else
Case Else
$vResult = DllStructSetData($tDCB, $sElement, $vValue)
$vResult = DllStructSetData($tDCB, $sElement, $vValue)
If @error Then Return SetError(@error)
If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
Return $vResult
EndSwitch
EndSwitch
$vResult = DllStructGetData($tDCB, "fBitFields")
If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
$vResult = BitXOR(BitShift(BitAND($vValue, $bValid), $iShift), BitAND($vResult, $bMask))
$vResult = DllStructSetData($tDCB, "fBitFields", $vResult)
If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
Return $vResult
Return $vResult
EndFunc  ;==>_CommAPI_SetCommStateElement
EndFunc  ;==>_CommAPI_SetCommStateElement
Line 290: Line 369:
; Return values .: Success - Previous data in the element of the struct.
; Return values .: Success - Previous data in the element of the struct.
;                  Failure - 0
;                  Failure - 0
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 0
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 299: Line 383:
Func _CommAPI_SetCommTimeoutsElement(ByRef $tCommTimeouts, Const $sElement, Const $vValue)
Func _CommAPI_SetCommTimeoutsElement(ByRef $tCommTimeouts, Const $sElement, Const $vValue)
Local $vResult = DllStructSetData($tCommTimeouts, $sElement, $vValue)
Local $vResult = DllStructSetData($tCommTimeouts, $sElement, $vValue)
If @error Then Return SetError(@error)
If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
Return $vResult
Return $vResult
EndFunc  ;==>_CommAPI_SetCommTimeoutsElement
EndFunc  ;==>_CommAPI_SetCommTimeoutsElement
Line 312: Line 396:
; Return values .: Success - True
; Return values .: Success - True
;                  Failure - False
;                  Failure - False
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:
Line 343: Line 432:
; Return values .: Success - True
; Return values .: Success - True
;                  Failure - False
;                  Failure - False
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; Author ........:
; Author ........:
; Modified ......:
; Modified ......:

Revision as of 15:22, 27 March 2014

; #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.
; ===============================================================================================================================

#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
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 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)
	_CommAPI_GetCommState($hFile, $tDCB)
	If @error Then Return SetError(@error, @extended, 0)
	Local $vResult = _CommAPI_SetCommStateElement($tDCB, $sElement, $vValue)
	_CommAPI_SetCommState($hFile, $tDCB)
	If @error 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
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 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)
	_CommAPI_GetCommTimeouts($hFile, $tCommTimeouts)
	If @error Then Return SetError(@error, @extended, 0)
	Local $vResult = _CommAPI_SetCommTimeoutsElement($tCommTimeouts, $sElement, $vValue)
	_CommAPI_SetCommTimeouts($hFile, $tCommTimeouts)
	If @error 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
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 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)
	Local $vResult = 0
	Local $iShift = 0
	Local $bValid = 0x1
	Switch $sElement
		Case "fBinary"
		Case "fParity"
			$iShift = 1
		Case "fOutxCTSFlow"
			$iShift = 2
		Case "fOutxDSRFlow"
			$iShift = 3
		Case "fDTRControl"
			$iShift = 4
			$bValid = 0x3
		Case "fDsrSensitivity"
			$iShift = 6
		Case "fTXContinueOnXoff"
			$iShift = 7
		Case "fOutX"
			$iShift = 8
		Case "fInX"
			$iShift = 9
		Case "fErrorChar"
			$iShift = 10
		Case "fNull"
			$iShift = 11
		Case "fRTSControl"
			$iShift = 12
			$bValid = 0x3
		Case "fAbortOnError"
			$iShift = 14
		Case "Dymmy2"
			$iShift = 15
			$bValid = 0x1FFFF
		Case Else
			$vResult = DllStructGetData($tDCB, $sElement)
			If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
			Return $vResult
	EndSwitch
	$vResult = DllStructGetData($tDCB, "fBitFields")
	If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
	Return BitAND($bValid, BitShift($vResult, $iShift))
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
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 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)
	Local $vResult = 0
	$vResult = DllStructGetData($tCommTimeouts, $sElement)
	If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
	Return $vResult
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
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; 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
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; 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
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; 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
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; 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
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 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
	Local $iShift = 0
	Local $bValid = 0x1
	Local $bMask = 0xFFFFFFFF
	Switch $sElement
		Case "fBinary"
			$bMask = 0xFFFFFFFE
		Case "fParity"
			$iShift = -1
			$bMask = 0xFFFFFFFD
		Case "fOutxCTSFlow"
			$iShift = -2
			$bMask = 0xFFFFFFFB
		Case "fOutxDSRFlow"
			$iShift = -3
			$bMask = 0xFFFFFFF7
		Case "fDTRControl"
			$iShift = -4
			$bValid = 0x3
			$bMask = 0xFFFFFFCF
		Case "fDsrSensitivity"
			$iShift = -6
			$bMask = 0xFFFFFFBF
		Case "fTXContinueOnXoff"
			$iShift = -7
			$bMask = 0xFFFFFF7F
		Case "fOutX"
			$iShift = -8
			$bMask = 0xFFFFFEFF
		Case "fInX"
			$iShift = -9
			$bMask = 0xFFFFFDFF
		Case "fErrorChar"
			$iShift = -10
			$bMask = 0xFFFFFBFF
		Case "fNull"
			$iShift = -11
			$bMask = 0xFFFFF7FF
		Case "fRTSControl"
			$iShift = -12
			$bValid = 0x3
			$bMask = 0xFFFFCFFF
		Case "fAbortOnError"
			$iShift = -14
			$bMask = 0xFFFFBFFF
		Case "Dymmy2"
			$iShift = -15
			$bValid = 0x1FFFF
			$bMask = 0x7FFF
		Case Else
			$vResult = DllStructSetData($tDCB, $sElement, $vValue)
			If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
			Return $vResult
	EndSwitch
	$vResult = DllStructGetData($tDCB, "fBitFields")
	If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
	$vResult = BitXOR(BitShift(BitAND($vValue, $bValid), $iShift), BitAND($vResult, $bMask))
	$vResult = DllStructSetData($tDCB, "fBitFields", $vResult)
	If @error Then Return SetError(@error+10, @ScriptLineNumber, 0)
	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
;                  @error  - 11 struct not a correct struct returned by DllStructCreate
;                            12 element value out of range
;                            13 index would be outside of the struct
;                            14 element data type is unknown
;                            15 index <= 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+10, @ScriptLineNumber, 0)
	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
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; 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
;                  @error  - 1 unable to use the DLL file
;                            2 unknown "return type"
;                            3 "function" not found in the DLL file
;                            4 bad number of parameters
;                            5 bad parameter
; 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