[Lugge] Re: Gestione della Seriale

  • From: Roberto A.Foglietta <fogliettar@xxxxxxxxxxxxxxxxxx>
  • To: LUGGE MLIST <lugge@xxxxxxxxx>
  • Date: Thu, 19 Sep 2002 13:41:34 +0200

On Thu, 19 Sep 2002 13:08:37 +0200
"Giuseppe" <ansgius@xxxxxxxxx> wrote:
 
> Purtroppo tuttora  non ho trovato un esempio concreto.

 Perche` non lo hai cercato. 

 Per settare un device si usa ioctl e per leggere e scrivere fread/fwrite.

 Cercando con google 

 linux serial how to program

 si trova l'how-to sulla programmazione delle seriali sotto linux 

 http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/

 ed al punto 2.2 il setting delle porte

 http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/x56.html#AEN69

 Ma quello che mi spaventa di piu` e` leggere che scrivi:

> Quello che vorrei sono degli esempi
> Di codice possibilmente  in C che vengano digeriti dal compilatore
> su come controllare la seriale.

 Perche` il codice in rubrica italiana non era un esempio, dov'era il 
difficile? 
 Ne attacco un'evoluzione in fondo.

 Che vengano digeriti dal compilatore? Magari se ci fosse gia` l'esguibile 
bello pronto?
 Non so che compilatore hai. Non so se verra` digerito il codice qua sotto ma 
su un linux/gcc funziona abbastanza bene da usarlo. 


 int
serial_init_speed (int fd, int baudcode)
{
        struct termios oldtio, newtio;
        tcgetattr (fd, &oldtio);                // save current serial port 
settings 
        bzero (&newtio, sizeof (newtio));       // clear struct for new port
                                                // settings 

        /*
         * baudcode: Set bps rate. You could also use cfsetispeed and 
cfsetospeed.
         * CRTSCTS : output hardware flow control (only used if the cable has
         * all necessary lines. See sect. 7 of Serial-HOWTO)
         * CS8     : 8n1 (8bit,no parity,1 stopbit)
         * CLOCAL  : local connection, no modem contol
         * CREAD   : enable receiving characters
         */
        newtio.c_cflag = baudcode | CS8 | CLOCAL | CREAD;       // | CRTSCTS;
        /*
         * IGNPAR  : ignore parity errors
         * ICRNL   : map CR to NL (otherwise a CR input on the other computer
         * will not terminate input)
         * otherwise make device raw (no other input processing)
         */
        newtio.c_iflag = IGNPAR;        // | ICRNL | INLCR;
        /*
         * Raw output.
         */
        newtio.c_oflag = 0;

        /*
         * ICANON  : enable canonical input
         * disable all echo functionality, and don't send signals to calling 
program
         */
        newtio.c_lflag = ICANON;

        /*
         * initialize all control characters 
         * default values can be found in /usr/include/termios.h, and are given
         * in the comments, but we don't need them here
         */
        newtio.c_cc[VINTR] = 0;         // Ctrl-c 
        newtio.c_cc[VQUIT] = 0;         /* Ctrl-\  */
        newtio.c_cc[VERASE] = 0;        // del 
        newtio.c_cc[VKILL] = 0;         // @ 
        newtio.c_cc[VEOF] = 0;          // Ctrl-d (era 4)
        newtio.c_cc[VTIME] = 0;         // inter-character timer unused 
        newtio.c_cc[VMIN] = 1;          // blocking read until 1 character 
arrives 
        newtio.c_cc[VSWTC] = 0;         // '\0' 
        newtio.c_cc[VSTART] = 0;        // Ctrl-q 
        newtio.c_cc[VSTOP] = 0;         // Ctrl-s 
        newtio.c_cc[VSUSP] = 0;         // Ctrl-z 
        newtio.c_cc[VEOL] = 0;          // '\0' 
        newtio.c_cc[VREPRINT] = 0;      // Ctrl-r 
        newtio.c_cc[VDISCARD] = 0;      // Ctrl-u 
        newtio.c_cc[VWERASE] = 0;       // Ctrl-w 
        newtio.c_cc[VLNEXT] = 0;        // Ctrl-v 
        newtio.c_cc[VEOL2] = 0;         // '\0' 

        // now clean the modem line and activate the settings for the port
        tcflush (fd, TCIFLUSH);
        if (tcsetattr (fd, TCSANOW, &newtio) < 0) {
                perror ("Couldn't set term attributes");
                return -2;
        }

        return 0;
}


 

-- 
   ,__    ,_     ,___   .-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.
   ||_)   ||\    ||_   /            GEA Automotive S.R.L.             |
   || \   ||¯\   ||¯       Software Research & Development Division   |
   ¯¯  ¯° ¯¯  ¯° ¯¯  °   tel.: +39 010 65966917                       |  
   Roberto A. Foglietta  com.: mailto:fogliettar@xxxxxxxxxxxxxxxxxx   |
 \ Linux & SW Architect  per.: http://digilander.iol.it/robang        |
  `-----------------------=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-'
========----------
 
 Prima di scrivere in m-list per favore leggi il regolamento
 http://www.lugge.net/soci/manifesto.htm#list

 Archivio delle e-mail postate in lista
 //www.freelists.org/archives/lugge/

 Modifica dell'account su freelists
 //www.freelists.org/cgi-bin/lsg2.cgi 

----------========

 La sede e` aperta ogni martedi` pomeriggio 14.30-18.00
 http://www.lugge.net/soci/sede.htm
 


 

Other related posts: