Re: Socket

トップ ページ

このメッセージに返信
著者: Habib Bouaziz-Viallet
日付:  
To: Frédéric
CC: guilde
題目: Re: Socket
Le 19/03/08, Frédéric<frederic.mantegazza@???> a écrit :
>

/*
 * posix termios interface
 *
 *  Compile :
 *     gcc -O2 -Wall -o trans_115k trans_115k.c
 *
 */



#define PORT "/dev/ttyS0"
/* #define PORT "/dev/USB/ttyUSB0" */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>



int
main(int argc, char **argv)
{
    int port, n;
    struct termios tty_settings, orig_settings;
    unsigned char buf[256];
    unsigned char baud_change_cmd[2] = {0x6A, 0xB4}; /* Un peu à l'arrache ...*/


    /* ouvrir le port serie en ecriture */
    port = open(PORT, O_WRONLY | O_NOCTTY);
    if (port < 0) {
        fprintf(stderr, "Unable to open \"%s\"\n", PORT);
        exit(1);
    }


    /* envoyer ~ i.e wake_up ! a la cible */
    write(port, "~", 1);
    usleep(100);


    /* passe la cible en 115k */
           write(port, baud_change_cmd, 2);
    usleep(100);


    /* et moi aussi bien sûr ...*/
    tcgetattr(port, &tty_settings);
    memcpy(&orig_settings, &tty_settings, sizeof(struct termios));
    cfsetospeed(&tty_settings, B115200);
    tcsetattr(port, 0, &tty_settings);


    /* copie les data à partir de stdin vers le port serie :) */
    while (1) {
        n = read(fileno(stdin), buf, sizeof(buf));
        if (n == 0) break;
        if (n < 0) {
            if (errno == EINTR) continue;
            fprintf(stderr, "error reading input\n");
            exit(1);
        }
        write(port, buf, n);
    }


    /* attends que TOUTES les data soient sorties ... */
    tcdrain(port);
    usleep(100);


    /* restaure les paramètres initiaux du port série */
    tcsetattr(port, 0, &orig_settings);



    return 0; /* Die gracefully :| */
}




--
M. Bouaziz-Viallet