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;
} |
Partager