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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
#include "fonctionsSerie.h"
fd_set readfds;
int messageDispo(int port1, int port2){
/*SE REPORTER A LA PAGE DE MAN POUR COMPRENDRE :
http://www.linux-kheops.com/doc/man/manfr/man-html-0.9/man2/select.2.html */
int portPret;
FD_ZERO(&readfds); // efface l'ensemble
FD_SET(port1,&readfds); // on ajoute les 2 ports series à l'ensemble à surveiller
FD_SET(port2,&readfds);
//on attend indefiniment qu'il y ai quelquechose a lire sur l'un des deux ports :
if(select(FD_SETSIZE,&readfds,NULL,NULL,NULL)>0)
{
//on regarde sur quel port il s'est passé quelque chose
if (FD_ISSET(port1,&readfds)){
//le port1 est pret en lecture
//printf("port1 pret en lecture\n");
portPret=port1;
}
if (FD_ISSET(port2,&readfds)) {
//le port2 est pret en lecture
//printf("port2 pret en lecture\n");
portPret=port2;
}
}
//on fait une petite pause afin d'avoir le temps de recevoir tout
//le message sur le port serie.
usleep(1000);
//on renvoit le numéro du port qui est pret
return(portPret);
}
unsigned int ReadBytes(int theDevice,unsigned char *theBytes,unsigned int maxBytes)
{
unsigned int numRead;
//li ce qu'il y a dans le tampon du port serie
if((numRead=read(theDevice,theBytes,maxBytes))>0)
{
return(numRead);
}
//retourne faux s'il ny avait rien et donc qu'on a rien lu.
return(0);
}
void WriteBytes(int theDevice,unsigned char *theBytes,unsigned int numBytes)
// Write theBytes to theDevice.
{
write(theDevice,theBytes,numBytes);
}
void FlushBytes(int theDevice)
// Flush any bytes that may be waiting at theDevice
{
ioctl(theDevice,TCFLSH,0); // flush the input stream
}
int ConfigureDevice(int theDevice,unsigned int baudRate,unsigned char dataBits,unsigned char stopBits,unsigned char parity,int cooked)
// set up data transmission configuration of theDevice
// NOTE: if any of the passed parameters is invalid, it will be set
// to an arbitrary valid value
// baudRate is: 50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200
// dataBits is 7 or 8
// stopBits is 1 or 2
// parity is 0=none,
// 1=odd,
// 2=even
// cooked : #include <sys/time.h>1 if you want to set up the serial port to be used as a terminal.
// if there is a problem, return false
{
struct termios
terminalParams;
speed_t
theSpeed;
if(ioctl(theDevice,TCGETS,&terminalParams)!=-1) // read the old value
{
switch(baudRate)
{
case 50:
theSpeed=B50;
break;
case 75:
theSpeed=B75;
break;
case 110:
theSpeed=B110;
break;
case 134:
theSpeed=B134;
break;
case 150:
theSpeed=B150;
break;
case 200:
theSpeed=B200;
break;
case 300:
theSpeed=B300;
break;
case 600:
theSpeed=B600;
break;
case 1200:
theSpeed=B1200;
break;
case 1800:
theSpeed=B1800;
break;
case 2400:
theSpeed=B2400;
break;
case 4800:
theSpeed=B4800;
break;
case 9600:
theSpeed=B9600;
break;
case 19200:
theSpeed=B19200;
break;
case 38400:
theSpeed=B38400;
break;
case 57600:
theSpeed=B57600;
break;
case 115200:
theSpeed=B115200;
break;
case 230400:
theSpeed=B230400;
break;
case 460800:
theSpeed=B460800;
break;
default:
theSpeed=B9600;
break;
}
cfsetospeed(&terminalParams,theSpeed);
cfsetispeed(&terminalParams,theSpeed);
terminalParams.c_cflag&=~CSIZE; // mask off the data bits
switch(dataBits)
{
case 7:
terminalParams.c_cflag|=CS7;
break;
case 8:
default:
terminalParams.c_cflag|=CS8;
break;
}
terminalParams.c_cflag&=~CSTOPB; // mask off the stop bits
switch(stopBits)
{
case 1:
break;
case 2:
terminalParams.c_cflag|=CSTOPB;
break;
default:
break;
}
terminalParams.c_cflag&=~(PARENB|PARODD); // mask off the parity bits
switch(parity)
{
case 0:
break;
case 1:
terminalParams.c_cflag|=(PARENB|PARODD); // odd parity
break;
case 2:
terminalParams.c_cflag|=PARENB; // even parity
break;
default:
break;
}
terminalParams.c_cflag|=CREAD; // allow reading
if(cooked) // use this when setting up the serial port to be used as a terminal
{
terminalParams.c_iflag=ICRNL;
terminalParams.c_oflag=OPOST|ONLCR;
terminalParams.c_lflag=ISIG|ICANON|ECHO|ECHOE|ECHONL;
}
else
{
terminalParams.c_iflag=0;
terminalParams.c_oflag=0;
terminalParams.c_lflag=0;
}
terminalParams.c_cc[VMIN]=0; // read returns immediately if no characters
terminalParams.c_cc[VTIME]=0;
if(ioctl(theDevice,TCSETS,&terminalParams)!=-1)
{
return(VRAI);
}
}
return(FAUX);
}
void GetDeviceConfiguration(int theDevice,unsigned int *baudRate,unsigned char *dataBits,unsigned char *stopBits,unsigned char *parity)
// return the configuration of theDevice
// baudRate is: 50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200,230400,460800
// dataBits is 7 or 8
// stopBits is 1 or 2
// parity is 0=none,
// 1=odd,
// 2=even
{
struct termios
terminalParams;
if(ioctl(theDevice,TCGETS,&terminalParams)!=-1) // read the old value
{
switch(cfgetospeed(&terminalParams))
{
case B50:
*baudRate=50;
break;
case B75:
*baudRate=75;
break;
case B110:
*baudRate=110;
break;
case B134:
*baudRate=134;
break;
case B150:
*baudRate=150;
break;
case B200:
*baudRate=200;
break;
case B300:
*baudRate=300;
break;
case B600:
*baudRate=600;
break;
case B1200:
*baudRate=1200;
break;
case B1800:
*baudRate=1800;
break;
case B2400:
*baudRate=2400;
break;
case B4800:
*baudRate=4800;
break;
case B9600:
*baudRate=9600;
break;
case B19200:
*baudRate=19200;
break;
case B38400:
*baudRate=38400;
break;
case B57600:
*baudRate=57600;
break;
case B115200:
*baudRate=115200;
break;
case B230400:
*baudRate=230400;
break;
case B460800:
*baudRate=460800;
break;
default:
*baudRate=0;
break;
}
switch(terminalParams.c_cflag&CSIZE)
{
case CS7:
*dataBits=7;
break;
case CS8:
*dataBits=8;
break;
default:
*dataBits=0;
break;
}
*stopBits=1;
if(terminalParams.c_cflag&CSTOPB)
{
*stopBits=2;
}
*parity=0;
if(terminalParams.c_cflag&PARENB)
{
if(terminalParams.c_cflag&PARODD)
{
*parity=1;
}
else
{
*parity=2;
}
}
}
}
int ConfigureFlowControl(int theDevice,int wantControl)
// if wantControl is true, configure theDevice to use CTS/RTS hardware
// flow control, if false, turn off flow control
// NOTE: when flow control is off, we must drive our RTS line to the
// modem active at all times
// if there is a problem, return false
{
struct termios
terminalParams;
if(ioctl(theDevice,TCGETS,&terminalParams)!=-1) // read the old value
{
if(wantControl==1)
{
terminalParams.c_cflag|=CRTSCTS; // set flow control
}
else
{
terminalParams.c_cflag&=~CRTSCTS; // clear flow control
}
terminalParams.c_cc[VMIN]=0; // read returns immediately if no characters
terminalParams.c_cc[VTIME]=0;
if(ioctl(theDevice,TCSETS,&terminalParams)!=-1)
{
return(1);
}
}
return(0);
}
int OpenDevice(char *theName,int *theDevice)
// Open theName immediately for both read/write
// (do not block under any circumstances)
// return a device handle.
// NOTE: since the device can be opened BEFORE it is locked,
// this function MUST NOT modify the parameters of the device
// or in any way mess with it!!
// if there is a problem, set the error, and return false
{
struct termios terminalParams;
if((*theDevice=open(theName,O_NDELAY|O_RDWR|O_NOCTTY))!=-1)
{
if(ioctl(*theDevice,TCGETS,&terminalParams)!=-1)
// attempt to read configuration, just to verify this is a serial device
{
return(VRAI);
}
close(*theDevice);
}
return(FAUX);
}
void CloseDevice(int theDevice)
// Close theDevice
{
close(theDevice);
} |
Partager