#include ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM2", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h[0] ;$res= SetError(_WinAPI_GetLastError(), 0, $h[0]) ConsoleWrite("handle=" & $handle & @LF) ;write String func writeString($handle,$str) for $i=0 to stringlen($str) writeChar($handle,StringMid($str,$i,1)) next EndFunc ;write a single char func writeChar($handle,$c) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1, $lpNumberOfBytesWritten) ;overlapped=0 ? if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc ;read a line (string ending with @LF) but don't wait for it Func readLineNoBlock($handle) $inputLine = "" while(true) $str = readCharNoBlock($handle) $inputLine = $inputLine & $str if $str=@LF or $str="" then return $inputLine EndIf sleep(50) wend endfunc ;read a line (string ending with @LF) and wait for it Func readLineBlock($handle) $inputLine = "" while(true) $str = readCharBlock($handle) $inputLine = $inputLine & $str if $str=@LF then return $inputLine EndIf sleep(50) wend endfunc ;read a single char, and wait for it func readCharBlock($handle) $numBytes = 1 $lpBuffer = DLLStructCreate("char w") ;create struct to hold the read data $lpNumberOfBytesRead = 0 ;will contain the number of chars read while ($lpNumberOfBytesRead<=0) $res=_WinAPI_ReadFile($handle, DllStructGetPtr($lpBuffer, "w"), $numBytes, $lpNumberOfBytesRead) ;read one char if (not $res) Then ConsoleWrite("error " & _WinAPI_GetLastErrorMessage() & @LF) ConsoleWrite($lpNumberOfBytesRead & @LF) return "" EndIf if ($lpNumberOfBytesRead>0) then ;if a char is read $str = DllStructGetData($lpBuffer, "w") ;return the char return $str EndIf WEnd EndFunc ;read a single char, but don't wait for it ("" if nothing) Func readCharNoBlock($handle) $numBytes = 1 $lpBuffer = DLLStructCreate("char w") ;create struct to hold the read data $lpNumberOfBytesRead = 0 ;will contain the number of chars read $res=_WinAPI_ReadFile($handle, DllStructGetPtr($lpBuffer, "w"), $numBytes, $lpNumberOfBytesRead) ;read one char if ($res) then ;if no error if ($lpNumberOfBytesRead>0) then ;if a char is read $str = DllStructGetData($lpBuffer, "w") ;return the char return $str else return "" EndIf Else ConsoleWrite( _WinAPI_GetLastErrorMessage() & @LF) ConsoleWrite($lpNumberOfBytesRead & @LF) return "" EndIf EndFunc