IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Programmation sur port COM


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5
    Par défaut Programmation sur port COM
    Bonjour à tous!

    Je suis actuellement chargé d'écrire une petite application qui me permet d'aller écrire directement sur un port COM spécifique.

    J'ai pas mal cherché (ca fait quasi une semaine maintenant) mais je n'arrive pas à trouver une solution à mon problème.

    Voici la situation, un bout de cote de mon programme main:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    char sCmd[254];
       char sResult[254];
       printf("Type q to quit.\n\n");
     
       if(OpenRS232Port(&firmware->RFB_PATH) <= 0) return -1;
       while (1) {
          int iSpot;
     
          printf("?:");
          scanf("%s", sCmd);
     
          if(sCmd[0] == 'q' || sCmd[0] == 'Q') break;
     
          iSpot = strlen(sCmd);
          sCmd[iSpot] = 0x0d; /* stick a <CR> after the command */
          sCmd[iSpot] = 0x00; /* terminate the string properly */
     
          if(WriteRS232Port(sCmd) < 0) {return -1;}
          sleep(5);
     
          if (ReadRS232Port(sResult,254) > 0) {
             printf("?>Response is %s\n", sResult);
          }
       }
     
       CloseRS232Port();
    Ce code permet donc d'avoir un prompt de commande avec "?:" en attendant que l'utilisateur introduise sa commande.

    Voici maintenant le code du fichier RS232.c :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    static int fd = 0;
    static struct termios oldtio, newtio;
     
    /***************************************************************************
     * OpenRS232Port                                                           *
     * Parameters:                                                             *
     *    - *path: the path to the RS232 port to open                          *
     * Return: fd if the port has been opened correctly                        *
     ***************************************************************************/
    int OpenRS232Port(char* path) {
     
       /* Making sure the port is closed */
       CloseRS232Port(fd);
     
       /* Opening the port */
       if((fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) {
          return -1;
       }
       else {
          /* Making the File Descriptor Asynchronous */
          fcntl(fd, F_SETFL, 0);
     
          /* Saving the current port settings */
          tcgetattr(fd, &oldtio);
     
          /* Setting new port settings */
          newtio.c_cflag = B38400 | CS8 | CLOCAL | CREAD;
          newtio.c_iflag = IGNPAR | ICRNL;
          newtio.c_oflag = 0;
          newtio.c_lflag = 0;
          newtio.c_cc[VMIN] = 0;
          newtio.c_cc[VTIME] = 1;
     
          /* Loading new port settings */
          cfsetspeed(&newtio, B38400);
          tcflush(fd, TCIOFLUSH);
          tcsetattr(fd, TCSANOW, &newtio);
       }
     
       return fd;
    }
     
    /***************************************************************************
     * WriteRS232Port                                                          *
     * Parameters:                                                             *
     *    - *output: the output to write on the RS232 port                     *
     * Return: the number of characters written                                *
     ***************************************************************************/
    int WriteRS232Port(char* output) {
     
       int iOut;
     
       /* Checking if the port is correctly opened */
       if(fd < 1) {
          return -1;
       }
     
       /* Flushing output from the port */
       /*tcflush(fd, TCOFLUSH);*/
     
       /* Writting the output on the RS232 port */
       if((iOut = write(fd, output, strlen(output))) < 0) {
          return -1;
       }
     
       return iOut;
    }
     
    /***************************************************************************
     * ReadRS232Port                                                           *
     * Parameters:                                                             *
     *    - *response: the response to read from the RS232 port                *
     * Return: the number of characters read                                   *
     ***************************************************************************/
    int ReadRS232Port(char* response) {
     
       int iIn;
     
       /* Checking if the port is correctly opened */
       if(fd < 1) {
          return -1;
       }
     
       /* Reading the intput from the RS232 port */
       if((iIn = read(fd, response, 254)) < 0) {
          if(errno == EAGAIN) {
             return 0;
          }
          return -1;
       }
       else {
          response[iIn] = '\0';
       }
     
       /* Flushing intput from the port */
       /*tcflush(fd, TCIFLUSH);*/
     
       return iIn;
    }
     
    /***************************************************************************
     * CloseRS232Port                                                          *
     * Parameters: None                                                        *
     * Return: None                                                            *
     ***************************************************************************/
    void CloseRS232Port() {
     
       if(fd > 0) {
          /* Restoring the old port settings */
          tcsetattr(fd,TCSANOW,&oldtio);
     
          /* Closing the port */
          close(fd);
       }
    }
    Voilà, j'espère que ce code n'est pas trop long ni trop lourd à lire.

    Le dispositif avec lequel je communique possède SerialNet installé dessus, donc c'est un interpreteur de commande AT.

    Le but de mon programme est que lorsque j'arrive à ce prompt, j'entre une commande (la commande AT, ou ATX, ou AT&F... n'importe) et que je réceptionne la réponse...

    Or actuellement voilà ce que mon programme propose:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    ?:AT
    ****Response is AT
    ?:ATX
    ****Response is ATX
    ?:AT&F
    ****Response is AT&F
    ?:
    Les réponses normalement souhaitées auraient été OK, OK et OK ou si j'entre une commande erronée, je devrais recevoir ERROR...

    Si des âmes charitables sont motivées pour m'aider, je leur serai très reconnaissant!

    Merci d'avance d'avoir lu mon problème!

    Bruno.

  2. #2
    Modérateur
    Avatar de Obsidian
    Homme Profil pro
    Chercheur d'emploi
    Inscrit en
    Septembre 2007
    Messages
    7 486
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Chercheur d'emploi
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2007
    Messages : 7 486
    Par défaut
    Citation Envoyé par br0uuu Voir le message
    Je suis actuellement chargé d'écrire une petite application qui me permet d'aller écrire directement sur un port COM spécifique.
    Sur quelle plateforme ? Les ports COM, c'est la dénomination que l'on donne au ports série RS-232 sous DOS et Windows, mais tcsetattr, c'est plutôt Unix (POSIX en tout cas).

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5
    Par défaut
    Citation Envoyé par Obsidian Voir le message
    Sur quelle plateforme ? Les ports COM, c'est la dénomination que l'on donne au ports série RS-232 sous DOS et Windows, mais tcsetattr, c'est plutôt Unix (POSIX en tout cas).
    Oui pardon j'ai oublié que je travaillais sous Linux (Ubuntu) et j'ai mis port COM pour que ca soit plus parlant, il s'agit bien sur d'un port RS232.

  4. #4
    Membre Expert
    Homme Profil pro
    Dév. Java & C#
    Inscrit en
    Octobre 2002
    Messages
    1 414
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Dév. Java & C#
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2002
    Messages : 1 414
    Par défaut
    Bonjour,

    Ton dispositif fait-il de l'écho? A contrôler à l'aide d'un programme tiers (Ex: miniterm).

    Ton code contient quelques "petits soucis" avec la gestion de l'état du port fermé:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
     
    /* -FMa- CLOSED_PORT indique un port fermé */
    #define CLOSED_PORT 0
     
    static int fd = CLOSED_PORT;
    static struct termios oldtio, newtio;
     
    /***************************************************************************
     * OpenRS232Port                                                           *
     * Parameters:                                                             *
     *    - *path: the path to the RS232 port to open                          *
     * Return: fd if the port has been opened correctly                        *
     ***************************************************************************/
    int OpenRS232Port(char* path) {
     
       /* Making sure the port is closed */
       CloseRS232Port(fd);
     
       /* Opening the port */
       if((fd = open(path, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) {
          /* -FMA- */
          fd = CLOSED_PORT;
          return -1;
       }
       else {
          /* Making the File Descriptor Asynchronous */
          fcntl(fd, F_SETFL, 0);
     
          /* Saving the current port settings */
          tcgetattr(fd, &oldtio);
     
          /* -FMa- On initialise la structure à zéro */
          bzero(&newtio, sizeof(newtio));
     
          /* Setting new port settings */
          newtio.c_cflag = B38400 | CS8 | CLOCAL | CREAD;
          newtio.c_iflag = IGNPAR | ICRNL;
          newtio.c_oflag = 0;
          /*  -FMa- Voir PS */
          newtio.c_lflag = 0;
          newtio.c_cc[VMIN] = 0;
          newtio.c_cc[VTIME] = 1;
     
          /* Loading new port settings */
          cfsetspeed(&newtio, B38400);
          tcflush(fd, TCIOFLUSH);
          tcsetattr(fd, TCSANOW, &newtio);
       }
     
       return fd;
    }
     
    /***************************************************************************
     * WriteRS232Port                                                          *
     * Parameters:                                                             *
     *    - *output: the output to write on the RS232 port                     *
     * Return: the number of characters written                                *
     ***************************************************************************/
    int WriteRS232Port(char* output) {
     
       int iOut;
     
       /* Checking if the port is correctly opened */
       if(fd < 1) {
          return -1;
       }
     
       /* Flushing output from the port */
       /*tcflush(fd, TCOFLUSH);*/
     
       /* Writting the output on the RS232 port */
       if((iOut = write(fd, output, strlen(output))) < 0) {
          return -1;
       }
     
       return iOut;
    }
     
    /***************************************************************************
     * ReadRS232Port                                                           *
     * Parameters:                                                             *
     *    - *response: the response to read from the RS232 port                *
     * Return: the number of characters read                                   *
     ***************************************************************************/
    int ReadRS232Port(char* response) {
     
       int iIn;
     
       /* Checking if the port is correctly opened */
       if(fd < 1) {
          return -1;
       }
     
       /* Reading the intput from the RS232 port */
       if((iIn = read(fd, response, 254)) < 0) {
          if(errno == EAGAIN) {
             return 0;
          }
          return -1;
       }
       else {
          response[iIn] = '\0';
       }
     
       /* Flushing intput from the port */
       /*tcflush(fd, TCIFLUSH);*/
     
       return iIn;
    }
     
    /***************************************************************************
     * CloseRS232Port                                                          *
     * Parameters: None                                                        *
     * Return: None                                                            *
     ***************************************************************************/
    void CloseRS232Port() {
     
       if(fd > 0) {
          /* Restoring the old port settings */
          tcsetattr(fd,TCSANOW,&oldtio);
     
          /* Closing the port */
          close(fd);
       }
       /* -FMA- On force l'indication que le port est fermé */
       fd = CLOSED_PORT;
    }
    PS: N'aurais-tu pas oublié un "Ch'ti canon"?
    Dans la fonction OpenRS232Port,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    newtio.c_lflag = ICANON;

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Janvier 2009
    Messages : 5
    Par défaut
    Je vais un peu simplifier mon code pour ceux qui veulent m'aider:

    J'ai un bout de code dans mon main:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    if(OpenRS232Port(&firmware->RFB_PATH) <= 0) return -1;
       while (1) {
          char sCmd[254] = "Bouh";
          char sResult[254] = "Bouh";
          int iSpot;
     
          printf("?:");
          scanf("%254s", sCmd);
     
          if(sCmd[0] == 'q' || sCmd[0] == 'Q') break;
     
          iSpot = strlen(sCmd);
          sCmd[iSpot] = 0x0d;
          sCmd[iSpot+1] = 0x00;
     
          if(WriteRS232Port(sCmd) < 0) {
             return -1;
          }
          do{
             sleep(1);
          }
          while(ReadRS232Port(sResult) <= 0);
          printf("?>[%s]\n", sResult);
       }
     
       CloseRS232Port();
    Ensuite voici la fonction Write et la fonction Read:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    int WriteRS232Port(char* output) {
     
       int iOut;
     
       /* Checking if the port is correctly opened */
       if(fd < 1) {
          return -1;
       }
     
       /* Writting the output on the RS232 port */
       if((iOut = write(fd, output, strlen(output))) < 0) {
          return -1;
       }
     
       /* Flushing output from the port */
       tcflush(fd, TCIOFLUSH);
     
       return iOut;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    int ReadRS232Port(char* response) {
     
       int iIn;
     
       /* Checking if the port is correctly opened */
       if(fd < 1) {
          return -1;
       }
     
       /* Reading the intput from the RS232 port */
       if((iIn = read(fd, response, 254)) < 0) {
          if(errno == EAGAIN) {
             return 0;
          }
          return -1;
       }
       else {
          response[iIn] = '\0';
       }
     
       if(strncmp(response, "OK", 2) == 0) {
          printf(">>%s\n", &response);
       }
     
     
       return iIn;
    }
    J'y suis presque arrivé mais voilà, j'ai l'impression que la commande interpretée est récupérée avant la réception de la réponse, voici un exemple de mon screen output:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    ?:AT
    ?>[AT
     
     
    OK
     
    ]
    ?:ATX
    ?>[ATX
     
     
    OK
     
    ]
    ?:AT+GSN?
    ?>[AT+GSN?
     
     
    +GSN:000100000E33D097
     
    OK
     
    ]
    ?:
    Si quelqu'un trouve une solution ou a une piste pour m'aider, ca serait très sympa ;-)

Discussions similaires

  1. comment détecter l'absence de connexion sur port COM ?
    Par nicoboud2 dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 12/04/2006, 14h41
  2. Problème de lecture sur port COM
    Par Marthym dans le forum MFC
    Réponses: 10
    Dernier message: 11/04/2006, 15h36
  3. Capteur InfraRouge sur port com
    Par PoOky dans le forum Composants VCL
    Réponses: 12
    Dernier message: 12/01/2006, 15h31
  4. Write puis read sur port com
    Par chourmo dans le forum API, COM et SDKs
    Réponses: 34
    Dernier message: 21/06/2005, 17h36
  5. Problème de reception sur Port COM
    Par Revan777 dans le forum C
    Réponses: 9
    Dernier message: 19/04/2005, 21h55

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo