diff --git a/Makefile b/Makefile new file mode 100755 --- /dev/null +++ b/Makefile @@ -0,0 +1,58 @@ + +VERSIONMJ=1 +VERSIONM=0 +BUILD=1 +lIBFILE=liblppserial.so.$(VERSIONMJ).$(VERSIONM).$(BUILD) +CurrentPath="$(shell pwd)" + + + +all:dirs + gcc -c RS232.c -fPIC -Wall -o ./tmp/RS232.o + gcc -shared -Wl,-soname,liblppserial.so.$(VERSIONMJ) -o ./bin/$(lIBFILE) ./tmp/RS232.o + ln -sf $(CurrentPath)/bin/$(lIBFILE) $(CurrentPath)/bin/liblppserial.so + ln -sf $(CurrentPath)/bin/$(lIBFILE) $(CurrentPath)/bin/liblppserial.so.1 + ar rs $(CurrentPath)/bin/liblppserial.a ./tmp/RS232.o + cp RS232.h ./include/lppserial.h + @echo "lppserial compiled" + + + +install: + sudo cp ./bin/$(lIBFILE) /usr/lib/$(lIBFILE) + sudo cp include/* /usr/include/ + ln -sf /usr/lib/$(lIBFILE) /usr/lib/liblppserial.so + ln -sf /usr/lib/$(lIBFILE) /usr/lib/liblppserial.so.1 + cp $(CurrentPath)/bin/liblppserial.a /usr/lib/liblppserial.a + ldconfig + + + +dist: + tar -cvzf ./../lppserial.tgz ../lppserial + + + + +clean: + rm bin/* + rm tmp/* + rm include/* + + + +distclean: + rm -f -R tmp + rm -f -R bin + rm -f -R include + + + + +dirs: + mkdir -p tmp + mkdir -p bin + mkdir -p include + + + diff --git a/RS232.c b/RS232.c new file mode 100755 --- /dev/null +++ b/RS232.c @@ -0,0 +1,242 @@ +// SOPSUYSI_RS232.c - Function to communicate on Serial Port + +#include +#include +#include +#include +#include +#include +#include "RS232.h" + + +speed_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 +// if(172799c_cflag |= PARENB; + break; + default: + terminos->c_cflag &= ~PARENB; + break; + } + return 0; + } +} + + +int rs232cfnbstop(int fd, struct termios *terminos, int NbStop) +{ + if (fd == -1) + { + 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) +{ + if (fd == -1) + { + return fd; + } + else + { + 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 rs232open(char* psPortName) +{ + + int fd = open(psPortName, O_RDWR | O_NOCTTY | O_NDELAY); + + return fd; +} + + +int rs232setup(int fd, int ChSize, int BaudeRate, int Parity, int NbStop) +{ + if (fd == -1) + { + return fd; + } + 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); + + + tcsetattr(fd, TCSANOW, &terminos); + tcgetattr(fd, &terminos); + return 0; + } +} + + +int rs232write(int fd,char *psWrite) +{ + int Len_psWrite = (int)strlen(psWrite); + int charsWrite=0; + + if (fd == -1) + { + return -1; + } + else + { + charsWrite = write(fd, psWrite, Len_psWrite); + return charsWrite; + } +} + + +int rs232read(int fd,char *psReadHex) +{ + char ReadBuff[ReadBufferSize]; + int CharsRead=0, Reads=0, i=0; + + for(i=0;i 0) + { + CharsRead = CharsRead + Reads; + ReadBuff[CharsRead]='\0'; + strcat(psReadHex, ReadBuff); + } + + } + return CharsRead; + } +} + + +int rs232close(int fd) +{ + if (fd == -1) + { + return fd; + } + else + { + close(fd); + return 0; + } +} diff --git a/RS232.h b/RS232.h new file mode 100755 --- /dev/null +++ b/RS232.h @@ -0,0 +1,11 @@ +// SOPSUYSI_RS232.h + + +#define ReadBufferSize 16*1024 + + +int rs232open(char* psPortName); +int rs232close(int fd); +int rs232setup(int fd, int ChSize, int BaudeRate, int Parity, int NbStop); +int rs232write(int fd,char *psWrite); +int rs232read(int fd,char *psReadHex);