Linuxtrent: Re: Porta seriale

  • From: Daniele Nicolodi <daniele@xxxxxxxxxx>
  • To: linuxtrent@xxxxxxxxxxxxxxxxx
  • Date: Tue, 27 Feb 2001 00:18:04 +0100

On Tue, Feb 27, 2001 at 12:09:43AM +0100, Canella Roberto wrote:
> 
> Ho scritto una routinetta in C il cui scopo e' quello di leggere da
> /dev/ttyS1 dei dati provenienti da un dispositivo seriale; il problema 
> e' che la read mi si inchioda senza restituirmi nessun carattere letto. 
> Sono certo che il dispositivo funziona perche' digitanto cat /dev/ttyS1 
> vedo il flusso di dati inviati.
> 
> Potete aiutarmi ? TIA Roby
> 
> Sunto della routine:
>  
> #include <stdio.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> 
> int main(int argc, char **argv)
> {
>     int iSerial;
>     char sDummy[BUFSIZ];

Orrore !!!! Che e` sta roba ?? Mi pare si chiami notazione ungherese
o qualcosa del genere. Lasci perdere e` solo un casino da ricordare.
Tanto il C ha i tipi e non ti buoi sbagliare (il compilatore te lo dice).

>     if ((iSerial = open("/dev/ttyS1", O_RDONLY) == -1)) {
>         perror("open");
>         exit (1);
>     }

Un open sul file di dispositivo corrispondente a un device seriale
non e` come un open su un file normale. Devi impostare un po' piu`
di flags ... leggi il "Serial Programming HOWTO".
Nel frattempo un esempietto:

#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

int s_init(char * device)
{
        int fd;
        struct termios * newtio;

        fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
        newtio->c_cflag = (B9600 | CRTSCTS | CS8 | CLOCAL | CREAD);
        newtio->c_iflag = (IGNPAR | ICRNL);
        newtio->c_lflag = (ICANON);
        newtio->c_oflag = (0);
        newtio->c_cc[VTIME] = 0;
        newtio->c_cc[VMIN] = 1;

        tcflush(fd, TCIOFLUSH);
        tcsetattr(fd, TCSANOW, newtio);
        
        return(fd);
}

Ciao
-- 
Daniele
                    --- http://www.grinta.net ---

-- 
Per iscriversi  (o disiscriversi), basta spedire un  messaggio con SOGGETTO
"subscribe" (o "unsubscribe") a mailto:linuxtrent-request@xxxxxxxxxxxxxxxxx


Other related posts: