[fpc-pascal] Access to RS232 ports with fpc

Marc Santhoff M.Santhoff at web.de
Mon Nov 2 19:55:37 CET 2009

Am Montag, den 02.11.2009, 15:20 +0100 schrieb Holger Bruns:

> I am new to linux and wrote only small things for MS-DOS before. As far 
> as I could read on the internet, fpc uses ioperm and iopl, and I will 
> try to use iopl to get access to the ports I want to use. The problem is 
> indeed the rights management, as far as I can read on several web pages, 
> which addresses my technical problem. The rights management cannot be 
> ruled out by an application like I intend to write. But I can deal with 
> it, I hope so far.

The best things you can do are to:

- Search the web for a freely available document called:
  "Serial Programming Guide for POSIX Oaperating Systems"

This document shows you how to set up serial ports on Linux, the most
important part when you cann open the port but not send or receive
anything meaningful.

- Use the device the system gives you. ANything else is aking for
trouble (IMHO).

If you can access and use the hardware port in question at all, maybe
connecting a mouse or something, it will work in your fpc programs.

> My posts to this list are not written against any person. My problem is 
> not an interpersonal problem, but a technical question, and I try to 
> find a good answer to it.

Some more examples (shortened from real code for showing the principle):

sDefaultPort: string = '/dev/cuaa0'; { this would be /dev/ttyS2 for you }

fPort = sDefaultPort;

{ Init }
fCom := SerOpen(fPort);
if (fCom > -1) then begin
  SerSetParams(fCom, 1200, 7, NoneParity, 2, []);
end else begin
  writeln('Metex: failed to open port');
  n := GetLastOSError;

{ Reading }
  inbuf: array[0..14] of char;
  fillchar(inbuf, sizeof(inbuf), #0);
  res := SerRead(fCom, inbuf[0], 14); { last figure would be 1000 for you, bytes to read }
  if (res < 0) then
    { signal error }

That way I use serial ports successfully on FreeBSD and Windows.

Marc Santhoff <M.Santhoff at web.de>

More information about the fpc-pascal mailing list