problem setting up serial port

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

problem setting up serial port

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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: problem setting up serial port

Sebastian Günther
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...


Regards,
Sebastian
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: problem setting up serial port

Michael Van Canneyt


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...

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: problem setting up serial port

Marc Santhoff
In reply to this post by Sebastian Günther
Am Freitag, den 10.03.2006, 16:46 +0100 schrieb Sebastian Günther:
> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: problem setting up serial port

Marc Santhoff
In reply to this post by Michael Van Canneyt
Am Freitag, den 10.03.2006, 17:06 +0100 schrieb Michael Van Canneyt:

>
> 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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: problem setting up serial port

Marc Santhoff
In reply to this post by Marc Santhoff
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


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: problem setting up serial port

Marco van de Voort
 
> 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?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: problem setting up serial port

Marc Santhoff
Resending, was blocked:

Am Samstag, den 11.03.2006, 12:26 +0100 schrieb Marco van de Voort:
>  > 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

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: problem setting up serial port

Marc Santhoff
In reply to this post by Marco van de Voort
Resending second time, was blocked again:

Am Samstag, den 11.03.2006, 12:26 +0100 schrieb Marco van de Voort:
>  > 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?

YES! Communication established!

Puh, I thought I was going crazy on this one...

Regards,
Marc


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: problem setting up serial port

Felipe Monteiro de Carvalho
Hello,

Would you guys please add a serial communication tutorial in this wiki page:

http://wiki.lazarus.freepascal.org/index.php/Hardware_Access#Serial_Communication

I wrote the parallel part of the tutorial, but I know nothing about
serial comm and I would be very thankful =)

thanks,
--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal