program Drawer;

{$APPTYPE CONSOLE}

uses
  Classes,
  Windows,
  Messages,
  SyncObjs,
  SysUtils,
  DateUtils;

var
  i: Integer;
  str: string;
  hLptFile: THandle;
  commtimeouts:   TCommTimeouts;
  dwNumberOfBytesWritten: DWORD;
begin
  { TODO -oUser -cConsole Main : Insert code here }
  if ParamCount >= 1 then
  begin
    str := Trim(UpperCase(ParamStr(1)));
    if Copy(str,1,3) <> 'COM' then
    begin
      WriteLn('Syntax: ' + ExtractFileName(ParamStr(0))+' COMx');
      Exit;
    end;
    hLptFile := CreateFile( Pchar(str),
                          GENERIC_WRITE,
                          0, {not shared}
                          nil, {no security ??}
                          OPEN_EXISTING,
                          FILE_ATTRIBUTE_NORMAL,
                          0 {template} );
    if hLptFile = INVALID_HANDLE_VALUE then
    begin
       WriteLn( 'Error opening ' + str);
       Exit;
    end;
    GetCommTimeouts( hLptFile, commtimeouts );
    commtimeouts.ReadIntervalTimeout         := 1;
    commtimeouts.ReadTotalTimeoutMultiplier  := 1;
    commtimeouts.ReadTotalTimeoutConstant    := 1;
    commtimeouts.WriteTotalTimeoutMultiplier := 1;
    commtimeouts.WriteTotalTimeoutConstant   := 1;
    SetCommTimeouts( hLptFile, commtimeouts );
    WriteFile( hLptFile,'12345678901234',10, dwNumberOfBytesWritten,nil);
    CloseHandle(hLptFile);
  end
  else
  begin
    for i:=3 to 8 do
    begin
      str := Format('COM%d',[i]);
      hLptFile := CreateFile( Pchar(str),
                            GENERIC_WRITE,
                            0, {not shared}
                            nil, {no security ??}
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            0 {template} );
      if hLptFile = INVALID_HANDLE_VALUE then
      begin
         WriteLn( 'Error opening ' + str);
         continue;
      end;
      GetCommTimeouts( hLptFile, commtimeouts );
      commtimeouts.ReadIntervalTimeout         := 1;
      commtimeouts.ReadTotalTimeoutMultiplier  := 1;
      commtimeouts.ReadTotalTimeoutConstant    := 1;
      commtimeouts.WriteTotalTimeoutMultiplier := 1;
      commtimeouts.WriteTotalTimeoutConstant   := 1;
      SetCommTimeouts( hLptFile, commtimeouts );
      WriteFile( hLptFile,'12345678901234',10, dwNumberOfBytesWritten,nil);
      CloseHandle(hLptFile);
      break;
    end;
  end;
end.
