diff --git a/Application_raspberryJustine/liaisonSerie/serial.c b/Application_raspberryJustine/liaisonSerie/serial.c new file mode 100644 index 0000000..4f3c3b6 --- /dev/null +++ b/Application_raspberryJustine/liaisonSerie/serial.c @@ -0,0 +1,55 @@ +/* + * Serial library + */ + +//// +// Include files +//// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" + +//// +// Functions +//// + +// +// Open serial port device +// +int serialOpen(char *device,int mode){ +int flags=(mode==SERIAL_READ?O_RDONLY:(mode==SERIAL_WRITE?O_WRONLY:O_RDWR)); +int fd=open(device,flags|O_NOCTTY); +if(fd<0){ perror(device); exit(-1); } +return fd; +} + +// +// Serial port configuration +// +void serialConfig(int fd,int speed){ +struct termios new; +bzero(&new,sizeof(new)); +new.c_cflag=CLOCAL|CREAD|speed|CS8; +new.c_iflag=0; +new.c_oflag=0; +new.c_lflag=0; /* set input mode (non-canonical, no echo,...) */ +new.c_cc[VTIME]=0; /* inter-character timer unused */ +new.c_cc[VMIN]=1; /* blocking read until 1 char received */ +if(tcsetattr(fd,TCSANOW,&new)<0){ perror("serialInit.tcsetattr"); exit(-1); } +} + +// +// Serial port termination +// +void serialClose(int fd){ +close(fd); +} diff --git a/Application_raspberryJustine/liaisonSerie/serial.h b/Application_raspberryJustine/liaisonSerie/serial.h new file mode 100644 index 0000000..e7eac5b --- /dev/null +++ b/Application_raspberryJustine/liaisonSerie/serial.h @@ -0,0 +1,18 @@ +/* + * Public definitions for serial library + */ + +//// +// Constants +//// + +#define SERIAL_READ 0 +#define SERIAL_WRITE 1 +#define SERIAL_BOTH 2 + +//// +// Public prototypes +//// +int serialOpen(char *device,int mode); +void serialConfig(int fd,int speed); +void serialClose(int fd); diff --git a/Application_raspberryJustine/liaisonSerie/test.c b/Application_raspberryJustine/liaisonSerie/test.c new file mode 100644 index 0000000..50bf655 --- /dev/null +++ b/Application_raspberryJustine/liaisonSerie/test.c @@ -0,0 +1,40 @@ +/* + * Test on serial device + */ + +//// +// Include files +//// +#include +#include +#include +#include + +#include "serial.h" + +//// +// Constants +//// +#define SERIAL_DEVICE "/dev/ttyACM0" + +//// +// Global variables +//// + +//// +// Main function +//// + +int main(void){ +int c=0; +int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH); +serialConfig(sd,B9600); +if(write(sd,&c,sizeof(char))!=1){ perror("main.write"); exit(-1); } +int i; +for(i=0;i<8;i++){ + if(read(sd,&c,sizeof(char))!=1){ perror("main.read"); exit(-1); } + printf("%02x\n",c); + } +serialClose(sd); +exit(0); +} diff --git a/Application_raspberryJustine/liaisonSerie/test.c~ b/Application_raspberryJustine/liaisonSerie/test.c~ new file mode 100644 index 0000000..50bf655 --- /dev/null +++ b/Application_raspberryJustine/liaisonSerie/test.c~ @@ -0,0 +1,40 @@ +/* + * Test on serial device + */ + +//// +// Include files +//// +#include +#include +#include +#include + +#include "serial.h" + +//// +// Constants +//// +#define SERIAL_DEVICE "/dev/ttyACM0" + +//// +// Global variables +//// + +//// +// Main function +//// + +int main(void){ +int c=0; +int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH); +serialConfig(sd,B9600); +if(write(sd,&c,sizeof(char))!=1){ perror("main.write"); exit(-1); } +int i; +for(i=0;i<8;i++){ + if(read(sd,&c,sizeof(char))!=1){ perror("main.read"); exit(-1); } + printf("%02x\n",c); + } +serialClose(sd); +exit(0); +} diff --git a/liaisonSerie/serial.c b/liaisonSerie/serial.c deleted file mode 100644 index 4f3c3b6..0000000 --- a/liaisonSerie/serial.c +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Serial library - */ - -//// -// Include files -//// -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "serial.h" - -//// -// Functions -//// - -// -// Open serial port device -// -int serialOpen(char *device,int mode){ -int flags=(mode==SERIAL_READ?O_RDONLY:(mode==SERIAL_WRITE?O_WRONLY:O_RDWR)); -int fd=open(device,flags|O_NOCTTY); -if(fd<0){ perror(device); exit(-1); } -return fd; -} - -// -// Serial port configuration -// -void serialConfig(int fd,int speed){ -struct termios new; -bzero(&new,sizeof(new)); -new.c_cflag=CLOCAL|CREAD|speed|CS8; -new.c_iflag=0; -new.c_oflag=0; -new.c_lflag=0; /* set input mode (non-canonical, no echo,...) */ -new.c_cc[VTIME]=0; /* inter-character timer unused */ -new.c_cc[VMIN]=1; /* blocking read until 1 char received */ -if(tcsetattr(fd,TCSANOW,&new)<0){ perror("serialInit.tcsetattr"); exit(-1); } -} - -// -// Serial port termination -// -void serialClose(int fd){ -close(fd); -} diff --git a/liaisonSerie/serial.h b/liaisonSerie/serial.h deleted file mode 100644 index e7eac5b..0000000 --- a/liaisonSerie/serial.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Public definitions for serial library - */ - -//// -// Constants -//// - -#define SERIAL_READ 0 -#define SERIAL_WRITE 1 -#define SERIAL_BOTH 2 - -//// -// Public prototypes -//// -int serialOpen(char *device,int mode); -void serialConfig(int fd,int speed); -void serialClose(int fd); diff --git a/liaisonSerie/test.c b/liaisonSerie/test.c deleted file mode 100644 index 50bf655..0000000 --- a/liaisonSerie/test.c +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Test on serial device - */ - -//// -// Include files -//// -#include -#include -#include -#include - -#include "serial.h" - -//// -// Constants -//// -#define SERIAL_DEVICE "/dev/ttyACM0" - -//// -// Global variables -//// - -//// -// Main function -//// - -int main(void){ -int c=0; -int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH); -serialConfig(sd,B9600); -if(write(sd,&c,sizeof(char))!=1){ perror("main.write"); exit(-1); } -int i; -for(i=0;i<8;i++){ - if(read(sd,&c,sizeof(char))!=1){ perror("main.read"); exit(-1); } - printf("%02x\n",c); - } -serialClose(sd); -exit(0); -} diff --git a/liaisonSerie/test.c~ b/liaisonSerie/test.c~ deleted file mode 100644 index 50bf655..0000000 --- a/liaisonSerie/test.c~ +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Test on serial device - */ - -//// -// Include files -//// -#include -#include -#include -#include - -#include "serial.h" - -//// -// Constants -//// -#define SERIAL_DEVICE "/dev/ttyACM0" - -//// -// Global variables -//// - -//// -// Main function -//// - -int main(void){ -int c=0; -int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH); -serialConfig(sd,B9600); -if(write(sd,&c,sizeof(char))!=1){ perror("main.write"); exit(-1); } -int i; -for(i=0;i<8;i++){ - if(read(sd,&c,sizeof(char))!=1){ perror("main.read"); exit(-1); } - printf("%02x\n",c); - } -serialClose(sd); -exit(0); -} -- libgit2 0.21.2