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