Re: Reading from ports with Delphi

Posted by Maxwell on June 16, 2005

In Reply to: Reading input from ports with Delphi posted by zmo on June 09, 2005

: I want to know how can read datas from ports (LPT, ...). It's for data acquisition. I alread think for this in (LPT ports), but i don't know it is right or not. It's in the following:

: var port:array[0..65536]of byte;
: bits:byte;

: const data=$378;
: status=data+1;
: control=data+2;
: begin
: port[data]:=bits; {output data}
: bits:=port[status]; {input data}

With the arrival of the Windows NT, Windows 2000 and Windows XP, accessing a port became complicated.

There are 2 modes, kernel mode and user mode:

Kernel mode has access to everything, this mode is used by the kernel and the drivers installed in the system.
For security reasons, in user mode, programs can't access the hardware. Delphi applications are always executed in user mode.

Solution: use a driver that runs in kernel mode, and access this driver in user mode. Luckily, somebody has already developed such a DLL for the ports, have a look at:
www.logix4u.net/inpout32.htm

You have to link the functions of the DLL to your application. Add these two lines of code to the implementation part:

function Inp32(wAddr: word): byte; stdcall; external 'inpout32.dll'; 
function Out32(wAddr: word; bOut: byte): byte; stdcall; external 'inpout32.dll';
Later, use the functions, for example:
  { Send 65 to port 378 } 
  Out32($378,65); 
  { Read port 378 } 
  ShowMessage('Port $378:'+ IntToStr(Inp32($378))); 


 

Delphi Forum :: Tutorials :: Source code :: Tips