Thread: Read and display signel serial mouse RS232 in C

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Read and display signel serial mouse RS232 in C

    Hello everyone,

    Sorry for my bad english!

    I am asking because my knowledge of C programming is very limited.

    I'd like to see the movement of a mouse and RS232 time in milli seconds between each increment of displacement

    It would appear that:
    the mouse RS232 protocol uses 1 start bit, 7 data bits, no parity, 1 stop bit, in a 1200 bits / sec.
    The data is sent in packets of 3 bytes, comprising the following telegram:

    byte d7 d6 d5 d4 d3 d2 d1 d0
    1 X 1 LB RB dy7 dy6 dx7 dx6
    2 X 0 dx5 dx4 dx3 dx2 dx1 dx0
    3 X 0 dy5 dy4 dy3 dy2 dy1 dy0

    LB (left button), RB (right button), is 1 when the button left, right, is pressed.
    The movements in x (dx) and y (dy) are coded on 8 bits, whose bits are low on bytes 2 and 3 and the rest in the first byte.

    After a few values, shift bytes
    (the 1 is in position 2, 2 is in position 3 and 3 is in position 1 and so on)
    And sometimes I have more than 3 per play while the mouse only sends 3

    Code:
    046 037 068 000 000 24009 usec
    007 035 064 000 000 27987 usec
    063 033 065 000 000 23992 usec
    007 001 077 000 000 24110 usec
    005 062 077 010 005 76173 usec
    072 014 039 000 005 15797 usec
    075 038 045 000 005 23897 usec
    078 062 032 000 005 24002 usec
    I wonder if there is an error in my code or if I do not use the right type of transmission (canonical, non-canonical ,...) which is described in this link:

    http://www.faqs.org/docs/Linux-HOWTO...ing-HOWTO.html

    I need your help because I do not really know what to do!
    A big thank you in advance.

    I use linux kubuntu.

    Here is my code:

    Code:
    #include <sys/time.h>  //temps
          #include <unistd.h>
    //-----------------------------------------------------------------------------
          #include <sys/types.h>
          #include <sys/stat.h>
          #include <fcntl.h>
          #include <termios.h>   // settins du port serie
          #include <stdio.h>
    //-----------------------------------------------------------------------------
          #include <stdlib.h>   //fichier
    //-----------------------------------------------------------------------------
          #define BAUDRATE B1200    // baudrate settings sont définis dans <asm/termbits.h>, qui est inclus par <termios.h>
          #define ADRESSE_PORT "/dev/ttyS0"  // numero du port
          #define _POSIX_SOURCE 1            // source conforme à POSIX
    //---------------- Timer ------------------------------------------------------
            struct timeval tv1,tv2;
            struct timezone tz;
            long long delta_temps;
    //----------------- Fichier ---------------------------------------------------
            int mesure_impulsions;  //ecriture ds le fichier
            char nom_fichier[200] ; // On initialise la variable à 0
    //-----------------------------------------------------------------------------
            volatile int STOP=0;
            int fd, c, res;
            struct termios oldtio, newtio;
            char buf[255];  // nombre max de caracteres dans le buffuer pour une lecture
            char dep_x, dep_y;
            int deplacement_x();
            int deplacement_y();
    //-----------------------------------------------------------------------------
    // Open modem device pour lecture + écriture et pas comme controleur tty car on ne veut pas etre détruit si on recoit un caractère CTRL-C.
          main()
          {
            printf("Mesure \n\nVeuillez introduire le nom du fichier: ");
            scanf("%s", &nom_fichier); // On demande d'entrer le nom
    
            fd = open(ADRESSE_PORT, O_RDWR | O_NOCTTY );// O_NOCTTY flag dit que ce progr n'est pas un "controlling terminal" pour ce port. Si on ne le spécifie pas, chaque input (such as keyboard abort signals and so forth) va affecter le process. normally a user program does not want this behavior.
            if (fd <0) {perror(ADRESSE_PORT); exit(-1); }
            tcgetattr(fd,&oldtio);    // sauve config courante
            bzero(&newtio, sizeof(newtio));  //  on initialise la structure à zéro
            newtio.c_cflag = BAUDRATE | CRTSCTS | CS7 | CLOCAL | CREAD;     // BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed.
                                                                            // CRTSCTS : contrôle de flux matériel (uniquement utilisé si le câble a les lignes nécessaires. Voir la section 7 du Serial-HOWTO).
                                                                            // CS8     : 8n1 (8bit,no parity,1 stopbit)
                                                                            // CLOCAL  : connexion locale, pas de contrôle par le modem
                                                                            // CREAD   : permet la réception des caractères
    
            newtio.c_iflag = IGNPAR;    // IGNPAR  : ignore les octets ayant une erreur de parité.
                                        // ICRNL   : transforme CR en NL (sinon un CR de l'autre côté de la ligne ne terminera pas l'entrée). sinon, utiliser l'entrée sans traitement (device en mode raw).
            newtio.c_oflag = 0;     // positionne le mode de lecture(non-canonical, no echo,...)
            newtio.c_lflag = 0;     // si ICANON  : active l'entrée en mode canonique désactive toute fonctionnalité d'echo, et n'envoit pas de signal au programme appelant.
                // 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[VTIME]    = 0;   // timer inter-caractères non utilisé
            newtio.c_cc[VMIN]     = 3;   // read bloquant jusqu'à ce que 3 caractères soient lus
    // now clean the modem line and activate the settings for the port
            tcflush(fd, TCIFLUSH);
            tcsetattr(fd,TCSANOW,&newtio);
                //  la configuration du terminal est faite, à présent on traite les entrées
    //--------------------------------------------------------------------------------
            while (STOP==0) {  // boucle jusqu'à condition de terminaison read bloque l'exécution du programme jusqu'à ce qu'un caractère de fin de ligne soit lu, même si plus de 255 caractères sont saisis.
                               //Si le nombre de caractères lus est inférieur au nombre de caractères disponibles, des read suivant retourneront les caractères restants.
                               //res sera positionné au nombre de caractères effectivement lus
            res = read(fd,buf,255);   // returns after xxx chars have been input
            buf[res]=0;
            gettimeofday(&tv2, &tz);
            delta_temps=(tv2.tv_sec-tv1.tv_sec) * 1000000L + (tv2.tv_usec-tv1.tv_usec); //calcul delta temps
    //-----------------------AFFICHAGE ECRAN--------------------------------------------------------------
            //deplacement_x();
            //deplacement_y();
            gettimeofday(&tv1, &tz);
            char octet1,octet2,octet3;
            //octet1=buf[0];
            //octet2=buf[1];
            //octet3=buf[2];
    
            printf("%03d %03d %03d %03d %03d", buf[0], buf[1], buf[2], buf[3], buf[4]);
            //printf("%02d %02d %02d", octet1, octet2, octet3);//affiche les deplacamants x et y
            printf("  %d microsec\n", delta_temps);//affiche le temps
    //-----------------------ECRITURE FICHIER------------------------------------------------------------
            mesure_impulsions = fopen(nom_fichier, "a+");//ecriture ds fichier
            fprintf(mesure_impulsions,"%03d %03d %03d %03d %03d %d usec\n", buf[0], buf[1], buf[2], buf[3], buf[4], delta_temps);
    
            fclose(mesure_impulsions);
    //---------------------------------------------------------------------------------------------------
           if (buf[0]=='z') STOP=1;
            }
            tcsetattr(fd,TCSANOW,&oldtio);  // restaure les anciens paramètres du port
          }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Hello,

    Small Up!

    I need help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and display data from files
    By Karpaty in forum C Programming
    Replies: 5
    Last Post: 11-30-2007, 02:11 PM
  2. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  3. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Need to read mouse movement in program
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-03-2002, 03:15 PM