Delphi - how to prevent application from freezing when writing to LPT port

I have an application that writes commands to some specialized printers directly to the LPT1 port. The code is as follows:

AssignFile(t, 'LPT1');
Rewrite(t);

Write(t,#27 + '@');          // initialize
Sleep(50);                   // avoid buffer fill
Write(t,#27#32 + Chr(0));    // set default font
...

The problem is that when the printer is not connected to the port, the first Write command does nothing, it just hangs and the entire stream is blocked.

Is there a way to determine the timeout for these instructions, or can you recommend another library that could do the job? It would be great if it had a Write function, similar to the function in Delphi, because the amount of code using this approach is very large, and it would be very difficult to change all this.

+4
source share
2 answers

SetCommTimeouts - . TextFile, TTextRec Handle:

var
  CommTimeouts: TCommTimeouts;

CommTimeouts.WriteTotalTimeoutConstant := DesiredTimeout;
Win32Check(SetCommTimeouts(TTextRec(t).Handle, CommTimeouts));

GetCommTimeouts, , .

+5

. -, - , , .

CancelSynchronousIo, - . Write. , .

Windows Vista , , , Windows XP. CreateFile, -. CancelIo CancelIoEx. Write, Delphi .

+2

All Articles