Unitportdetect.pas V1.1 [ 09-09-2016 ]
(best version, bugfix thanks to a user preventing underflows in case of zero resource counts & possible overflows)
Unitportdetect.pas V1.0 [ 17-06-2007 ]
(obsolete, same file as used by Lightning V0.80..V0.86)
(obsolete, same file as used by Lightning ≤ V0.70)
Unitportdetect.pas is a Borland Delphi source code unit that lists parallel and serial port hardware resources in an easy and accurate way. The unit is used by various Lightning modules
: inSerialMouse, inParallelPort, outParallelPort and outParallelPortMultiplex.
The unit is available for the public domain, with no copying limitations residing on the source code. If you develop an application using Borland Delphi that needs to enumerate parallel or serial port resources, this unit can be useful. The code was designed for Borland Delphi 7, but should work with any Delphi version that supports dynamic arrays (one user tested it with Embarcadero Delphi 2007). It detects the available LPT and COM ports in the system using various approaches. In most cases it will also detect the I/O address, IRQ number and DMA channel number of the ports.
For COM ports, the focus of the code is on detecting the associated hardware resources. If you don't need such information and you just need to list the COM port names, there may be better alternatives available elsewhere on the internet. The archive contains a ready to run demo application. Try it to see if the unit suits your needs.
The unit uses various approaches for detecting the ports:
- Windows 10 64+32 bit/7/Vista/XP/2000/98:
using the Windows Driver Model with SetupDi* and CM_* API calls.
- Windows NT: using the registry devicemap and resourcemap.
- Windows 95: using PortEnum and Toolhelp32* calls.
Should one method fail, another method will be tried automatically. No administrator token is required, not even in Windows 10/7/Vista.
The actual detection is as easy including "unitportdetect" in the uses clause, declaring an "array of TDetectedPort" of the desired size and calling getCOMPorts() or getLPTPorts() with the array as parameter. The array will be emptied and each slot will contain information about a serial or parallel port. The functions return True if any ports were found, False if no ports were found.
The maximum number of detectable ports is up to your application with only small memory increase as the array slot count grows. Only with an array with 256 elements, the highest COM port COM256 will be detectable, like in the demonstration application. In Lightning
the limits are set to a maximum of 16 serial ports and 8 parallel ports.
TDetectedPort = record
function getCOMPorts(var ports: array of TDetectedPort): Boolean;
function getLPTPorts(var ports: array of TDetectedPort): Boolean;
TDetectedPort record explanation:
- The "friendlyName" field can be used to determine if a port was found at the port index number.
- If the "portLength" field is greater than 0, an I/O address was found and stored in the "portStart" field.
- If "irq" is True, an IRQ level was found and stored in the "irqLevel" field.
- If "dma" is True, a DMA channel number was found and stored in the "dmaChannel" field.