Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-03

problem setting up serial port

10 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marc Santhoff

Hi,

I'm trying to use a serial port at 1200baud, 7N2 with this code sequence:

<snip>
fCom := fpOpen('/dev/cuaa0', O_RDWR OR O_NOCTTY );//OR O_NONBLOCK);
if (fCom < 0) then begin
	writeln(stderr, 'Couldn''t open serial port.');
	halt(1);
end;
r := 0;
fillchar(tios, sizeof(tios), #0);
cfsetispeed(tios, B1200);
cfsetospeed(tios, 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!');
	fpClose(fCom);
	halt(1);
end;
</snip>

and all I get when running it is: 'tcsetattr failed!'. The rights are okay and running as root does make no difference. The hardware is working, connections are checked okay ... another program doing mostly the same is working as it should.

Maybe I have looked at it for too long, where is the problem?

TIA,
Marc

Sebastian Günther
to Marc Santhoff
Maybe I have looked at it for too long, where is the problem?

just by the way, there is a unit called 'serial' in Free Pascal. Perhaps you might want to use this one, or at least have a look at the source code...

Regards,
Sebastian

Michael Van Canneyt
to Sebastian Günther
Sebastian Günther wrote 6 lines
Marc Santhoff schrieb: > > Maybe I have looked at it for too long, where is the problem? just by the way, there is a unit called 'serial' in Free Pascal. Perhaps you might want to use this one, or at least have a look at the source code...

Better yet, the german Magazine Toolbox distributes a unit by Rainer Reush, with a 'TSerial' component.
AFAIK: Cross-platform and cross-delphi-lazarus...

Michael.

Marc Santhoff
to Sebastian Günther: just by the way, there is a unit called 'serial' in Free Pascal. Perhaps you might want…
Sebastian Günther wrote 7 lines
Marc Santhoff schrieb: > > Maybe I have looked at it for too long, where is the problem? just by the way, there is a unit called 'serial' in Free Pascal. Perhaps you might want to use this one, or at least have a look at the source code...

Yes, I know and used it at first, but since it does not give result codes back I had to rip it up to create a test program for detecting why it doesn't work.

Thank you anyways,
Marc

Marc Santhoff
to Michael Van Canneyt: Better yet, the german Magazine Toolbox distributes a unit by Rainer Reush, with a …
Michael Van Canneyt wrote 13 lines
On Fri, 10 Mar 2006, Sebastian Günther wrote: > Marc Santhoff schrieb: >> >> Maybe I have looked at it for too long, where is the problem? > > just by the way, there is a unit called 'serial' in Free Pascal. Perhaps you > might want to use this one, or at least have a look at the source code... Better yet, the german Magazine Toolbox distributes a unit by Rainer Reush, with a 'TSerial' component. AFAIK: Cross-platform and cross-delphi-lazarus...

Yes, but it is not open source, I'd need something under BSD or LGPL style licence.

And my code should work ... ;)

Marc

Marc Santhoff
to Marc Santhoff: I'm trying to use a serial port at 1200baud, 7N2 with this code sequence: <snip> fCom :=…

Hi again,

from trying and decoding error messages I know the call to tcsetattr fails with an invalid input. The problem is caused by trying to set the field termios.c_ospeed, if left out the call is successfull. So I went the way trough the sources and have found something.

On FreeBSD 4.11 termios is defined like this:

/usr/include/termios.h -> sys/termios.h

struct termios {
tcflag_t c_iflag; /* input flags */
tcflag_t c_oflag; /* output flags */
tcflag_t c_cflag; /* control flags */
tcflag_t c_lflag; /* local flags */
cc_t c_cc[NCCS]; /* control chars */
speed_t c_ispeed; /* input speed */
speed_t c_ospeed; /* output speed */
};

The fpc source file "" defines it as:

<fpc-2.0.2>/share/src/fpc-2.0.2/rtl/freebsd/termios.inc

type
  Termios = packed record
    c_iflag,
    c_oflag,
    c_cflag,
    c_lflag  : longint;
    c_line   : char;
    c_cc     : array[0..NCCS-1] of byte;
   {$IFDEF BSD}
    c_ispeed,
    c_ospeed : longint;
   {$endif}
  end;
  TTermios=Termios;

Note the line in pascal saying "c_line: char" which is missing in the systems declaration. This is an error, insn't it?

Marc

Marco van de Voort
to Marc Santhoff
Note the line in pascal saying "c_line: char" which is missing in the systems declaration. This is an error, insn't it?

I think so. Can you verify that it works with that line commented?

Marc Santhoff
to Marco van de Voort

Resending, was blocked:

Marco van de Voort wrote
> Note the line in pascal saying "c_line: char" which is missing in the > systems declaration. This is an error, insn't it? I think so. Can you verify that it works with that line commented?

It looks like it would ... ;).

What I've done:

- changed termios.inc (comment out the line in question)
- ran gmake in that dir
- copied termio.o and termio.ppu to the same dir with "lib" instead of "src" in it's name, because fpc -vu told me to put it there
- compiled my program with setting ospeed in two ways:

  tios.c_ospeed := B1200;
  cfsetospeed(tios, B1200);

In both cases the call to tcsetattr worked without error.

I cannot tell if communication on the serial port is working now, my program seems to have some other problems, but this part seems to be okay.

HTH and thanks,
Marc