diff --git a/config.h.in b/config.h.in deleted file mode 100755 --- a/config.h.in +++ /dev/null @@ -1,62 +0,0 @@ -/* config.h.in. Generated from configure.ac by autoheader. */ - -/* Define to 1 if you have the header file. */ -#undef HAVE_DLFCN_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_INTTYPES_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_MEMORY_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STDINT_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STDLIB_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STRINGS_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_STRING_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_SYS_STAT_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_SYS_TYPES_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_TERMIOS_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_UNISTD_H - -/* Define to 1 if you have the header file. */ -#undef HAVE_WINDOWS_H - -/* Define to the sub-directory in which libtool stores uninstalled libraries. - */ -#undef LT_OBJDIR - -/* Define to the address where bug reports for this package should be sent. */ -#undef PACKAGE_BUGREPORT - -/* Define to the full name of this package. */ -#undef PACKAGE_NAME - -/* Define to the full name and version of this package. */ -#undef PACKAGE_STRING - -/* Define to the one symbol short name of this package. */ -#undef PACKAGE_TARNAME - -/* Define to the home page for this package. */ -#undef PACKAGE_URL - -/* Define to the version of this package. */ -#undef PACKAGE_VERSION - -/* Define to 1 if you have the ANSI C header files. */ -#undef STDC_HEADERS diff --git a/configure.ac b/configure.ac --- a/configure.ac +++ b/configure.ac @@ -1,7 +1,7 @@ # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. AC_PREREQ([2.67]) -AC_INIT([librs232],[2.0.0],[alexis.jeandet@lpp.polytechnique.fr],[rs232],[http://www.lpp.fr]) +AC_INIT([librs232],[2.1.0],[alexis.jeandet@lpp.polytechnique.fr],[rs232],[http://www.lpp.fr]) AM_INIT_AUTOMAKE([1.10 -Wall foreign]) AC_CONFIG_MACRO_DIR([m4]) # Checks for programs. @@ -20,8 +20,8 @@ AM_PROG_LIBTOOL # Checks for header files. AC_CHECK_HEADERS([stdio.h string.h fcntl.h errno.h unistd.h termios.h termio.h windows.h]) -AC_SUBST([RS232_SO_VERSION], [2:0:0]) -AC_SUBST([RS232_API_VERSION], [2.0]) +AC_SUBST([RS232_SO_VERSION], [2:1:0]) +AC_SUBST([RS232_API_VERSION], [2.1]) # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,7 +12,7 @@ librs232_@RS232_API_VERSION@_la_SOURCES RS232_unix.c \ RS232_win.c -include_HEADERS = RS232.h rs232config.h +include_HEADERS = RS232.h pkgconfigdir = $(libdir)/pkgconfig diff --git a/src/RS232.h b/src/RS232.h --- a/src/RS232.h +++ b/src/RS232.h @@ -3,25 +3,14 @@ #define RS232_H #define debug -#include "rs232config.h" +#define badPortValue -1 +#define rs232noerr 0 +typedef int rs232port_t; +typedef int rs232speed_t; +typedef enum {rs232OneStop=1,rs232One5Stop=2,rs232TwoStop=2}rs232stop; +typedef enum {rs232parityNo,rs232parityOdd,rs232parityEven}rs232parity; -#ifdef HAVE_WINDOWS_H - #include - #define badPortValue INVALID_HANDLE_VALUE - typedef HANDLE rs232port_t; - typedef DWORD rs232speed_t; - typedef enum {rs232OneStop=ONESTOPBIT,rs232One5Stop=ONE5STOPBITS,rs232TwoStop=TWOSTOPBITS}rs232stop; - typedef enum {rs232parityNo=NOPARITY,rs232parityOdd=ODDPARITY,rs232parityEven=EVENPARITY}rs232parity; -#else -#ifdef HAVE_TERMIOS_H -#include - #define badPortValue -1 - typedef int rs232port_t; - typedef speed_t rs232speed_t; - typedef enum {rs232OneStop=1,rs232One5Stop=2,rs232TwoStop=2}rs232stop; - typedef enum {rs232parityNo,rs232parityOdd,rs232parityEven}rs232parity; -#endif -#endif + #ifdef __cplusplus #define rs232extern extern "C" #else diff --git a/src/RS232_unix.c b/src/RS232_unix.c --- a/src/RS232_unix.c +++ b/src/RS232_unix.c @@ -20,8 +20,8 @@ rs232speed_t rs232cfspeed(unsigned int B rs232port_t rs232open(char* psPortName) { rs232port_t fd; - fd = open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY); - fcntl(fd, F_SETFL, 0); + fd = (rs232port_t)open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY); + fcntl((int)fd, F_SETFL, 0); //fd = open(psPortName, O_RDWR | O_NOCTTY); #ifdef debug if(fd==-1)printf("can't open Port\n"); @@ -56,9 +56,9 @@ int rs232setup(rs232port_t fd, int ChSiz 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); + rs232cfparity((int)fd, &terminos, Parity); + rs232cfnbstop((int)fd, &terminos, NbStop); + rs232cfcsize((int)fd, &terminos, ChSize); terminos.c_cc[VMIN]=0; terminos.c_cc[VTIME]=1; tcflush(fd, TCIFLUSH); @@ -80,10 +80,10 @@ int rs232setbaudrate(rs232port_t fd, int else { struct termios terminos; - tcgetattr(fd, &terminos); + tcgetattr((int)fd, &terminos); cfsetispeed(&terminos, rs232cfspeed(baudrate)); cfsetospeed(&terminos, rs232cfspeed(baudrate)); - tcsetattr(fd, TCSANOW, &terminos); + tcsetattr((int)fd, TCSANOW, &terminos); return 0; } } @@ -97,7 +97,7 @@ int rs232setparity(rs232port_t fd, rs232 else { struct termios terminos; - tcgetattr(fd, &terminos); + tcgetattr((int)fd, &terminos); terminos.c_cflag &= ~PARENB; terminos.c_cflag &= ~PARODD; switch(Parity) @@ -117,7 +117,7 @@ int rs232setparity(rs232port_t fd, rs232 terminos.c_cflag &= ~PARENB; break; } - tcsetattr(fd, TCSANOW, &terminos); + tcsetattr((int)fd, TCSANOW, &terminos); return 0; } } @@ -131,7 +131,7 @@ int rs232setnbstop(rs232port_t fd, rs232 else { struct termios terminos; - tcgetattr(fd, &terminos); + tcgetattr((int)fd, &terminos); switch(NbStop) { case 2: @@ -141,7 +141,7 @@ int rs232setnbstop(rs232port_t fd, rs232 terminos.c_cflag &= ~CSTOPB; break; } - tcsetattr(fd, TCSANOW, &terminos); + tcsetattr((int)fd, TCSANOW, &terminos); return 0; } } @@ -156,7 +156,7 @@ int rs232setcsize(rs232port_t fd, int Ch else { struct termios terminos; - tcgetattr(fd, &terminos); + tcgetattr((int)fd, &terminos); terminos.c_cflag &= ~CSIZE; switch(ChSize) { @@ -173,7 +173,7 @@ int rs232setcsize(rs232port_t fd, int Ch terminos.c_cflag |= CS8; break; } - tcsetattr(fd, TCSANOW, &terminos); + tcsetattr((int)fd, TCSANOW, &terminos); return 0; } } @@ -330,7 +330,7 @@ int rs232write(rs232port_t fd,char *psWr } else { - return write(fd, psWrite, WriteBufferSize); + return write((int)fd, psWrite, WriteBufferSize); } } @@ -344,7 +344,7 @@ int rs232read(rs232port_t fd,char *psRea } else { - return read(fd, psReadHex, ReadBufferSize); + return read((int)fd, psReadHex, ReadBufferSize); } } @@ -353,9 +353,9 @@ int rs232read(rs232port_t fd,char *psRea int rs232setRTS(rs232port_t fd) { int status; - ioctl(fd, TIOCMGET, &status); + ioctl((int)fd, TIOCMGET, &status); status &= ~TIOCM_RTS; - if (ioctl(fd, TIOCMSET, &status)) + if (ioctl((int)fd, TIOCMSET, &status)) { return -1; } @@ -365,9 +365,9 @@ int rs232setRTS(rs232port_t fd) int rs232clearRTS(rs232port_t fd) { int status; - ioctl(fd, TIOCMGET, &status); + ioctl((int)fd, TIOCMGET, &status); status |= TIOCM_RTS; - if (ioctl(fd, TIOCMSET, &status)) + if (ioctl((int)fd, TIOCMSET, &status)) { return -1; } @@ -377,9 +377,9 @@ int rs232clearRTS(rs232port_t fd) int rs232setDTR(rs232port_t fd) { int status; - ioctl(fd, TIOCMGET, &status); + ioctl((int)fd, TIOCMGET, &status); status &= ~TIOCM_DTR; - if (ioctl(fd, TIOCMSET, &status)) + if (ioctl((int)fd, TIOCMSET, &status)) { return -1; } @@ -390,9 +390,9 @@ int rs232setDTR(rs232port_t fd) int rs232clearDTR(rs232port_t fd) { int status; - ioctl(fd, TIOCMGET, &status); + ioctl((int)fd, TIOCMGET, &status); status |= TIOCM_DTR; - if (ioctl(fd, TIOCMSET, &status)) + if (ioctl((int)fd, TIOCMSET, &status)) { return -1; } @@ -407,7 +407,7 @@ int rs232saferead(rs232port_t fd,char* d int i=0; for(i=0;i<100;i++) { - read = rs232read(fd,data,count); + read = rs232read((int)fd,data,count); count -=read; data+=read; if(count==0) @@ -424,7 +424,7 @@ int rs232safewrite(rs232port_t fd,char* int i=0; for(i=0;i<1000;i++) { - written = rs232write(fd,data+written,count); + written = rs232write((int)fd,data+written,count); count-=written; data+=written; if(count==0) diff --git a/src/RS232_win.c b/src/RS232_win.c --- a/src/RS232_win.c +++ b/src/RS232_win.c @@ -13,172 +13,209 @@ #ifdef HAVE_WINDOWS_H +int privatedecodeparity(rs232parity Parity ) +{ + switch(Parity) + { + case rs232parityNo: + return NOPARITY; + break; + case rs232parityOdd: + return ODDPARITY; + break; + case rs232parityEven: + return EVENPARITY; + break; + default: + return NOPARITY; + break; + } +} + +int privatedecodestop(rs232stop NbStop) +{ + switch(NbStop) + { + case rs232OneStop: + return ONESTOPBIT; + break; + case rs232One5Stop: + return ONE5STOPBITS; + break; + case rs232TwoStop: + return TWOSTOPBITS; + break; + default; + return ONESTOPBIT; + break; + } +} + rs232speed_t rs232cfspeed(unsigned int BaudeRate) { if(BaudeRate<123) - return CBR_110; + return (rs232speed_t)CBR_110; if(BaudeRate<450) - return CBR_300; + return (rs232speed_t)CBR_300; if(BaudeRate<900) - return CBR_600; + return (rs232speed_t)CBR_600; if(BaudeRate<1500) - return CBR_1200; + return (rs232speed_t)CBR_1200; if(BaudeRate<3600) - return CBR_2400; + return (rs232speed_t)CBR_2400; if(BaudeRate<7200) - return CBR_4800; + return (rs232speed_t)CBR_4800; if(BaudeRate<14000) - return CBR_9600; + return (rs232speed_t)CBR_9600; if(BaudeRate<16800) - return CBR_14400; + return (rs232speed_t)CBR_14400; if(BaudeRate<28800) - return CBR_19200; + return (rs232speed_t)CBR_19200; if(BaudeRate<48000) - return CBR_38400; + return (rs232speed_t)CBR_38400; if(BaudeRate<86400) - return CBR_57600; + return (rs232speed_t)CBR_57600; if(BaudeRate<172800) - return CBR_115200; + return (rs232speed_t)CBR_115200; else - return CBR_256000; + return (rs232speed_t)CBR_256000; } rs232port_t rs232open(char* psPortName) { rs232port_t fd; - fd = CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); + fd = (rs232port_t)CreateFile(psPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); + #ifdef debug + if(fd==INVALID_HANDLE_VALUE) + { + printf("can't open Port\n"); + return (rs232port_t)badPortValue; + } + #endif return fd; } int rs232close(rs232port_t fd) { - if (fd == badPortValue) + if (fd == (rs232port_t)INVALID_HANDLE_VALUE) { - return -1; + return (rs232port_t)badPortValue; } else { - CloseHandle(fd); - return 0; + CloseHandle((HANDLE)fd); + return rs232noerr; } } int rs232setup(rs232port_t fd, int ChSize, int BaudeRate, rs232parity Parity, rs232stop NbStop) { - if (fd == badPortValue) + if (fd == (rs232port_t)INVALID_HANDLE_VALUE) { - return -1; + return (rs232port_t)badPortValue; } else { DCB dcbSerialParams = {0}; dcbSerialParams.DCBlength=sizeof(dcbSerialParams); - GetCommState(fd, &dcbSerialParams); + GetCommState((HANDLE)fd, &dcbSerialParams); dcbSerialParams.BaudRate=rs232cfspeed(BaudeRate); dcbSerialParams.ByteSize=ChSize; - dcbSerialParams.StopBits=NbStop; - dcbSerialParams.Parity=Parity; - SetCommState(fd, &dcbSerialParams); + dcbSerialParams.StopBits=privatedecodestop(NbStop); + dcbSerialParams.Parity=privatedecodeparity(Parity); + SetCommState((HANDLE)fd, &dcbSerialParams); COMMTIMEOUTS timeouts={0}; timeouts.ReadIntervalTimeout=100; timeouts.ReadTotalTimeoutConstant=100; timeouts.ReadTotalTimeoutMultiplier=1; timeouts.WriteTotalTimeoutConstant=100; timeouts.WriteTotalTimeoutMultiplier=10; - SetCommTimeouts(fd, &timeouts); - return 0; + SetCommTimeouts((HANDLE)fd, &timeouts); + return rs232noerr; } } int rs232setbaudrate(rs232port_t fd, int baudrate) { - if (fd == badPortValue) + if (fd == (rs232port_t)INVALID_HANDLE_VALUE) { - return -1; + return (rs232port_t)badPortValue; } else { DCB dcbSerialParams = {0}; dcbSerialParams.DCBlength=sizeof(dcbSerialParams); - GetCommState(fd, &dcbSerialParams); + GetCommState((HANDLE)fd, &dcbSerialParams); dcbSerialParams.BaudRate=rs232cfspeed(baudrate); - SetCommState(fd, &dcbSerialParams); - return 0; + SetCommState((HANDLE)fd, &dcbSerialParams); + return rs232noerr; } } int rs232setparity(rs232port_t fd, rs232parity Parity) { - if (fd == badPortValue) + if (fd == (rs232port_t)INVALID_HANDLE_VALUE) { - return -1; + return (rs232port_t)badPortValue; } else { DCB dcbSerialParams = {0}; dcbSerialParams.DCBlength=sizeof(dcbSerialParams); - GetCommState(fd, &dcbSerialParams); - dcbSerialParams.Parity = Parity; - SetCommState(fd, &dcbSerialParams); - return 0; + GetCommState((HANDLE)fd, &dcbSerialParams); + dcbSerialParams.Parity = privatedecodeparity(Parity); + SetCommState((HANDLE)fd, &dcbSerialParams); + return rs232noerr; } } int rs232setnbstop(rs232port_t fd, rs232stop NbStop) { - if (fd == badPortValue) + if (fd == (rs232port_t)INVALID_HANDLE_VALUE) { - return -1; + return (rs232port_t)badPortValue; } else { DCB dcbSerialParams = {0}; dcbSerialParams.DCBlength=sizeof(dcbSerialParams); - GetCommState(fd, &dcbSerialParams); - switch(NbStop) - { - case 2: - dcbSerialParams.StopBits = 2; - break; - default: - dcbSerialParams.StopBits = 1; - break; - } - SetCommState(fd, &dcbSerialParams); - return 0; + GetCommState((HANDLE)fd, &dcbSerialParams); + dcbSerialParams.StopBits = privatedecodestop(NbStop); + SetCommState((HANDLE)fd, &dcbSerialParams); + return rs232noerr; } } int rs232setcsize(rs232port_t fd, int ChSize) { - if (fd == badPortValue) + if (fd == (rs232port_t)INVALID_HANDLE_VALUE) { - return -1; + return (rs232port_t)badPortValue; } else { DCB dcbSerialParams = {0}; dcbSerialParams.DCBlength=sizeof(dcbSerialParams); - GetCommState(fd, &dcbSerialParams); + GetCommState((HANDLE)fd, &dcbSerialParams); dcbSerialParams.ByteSize = ChSize; - SetCommState(fd, &dcbSerialParams); - return 0; + SetCommState((HANDLE)fd, &dcbSerialParams); + return rs232noerr; } } @@ -190,14 +227,14 @@ int rs232setcsize(rs232port_t fd, int Ch int rs232write(rs232port_t fd,char *psWrite, int WriteBufferSize) { - if (fd == badPortValue) + if (fd == (rs232port_t)INVALID_HANDLE_VALUE) { - return -1; + return (rs232port_t)badPortValue; } else { DWORD dwBytesWriten = 0; - WriteFile(fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL); + WriteFile((HANDLE)fd, psWrite, WriteBufferSize, &dwBytesWriten, NULL); return dwBytesWriten; } } @@ -206,14 +243,14 @@ int rs232write(rs232port_t fd,char *psWr int rs232read(rs232port_t fd,char *psRead, int ReadBufferSize) { - if (fd == badPortValue) + if (fd == (rs232port_t)INVALID_HANDLE_VALUE) { - return -1; + return (rs232port_t)badPortValue; } else { DWORD dwBytesRead = 0; - ReadFile(fd, psRead, ReadBufferSize, &dwBytesRead, NULL); + ReadFile((HANDLE)fd, psRead, ReadBufferSize, &dwBytesRead, NULL); return dwBytesRead; } @@ -230,7 +267,7 @@ int rs232saferead(rs232port_t fd,char* d count -=read; data+=read; if(count==0) - return 0; + return rs232noerr; } return -1; } @@ -247,7 +284,7 @@ int rs232safewrite(rs232port_t fd,char* count-=written; data+=written; if(count==0) - return 0; + return rs232noerr; } return -1; } @@ -255,30 +292,30 @@ int rs232safewrite(rs232port_t fd,char* int rs232setRTS(rs232port_t fd) { - if(EscapeCommFunction(fd, CLRRTS)) - return 0; + if(EscapeCommFunction((HANDLE)fd, CLRRTS)) + return rs232noerr; return -1; } int rs232clearRTS(rs232port_t fd) { - if(EscapeCommFunction(fd, SETRTS)) - return 0; + if(EscapeCommFunction((HANDLE)fd, SETRTS)) + return rs232noerr; return -1; } int rs232setDTR(rs232port_t fd) { - if(EscapeCommFunction(fd, CLRDTR)) - return 0; + if(EscapeCommFunction((HANDLE)fd, CLRDTR)) + return rs232noerr; return -1; } int rs232clearDTR(rs232port_t fd) { - if(EscapeCommFunction(fd, SETDTR)) - return 0; + if(EscapeCommFunction((HANDLE)fd, SETDTR)) + return rs232noerr; return -1; }