RS232_unix.c
438 lines
| 7.3 KiB
| text/x-c
|
CLexer
/ src / RS232_unix.c
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | #include <stdio.h> | ||
#include <unistd.h> | ||||
#include <fcntl.h> | ||||
#include <string.h> | ||||
#include <errno.h> | ||||
yannic
|
r7 | #include "rs232config.h" | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | #ifdef HAVE_TERMIOS_H | ||
#include <termios.h> | ||||
#endif | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r26 | #ifdef HAVE_TERMIO_H | ||
#include <termio.h> | ||||
#endif | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | #include "RS232.h" | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | #ifdef HAVE_WINDOWS_H | ||
#else | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | #ifdef HAVE_TERMIOS_H | ||
rs232speed_t rs232cfspeed(unsigned int BaudeRate); | ||||
rs232port_t rs232open(char* psPortName) | ||||
{ | ||||
rs232port_t fd; | ||||
fd = open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY); | ||||
r12 | fcntl(fd, F_SETFL, 0); | |||
//fd = open(psPortName, O_RDWR | O_NOCTTY); | ||||
yannic
|
r11 | #ifdef debug | ||
if(fd==-1)printf("can't open Port\n"); | ||||
#endif | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | return fd; | ||
} | ||||
int rs232close(rs232port_t fd) | ||||
{ | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
close(fd); | ||||
return 0; | ||||
} | ||||
} | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
struct termios terminos; | ||||
tcgetattr(fd, &terminos); | ||||
cfsetispeed(&terminos, rs232cfspeed(BaudeRate)); | ||||
cfsetospeed(&terminos, rs232cfspeed(BaudeRate)); | ||||
terminos.c_cflag |= (CLOCAL | CREAD); | ||||
rs232cfparity(fd, &terminos, Parity); | ||||
rs232cfnbstop(fd, &terminos, NbStop); | ||||
rs232cfcsize(fd, &terminos, ChSize); | ||||
r12 | terminos.c_cc[VMIN]=0; | |||
r13 | terminos.c_cc[VTIME]=1; | |||
r12 | tcflush(fd, TCIFLUSH); | |||
yannic
|
r11 | #ifdef debug | ||
if(tcsetattr(fd, TCSANOW, &terminos)!=0)printf("bad setup\n"); | ||||
#else | ||||
tcsetattr(fd, TCSANOW, &terminos); | ||||
#endif | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | return 0; | ||
} | ||||
} | ||||
int rs232setbaudrate(rs232port_t fd, int baudrate) | ||||
{ | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return fd; | ||||
} | ||||
else | ||||
{ | ||||
struct termios terminos; | ||||
tcgetattr(fd, &terminos); | ||||
cfsetispeed(&terminos, rs232cfspeed(baudrate)); | ||||
cfsetospeed(&terminos, rs232cfspeed(baudrate)); | ||||
tcsetattr(fd, TCSANOW, &terminos); | ||||
return 0; | ||||
} | ||||
} | ||||
int rs232setparity(rs232port_t fd, rs232parity Parity) | ||||
{ | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return fd; | ||||
} | ||||
else | ||||
{ | ||||
struct termios terminos; | ||||
tcgetattr(fd, &terminos); | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r26 | terminos.c_cflag &= ~PARENB; | ||
terminos.c_cflag &= ~PARODD; | ||||
yannic
|
r11 | switch(Parity) | ||
{ | ||||
case rs232parityNo: | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r25 | terminos.c_cflag &= ~PARENB; | ||
yannic
|
r11 | break; | ||
case rs232parityOdd: | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r25 | terminos.c_cflag |= PARENB; | ||
terminos.c_cflag |= PARODD; | ||||
yannic
|
r11 | break; | ||
case rs232parityEven: | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r25 | terminos.c_cflag |= PARENB; | ||
terminos.c_cflag &= ~PARODD; | ||||
yannic
|
r11 | break; | ||
default: | ||||
terminos.c_cflag &= ~PARENB; | ||||
break; | ||||
} | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | tcsetattr(fd, TCSANOW, &terminos); | ||
return 0; | ||||
} | ||||
} | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r9 | int rs232setnbstop(rs232port_t fd, rs232stop NbStop) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return fd; | ||||
} | ||||
else | ||||
{ | ||||
struct termios terminos; | ||||
tcgetattr(fd, &terminos); | ||||
switch(NbStop) | ||||
{ | ||||
case 2: | ||||
terminos.c_cflag |= CSTOPB; | ||||
break; | ||||
default: | ||||
terminos.c_cflag &= ~CSTOPB; | ||||
break; | ||||
} | ||||
tcsetattr(fd, TCSANOW, &terminos); | ||||
return 0; | ||||
} | ||||
} | ||||
int rs232setcsize(rs232port_t fd, int ChSize) | ||||
{ | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return fd; | ||||
} | ||||
else | ||||
{ | ||||
struct termios terminos; | ||||
tcgetattr(fd, &terminos); | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r26 | terminos.c_cflag &= ~CSIZE; | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | switch(ChSize) | ||
{ | ||||
case 5: | ||||
terminos.c_cflag |= CS5; | ||||
break; | ||||
case 6: | ||||
terminos.c_cflag |= CS6; | ||||
break; | ||||
case 7: | ||||
terminos.c_cflag |= CS7; | ||||
break; | ||||
default: | ||||
terminos.c_cflag |= CS8; | ||||
break; | ||||
} | ||||
tcsetattr(fd, TCSANOW, &terminos); | ||||
return 0; | ||||
} | ||||
} | ||||
rs232speed_t rs232cfspeed(unsigned int BaudeRate) | ||||
{ | ||||
if(BaudeRate<25) | ||||
return B0; | ||||
if(BaudeRate<67) | ||||
return B50; | ||||
if(BaudeRate<93) | ||||
return B75; | ||||
if(BaudeRate<123) | ||||
return B110; | ||||
if(BaudeRate<142) | ||||
return B134; | ||||
if(BaudeRate<175) | ||||
return B150; | ||||
if(BaudeRate<250) | ||||
return B200; | ||||
if(BaudeRate<450) | ||||
return B300; | ||||
if(BaudeRate<900) | ||||
return B600; | ||||
if(BaudeRate<1500) | ||||
return B1200; | ||||
if(BaudeRate<2100) | ||||
return B1800; | ||||
if(BaudeRate<3600) | ||||
return B2400; | ||||
if(BaudeRate<7200) | ||||
return B4800; | ||||
if(BaudeRate<1400) | ||||
return B9600; | ||||
if(BaudeRate<28800) | ||||
return B19200; | ||||
if(BaudeRate<48000) | ||||
return B38400; | ||||
if(BaudeRate<86400) | ||||
return B57600; | ||||
if(BaudeRate<172800) | ||||
return B115200; | ||||
else | ||||
return B230400; | ||||
} | ||||
int rs232cfparity(int fd, struct termios *terminos, rs232parity Parity) | ||||
{ | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return fd; | ||||
} | ||||
else | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r26 | terminos->c_cflag &= ~PARENB; | ||
terminos->c_cflag &= ~PARODD; | ||||
yannic
|
r11 | switch(Parity) | ||
{ | ||||
case rs232parityNo: | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r26 | terminos->c_cflag &= ~PARENB; | ||
terminos->c_cflag &= ~PARODD; | ||||
yannic
|
r11 | break; | ||
case rs232parityOdd: | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r26 | terminos->c_cflag |= PARENB; | ||
terminos->c_cflag |= PARODD; | ||||
yannic
|
r11 | break; | ||
case rs232parityEven: | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r26 | terminos->c_cflag |= PARENB; | ||
terminos->c_cflag &= ~PARODD; | ||||
yannic
|
r11 | break; | ||
default: | ||||
terminos->c_cflag &= ~PARENB; | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r26 | terminos->c_cflag &= ~PARODD; | ||
yannic
|
r11 | break; | ||
} | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | return 0; | ||
} | ||||
} | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | int rs232cfnbstop(int fd, struct termios *terminos, rs232stop NbStop) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return fd; | ||||
} | ||||
else | ||||
{ | ||||
switch(NbStop) | ||||
{ | ||||
case 2: | ||||
terminos->c_cflag |= CSTOPB; | ||||
break; | ||||
default: | ||||
terminos->c_cflag &= ~CSTOPB; | ||||
break; | ||||
} | ||||
return 0; | ||||
} | ||||
} | ||||
int rs232cfcsize(int fd, struct termios *terminos, int ChSize) | ||||
{ | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return fd; | ||||
} | ||||
else | ||||
{ | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r26 | terminos->c_cflag &= ~CSIZE; | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | switch(ChSize) | ||
{ | ||||
case 5: | ||||
terminos->c_cflag |= CS5; | ||||
break; | ||||
case 6: | ||||
terminos->c_cflag |= CS6; | ||||
break; | ||||
case 7: | ||||
terminos->c_cflag |= CS7; | ||||
break; | ||||
default: | ||||
terminos->c_cflag |= CS8; | ||||
break; | ||||
} | ||||
return 0; | ||||
} | ||||
} | ||||
int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) | ||||
{ | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
return write(fd, psWrite, WriteBufferSize); | ||||
} | ||||
} | ||||
int rs232read(rs232port_t fd,char *psReadHex, int ReadBufferSize) | ||||
{ | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | if ((int)fd == -1) | ||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | { | ||
return -1; | ||||
} | ||||
else | ||||
{ | ||||
return read(fd, psReadHex, ReadBufferSize); | ||||
} | ||||
} | ||||
jeandet@pc-de-jeandet3.lab-lpp.local
|
r26 | |||
int rs232setRTS(rs232port_t fd) | ||||
{ | ||||
int status; | ||||
ioctl(fd, TIOCMGET, &status); | ||||
status &= ~TIOCM_RTS; | ||||
if (ioctl(fd, TIOCMSET, &status)) | ||||
{ | ||||
return -1; | ||||
} | ||||
return 0; | ||||
} | ||||
int rs232clearRTS(rs232port_t fd) | ||||
{ | ||||
int status; | ||||
ioctl(fd, TIOCMGET, &status); | ||||
status |= TIOCM_RTS; | ||||
if (ioctl(fd, TIOCMSET, &status)) | ||||
{ | ||||
return -1; | ||||
} | ||||
return 0; | ||||
} | ||||
int rs232setDTR(rs232port_t fd) | ||||
{ | ||||
int status; | ||||
ioctl(fd, TIOCMGET, &status); | ||||
status &= ~TIOCM_DTR; | ||||
if (ioctl(fd, TIOCMSET, &status)) | ||||
{ | ||||
return -1; | ||||
} | ||||
return 0; | ||||
} | ||||
int rs232clearDTR(rs232port_t fd) | ||||
{ | ||||
int status; | ||||
ioctl(fd, TIOCMGET, &status); | ||||
status |= TIOCM_DTR; | ||||
if (ioctl(fd, TIOCMSET, &status)) | ||||
{ | ||||
return -1; | ||||
} | ||||
return 0; | ||||
} | ||||
int rs232saferead(rs232port_t fd,char* data,int count ) | ||||
{ | ||||
int read=0; | ||||
int i=0; | ||||
for(i=0;i<100;i++) | ||||
{ | ||||
read = rs232read(fd,data,count); | ||||
count -=read; | ||||
data+=read; | ||||
if(count==0) | ||||
return 0; | ||||
} | ||||
return -1; | ||||
} | ||||
int rs232safewrite(rs232port_t fd,char* data,int count) | ||||
{ | ||||
int written=0; | ||||
int i=0; | ||||
for(i=0;i<1000;i++) | ||||
{ | ||||
written = rs232write(fd,data+written,count); | ||||
count-=written; | ||||
data+=written; | ||||
if(count==0) | ||||
return 0; | ||||
} | ||||
return -1; | ||||
} | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r8 | #endif | ||
#endif //#ifdef HAVE_TERMIOS_H | ||||
jeandet@PC-DE-JEANDET.lpp.polytechnique.fr
|
r5 | |||