[fpc-pascal] serial ports under Unix using Freepascal

Marc Santhoff M.Santhoff at t-online.de
Thu Oct 4 14:05:24 CEST 2007


Am Mittwoch, den 03.10.2007, 16:53 -0300 schrieb
papelhigienico at gmail.com:
> Hi!
> 
> I'm creating a component that handles serial port in windows/unix.
> Under Windows I include in uses the unit windows, that open, close,
> read, write, test if the configuration of serial port is valid (some
> set of configurations of baudrate, stop bits and parity is invalid in
> windows)  and if serial port exists. 
> 
> Under Unix I found the unit Serial, that open, close, read and write.
> How to test if serial port exist? Unix accept any set of
> configurations of  the serial port? I see fpopen that return the
> handle of serial port but I not found anything that describes error
> codes returned by this function in fpc rtl documentation. 

Normally the libc function "tcsetattr()" is used to set the ports
parameters, as you can see in the source of serial.pp at the end of
"SetSerParams()". That is the place where a check should evaluate the
return code of tcsetattr and return the OS error code if it is -1. This
is a spot where serial.pp should be improved.

FPC exhibits the systems error code by using GetOSError() and
RaiseOSError(), both in sysutils.

If you want to set up the port directly you can use something like this
with the parameters you need:

  var
	tios: termios;

BEGIN
...

	(*
	SerSetParams(fCom, 1200, 7, NoneParity, 2, []);
	*)
	
	r := 0;
	fillchar(tios, sizeof(tios), #0);

	tios.c_ispeed := B1200;
	tios.c_ospeed := B1200;
	
	tios.c_cflag := CREAD or CLOCAL or CS7 or CSTOPB;
	
	tios.c_oflag := 0;
	tios.c_iflag := IGNBRK OR IGNPAR;
	tios.c_lflag := 0;
	
	r := tcsetattr(fCom, TCSANOW, tios);
	if (r = -1) then begin
		writeln(stderr, 'tcsetattr failed!');
		writeln(stderr, 'errno is ', errno);
		fpClose(fCom);
		halt(1);
	end;

That code is from a program I made and is actually running on FreeBSD
4.11.

One other hint: FreeBSD has very good manpages, try "man sio" and "man
tcsetattr" and so on.

Have fun,
Marc





More information about the fpc-pascal mailing list