; inspiré par http://kr3l.wordpress.com/2008/12/07/serial-com-port-communication-with-autoit/ #include Func SetReadTimeout($h, $t = 500) Local $timeouts = DllStructCreate("DWORD ReadIntervalTimeout;DWORD ReadTotalTimeoutMultiplier;DWORD ReadTotalTimeoutConstant;DWORD WriteTotalTimeoutMultiplier;DWORD WriteTotalTimeoutConstant;") DllStructSetData($timeouts, "ReadIntervalTimeout", -1) DllStructSetData($timeouts, "ReadTotalTimeoutMultiplier", -1) DllStructSetData($timeouts, "ReadTotalTimeoutConstant", $t) DllStructSetData($timeouts, "WriteTotalTimeoutMultiplier", 0) DllStructSetData($timeouts, "WriteTotalTimeoutConstant", 0) DllCall("Kernel32.dll", "bool", "SetCommTimeouts", "handle", $h, "ptr", DllStructGetPtr($timeouts)) $timeouts = 0 EndFunc Func SetComParams($h, $bauds) Local $dcb = DllStructCreate("DWORD DCBlength; DWORD BaudRate;DWORD fBinary;DWORD fParity;DWORD fOutxCtsFlow;DWORD fOutxDsrFlow;DWORD fDtrControl;" & _ "DWORD fDsrSensitivity;DWORD fTXContinueOnXoff;DWORD fOutX;DWORD fInX;DWORD fErrorChar;DWORD fNull;DWORD fRtsControl;" & _ "DWORD fAbortOnError;DWORD fDummy2;WORD wReserved;WORD XonLim;WORD XoffLim;BYTE ByteSize;BYTE Parity;BYTE StopBits;" & _ "char XonChar;char XoffChar;char ErrorChar;char EofChar;char EvtChar;WORD wReserved1;") DllStructSetData($dcb, "DCBlength", DllStructGetSize ($dcb)) DllCall("Kernel32.dll", "bool", "GetCommState", "handle", $h, "ptr", DllStructGetPtr($dcb)) DllStructSetData($dcb, "BaudRate", $bauds) DllStructSetData($dcb, "ByteSize", 8) DllStructSetData($dcb, "Parity", 0) ; pas de parité DllStructSetData($dcb, "StopBits", 0) ; 1 bit de stop DllCall("Kernel32.dll", "bool", "SetCommState", "handle", $h, "ptr", DllStructGetPtr($dcb)) $dcb = 0 SetReadTimeout($h) EndFunc Func OpenCom($port, $bauds) Local $h = DllCall("Kernel32.dll", "handle", "CreateFile", "str", $port, "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) If $h[0] <> $INVALID_HANDLE_VALUE Then SetComParams($h[0], $bauds) EndIf return $h[0] EndFunc Func CloseCom($h) _WinAPI_CloseHandle($h) EndFunc Func ReadComChar($h) Local $buff = DllStructCreate("char c") Local $bytesRead _WinAPI_ReadFile($h, DllStructGetPtr($buff), 1, $bytesRead); $c = DllStructGetData($buff, "c") $buff = 0 If $bytesRead = 1 Then Return $c Else Return False EndIf EndFunc Func ReadComString($h, $separator = @CR) Local $str While True Local $c $c = ReadComChar($h) If $c Then If $c <> $separator Then $str &= $c Else Return $str EndIf EndIf WEnd EndFunc