Skip to content

Commit

Permalink
added serial port settings restoration, being polite
Browse files Browse the repository at this point in the history
  • Loading branch information
stg committed May 21, 2012
1 parent 1f2c29a commit 7fda6ce
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Binary file modified bin-linux/knit
Binary file not shown.
11 changes: 9 additions & 2 deletions src/knit.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ bool sclose() {
#include <fcntl.h>

static int h_serial;
struct termios restore;

// device has form "/dev/ttySn"
bool sopen(char* device) {
Expand All @@ -130,8 +131,10 @@ bool sconfig(char* fmt) {
argv[argc++]=++p_parse;
} else p_parse++;
}
// get current settings
tcgetattr(h_serial,&options);
memcpy(&restore,&options,sizeof(options));
// configure baudrate
tcgetattr(h_serial,&options);
switch(atoi(argv[0])) {
case 1200: cfsetispeed(&options,B1200 ); cfsetospeed(&options,B1200 ); break;
case 2400: cfsetispeed(&options,B2400 ); cfsetospeed(&options,B2400 ); break;
Expand Down Expand Up @@ -179,7 +182,11 @@ int32_t swrite(void* p_write,uint16_t i_write) {
}

bool sclose() {
return close(h_serial)==0;
if(h_serial>=0) {
tcsetattr(h_serial,TCSANOW,&restore);
return close(h_serial)==0;
}
return false;
}

#endif
Expand Down

0 comments on commit 7fda6ce

Please sign in to comment.