Linux serial interface

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

Linux serial interface

Rainer Stratmann
How to open correct the serial interface ('/dev/ttyS0')
In my program it works for writing, but reading is not possible at the moment.
Has someone an idea or example code?
Rainer

With USB-to serial Converter the program works (!), but not with the standard
9P serial connector on the mainboard (tested more computers).
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Linux serial interface

michael.vancanneyt


On Tue, 26 Dec 2006, Rainer Stratmann wrote:

> How to open correct the serial interface ('/dev/ttyS0')

As far as I know, this is a terminal, so the terminal emulator will mess up
the IO. You should open the raw device.

> In my program it works for writing, but reading is not possible at the moment.
> Has someone an idea or example code?

If you have access to Toolbox, it comes with a TSerial component. It should
work with FPC and Linux.

I also think there is a serial unit distributed with FPC, but I am sure
about it's inner workings.

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

Re: Linux serial interface

Rainer Stratmann
Am Dienstag, 26. Dezember 2006 14:07 schrieb Michael Van Canneyt:
> On Tue, 26 Dec 2006, Rainer Stratmann wrote:
> > How to open correct the serial interface ('/dev/ttyS0')
>
> As far as I know, this is a terminal, so the terminal emulator will mess up
> the IO. You should open the raw device.

Isn't tty1 (without S) a terminal?

> > In my program it works for writing, but reading is not possible at the
> > moment. Has someone an idea or example code?
>
> If you have access to Toolbox, it comes with a TSerial component. It should
> work with FPC and Linux.
>
> I also think there is a serial unit distributed with FPC, but I am sure
> about it's inner workings.

I don't know to access the tools.
It should not be that difficult, because the same code works with USB-serial
converters ('/dev/ttyUSB0') correctly.
Only reading is not possible with the built in serial ports at the PC.
The connected Hardware indicates the receive of the writing data correctly.

Rainer

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

Re: Linux serial interface

Bugzilla from daniel.mantione@freepascal.org
In reply to this post by Rainer Stratmann


Op Tue, 26 Dec 2006, schreef Rainer Stratmann:

> How to open correct the serial interface ('/dev/ttyS0')
> In my program it works for writing, but reading is not possible at the moment.
> Has someone an idea or example code?

What goes wrong while reading? Does strace give any clues?

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

Re: Linux serial interface

Johann Glaser
In reply to this post by Rainer Stratmann
Hi!

Am Dienstag, den 26.12.2006, 15:38 +0100 schrieb Rainer Stratmann:
> Am Dienstag, 26. Dezember 2006 14:07 schrieb Michael Van Canneyt:
> > On Tue, 26 Dec 2006, Rainer Stratmann wrote:
> > > How to open correct the serial interface ('/dev/ttyS0')
> >
> > As far as I know, this is a terminal, so the terminal emulator will mess up
> > the IO. You should open the raw device.
>
> Isn't tty1 (without S) a terminal?

The ttySx devices also do a lot of terminal emulation stuff internally
(see e.g. stty(1) for how many things are possible, in your program you
have to do these with ioctl()).

Especially I have made the observation that flow control often is in
your way when you "just want to transmit data". Switch off flow control
completely (neither hardware nor software/XON/XOFF). Probably your
USB-serial converter has different default values for this and this is
why your program works with it.

Bye
  Hansi


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

Re: Linux serial interface

Bugzilla from daniel.mantione@freepascal.org
In reply to this post by michael.vancanneyt


Op Tue, 26 Dec 2006, schreef Michael Van Canneyt:

>
>
> On Tue, 26 Dec 2006, Rainer Stratmann wrote:
>
> > How to open correct the serial interface ('/dev/ttyS0')
>
> As far as I know, this is a terminal, so the terminal emulator will mess up
> the IO. You should open the raw device.

The raw decives are no longer in use, they are even no longer on my
system:

laptop:~ # ls -al /dev/cua*
/bin/ls: /dev/cua*: Onbekend bestand of map
laptop:~ #

/dev/ttyS0 is the correct device, you have to disable most of the terminal
handling through ioctls.

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

Re: Linux serial interface

Rainer Stratmann
In reply to this post by Johann Glaser
Am Dienstag, 26. Dezember 2006 14:32 schrieb Johann Glaser:

> Hi!
>
> Am Dienstag, den 26.12.2006, 15:38 +0100 schrieb Rainer Stratmann:
> > Am Dienstag, 26. Dezember 2006 14:07 schrieb Michael Van Canneyt:
> > > On Tue, 26 Dec 2006, Rainer Stratmann wrote:
> > > > How to open correct the serial interface ('/dev/ttyS0')
> > >
> > > As far as I know, this is a terminal, so the terminal emulator will
> > > mess up the IO. You should open the raw device.
> >
> > Isn't tty1 (without S) a terminal?
>
> The ttySx devices also do a lot of terminal emulation stuff internally
> (see e.g. stty(1) for how many things are possible, in your program you
> have to do these with ioctl()).
how to do that?
> Especially I have made the observation that flow control often is in
> your way when you "just want to transmit data". Switch off flow control
> completely (neither hardware nor software/XON/XOFF). Probably your
> USB-serial converter has different default values for this and this is
> why your program works with it.
do you have little example code?
thanks
Rainer
> Bye
>   Hansi
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Linux serial interface

Marc Santhoff
In reply to this post by Rainer Stratmann
Am Dienstag, den 26.12.2006, 15:10 +0100 schrieb Rainer Stratmann:
> How to open correct the serial interface ('/dev/ttyS0')
> In my program it works for writing, but reading is not possible at the moment.
> Has someone an idea or example code?

