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.