Bonjour à tous,

à nouveau je requiert votre aide pour mon programme.

Que dois-je faire?

Communiquer avec un périphérique depuis le port série.
Je dois envoyer une série de byte (5 byte = une commande) et je dois récupérer la réponse (de 0 à 300 bytes)

Mon problème ?
Je ne parviens pas à configurer, dans mon programme, le port série comme il faudrait.
Il doit avoir la configuration suivante

300 baud ; Parité even ; 8 bit ; 1 bit stop ; pas de controle de flux

Dans mon code j'utilise la structure termios (d'après ce que j'ai trouvé sur le net c'est comme cela qu'il faut faire). Mais lorsque je tente une commande du type stty -F /dev/ttyUSB1 -a après avoir lancé mon code, rien n'y fait, je suis toujours en 9600 bauds (pour le reste de la configuration je ne pense pas non plus que cela change...)

Mon code actuellement ?
Voici ce que j'ai pour l'instant. Qu'est-ce qui est faux?

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
 
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>
#include "serial.h"
int fd = 2;
 
 
 
int OpenSerialPort()
{
 
   //ouverture port serie en mode non-bloquant (read fait un return imédiat)
      fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY );
      if (fd < 0)
      {
         perror(DEVICE);
         exit(-1);
      }
    int rate;
    rate= B300;
 
    tcgetattr(fd,&oldtio); // sauvegarde de la configuration courante
 
    bzero(&newtio, sizeof(newtio));
 
    newtio.c_cflag = rate; //| CS8 | ~CSTOPB | PARENB | ~PARODD | CLOCAL | CREAD;
    //newtio.c_iflag = IGNPAR;
    newtio.c_oflag = 0;
    newtio.c_lflag = 0;       //ICANON;
    //newtio.c_cc[VMIN]=1;
    //newtio.c_cc[VTIME]=0;
 
    // à présent, on vide la ligne du port, et on active la configuration
    tcflush(fd, TCIFLUSH);
    tcsetattr(fd,TCSANOW,&newtio);
    fcntl(fd, F_SETFL, getpid());
    return fd; //renvoit résultat tentative ouverture
 
 
 
 
}
 
int ResetSerialPort()
{
    //restaure l'ancienne configuration du port série
    tcsetattr(fd,TCSANOW,&oldtio);
    return 0;
}
 
int WriteSerialPort(char* SerialPOut)
{
    int iOut;
    if (fd < 1)
    {
        printf(" Port série non ouvert   %d\n", fd);
        return -1;
    } // end if
    iOut = write(fd, SerialPOut, strlen(SerialPOut));
    if (iOut < 0)
    {
        printf("Erreur écriture %d %s\n", errno, strerror(errno));
    }
    else
    {
    	printf("%d caractères ont été écrit: %s\n", iOut, SerialPOut);
    }
    return iOut; //renvoit le résultat de l'écriture
}
 
 
int ReadSerialPort(char* SerialPReponse, int Chmax)
{
    int iIn,dawf;
 
    //printf("in ReadAdrPort Chmax=%d\n", Chmax);
    if (fd < 1)
    {
        printf(" Port série non ouvert\n");
        return -1;
    } // end if
    strncpy (SerialPReponse, "N/A", Chmax<4?Chmax:4); //si Chmax est < 4, alors on prend Chmax, sinon 4
    iIn = read(fd, SerialPReponse, Chmax-1);
    if (iIn < 0)
    {
    	if (errno == EAGAIN)
    	{
            return 0; // assume that command generated no response
        }
        else
        {
            printf("Erreur de lecture %d %s\n", errno, strerror(errno));
        }
    }
    else
    {
    	SerialPReponse[iIn<Chmax?iIn:Chmax] = '\0';
	    printf("%d carcatères ont été lu: %s\n", iIn, SerialPReponse);
	    dawf=strlen(SerialPReponse);
	    printf ("longueur %d \n",dawf);
 
    }
    return iIn;
}
 
int SerialPort_sendByte(int caract){
 
    unsigned char c = (unsigned char)caract;
 
    if ( caract >= 0 && caract < 256 )
    {
        write(fd, &c, 1);
        //printf("Sending: %02X\n",c);
        //usleep(20000);
        return 0;
    }else return 1;
 
}
 
 
int main ()
{
    char sCmd[255];
	char sResult[255];
	int i;
 
	//strcpy(sCmd,"105B086316");
 
    if (OpenSerialPort < 0) return 0;
 
    printf("tagada");
    //if (WriteSerialPort(sCmd) < 0) return 0;
		//sleep(1); // temps d'attente pour une réponse
		/*
SerialPort_sendByte(0x10);
SerialPort_sendByte(0x5B);
SerialPort_sendByte(0x08);
SerialPort_sendByte(0x63);
SerialPort_sendByte(0x16);
usleep(1000);
 
    if (ReadSerialPort(sResult,255) > 0)
	{
			printf("La reponse fut: %s\n", sResult);
			fflush (stdout);
	}
*/
    //ResetSerialPort();
//sleep(5);
    return 1;
}
Merci pour votre aide