You want to have a look at the serial.pp unit coming with fpc- Its in
the rtl/unix directory.

HTH,
Marc


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

Re: Linux serial interface

John Coppens
In reply to this post by Rainer Stratmann
On Tue, 26 Dec 2006 15:10:27 +0100
Rainer Stratmann <[hidden email]> wrote:

> In my program it works for writing, but reading is not possible at the
> moment.

Did you check if (hardware) handshaking is enabled? I've found quite
regularly that one of the lines is stopping the input. If you have
connected only RX/TX/GND, this can also be the source of problem if the
peripheral expects proper level on the handshake lines.

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

Re: Linux serial interface

Rainer Stratmann
Am Dienstag, 26. Dezember 2006 19:28 schrieb John Coppens:

> On Tue, 26 Dec 2006 15:10:27 +0100
>
> Rainer Stratmann <[hidden email]> wrote:
> > In my program it works for writing, but reading is not possible at the
> > moment.
>
> Did you check if (hardware) handshaking is enabled? I've found quite
> regularly that one of the lines is stopping the input. If you have
> connected only RX/TX/GND, this can also be the source of problem if the
> peripheral expects proper level on the handshake lines.
How to do that?
The code for initialisation of the serial interface:

const
 iflagoff = BRKINT or INPCK or ISTRIP or IGNCR or INLCR or ICRNL or IUCLC or
            IXON or IXANY or IXOFF or IMAXBEL;
 iflagon  = IGNBRK or IGNPAR;
 oflagoff = OPOST or OLCUC or ONLCR or OCRNL or ONOCR or ONLRET or OFILL or
            OFDEL or NLDLY or TABDLY or BSDLY or VTDLY or FFDLY;
 oflagon  = 0;
 cflagoff = CBAUD or CSIZE or CSTOPB or PARENB or PARODD or HUPCL or CBAUDEX
or
            CIBAUD or CRTSCTS ;
 cflagon  = CREAD or CLOCAL;
 lflagoff = ISIG or ICANON or XCASE or ECHO or ECHOE or ECHOK or ECHONL or
            NOFLSH or TOSTOP or ECHOCTL or ECHOPRT or ECHOKE or IEXTEN;
 lflagon  = 0;

 linuxfile:array[serportmin..serportmax] of string =
 ('ttyS0','ttyS1','ttyUSB0','ttyUSB1','ttyUSB2');
begin

  s:='/dev/'+linuxfile[ger_port];

  ger_fd:=fdOpen(s,Open_RdWr or Open_NonBlock);

  tcgetattr(ger_fd,newtio_ger);

  memclr(@newtio_ger,sizeof(newtio_ger));

  newtio_ger.c_iflag:= newtio_ger.c_iflag and not(iflagoff) or iflagon;
  newtio_ger.c_oflag:= newtio_ger.c_oflag and not(oflagoff) or oflagon;
  newtio_ger.c_cflag:= newtio_ger.c_cflag and not(cflagoff) or cflagon;
  newtio_ger.c_lflag:= newtio_ger.c_lflag and not(lflagoff) or lflagon;

  newtio_ger.c_cflag:= newtio_ger.c_cflag or speednr[ger_baudnr] or CS8 or
CREAD or CLOCAL;


  tcsetattr(ger_fd,TCSAFLUSH,newtio_ger);

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

Re: Linux serial interface

Martin Schreiber
On Tuesday 26 December 2006 21.53, Rainer Stratmann wrote:

>
> How to do that?
> The code for initialisation of the serial interface:
>
> const
>  iflagoff = BRKINT or INPCK or ISTRIP or IGNCR or INLCR or ICRNL or IUCLC
> or IXON or IXANY or IXOFF or IMAXBEL;
>  iflagon  = IGNBRK or IGNPAR;
>  oflagoff = OPOST or OLCUC or ONLCR or OCRNL or ONOCR or ONLRET or OFILL or
>             OFDEL or NLDLY or TABDLY or BSDLY or VTDLY or FFDLY;
>  oflagon  = 0;
>  cflagoff = CBAUD or CSIZE or CSTOPB or PARENB or PARODD or HUPCL or
> CBAUDEX or
>             CIBAUD or CRTSCTS ;
>  cflagon  = CREAD or CLOCAL;
>  lflagoff = ISIG or ICANON or XCASE or ECHO or ECHOE or ECHOK or ECHONL or
>             NOFLSH or TOSTOP or ECHOCTL or ECHOPRT or ECHOKE or IEXTEN;
>  lflagon  = 0;
>
>  linuxfile:array[serportmin..serportmax] of string =
>  ('ttyS0','ttyS1','ttyUSB0','ttyUSB1','ttyUSB2');
> begin
>
>   s:='/dev/'+linuxfile[ger_port];
>
>   ger_fd:=fdOpen(s,Open_RdWr or Open_NonBlock);
>
>   tcgetattr(ger_fd,newtio_ger);
>
>   memclr(@newtio_ger,sizeof(newtio_ger));
      <<<<<----- now you have lost all values from tcgetattr.

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

Re: Linux serial interface

John Coppens
In reply to this post by Rainer Stratmann
On Tue, 26 Dec 2006 21:53:25 +0100
Rainer Stratmann <[hidden email]> wrote:

> How to do that?
> The code for initialisation of the serial interface:

Run   stty --help
with clocal you can disable all the modem control signals.

(I believe the ioctl have an equivalent)

John
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal