Skip to content
Snippets Groups Projects
Commit 509b0871 authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Chnages to allow non-standard baudrates.

parent d961fc9f
No related branches found
No related tags found
No related merge requests found
...@@ -14,82 +14,38 @@ CRS232::CRS232(const std::string& comm_id) : CComm(comm_id) ...@@ -14,82 +14,38 @@ CRS232::CRS232(const std::string& comm_id) : CComm(comm_id)
void CRS232::set_baudrate(int baud) void CRS232::set_baudrate(int baud)
{ {
struct termios config; struct termios2 config;
int baudrate; int error;
if(tcgetattr(this->serial_fd,&config)==-1) if((error=ioctl(this->serial_fd,TCGETS2,&config))<0)
{ {
/* handle exceptions */ /* handle exceptions */
throw CRS232Exception(_HERE_,"Impossible to get the attribute structure.\n",this->comm_id); throw CRS232Exception(_HERE_,"Impossible to execute the IOCTL to get the configuration structure.\n",this->comm_id);
} }
else else
{ {
switch(baud) config.c_cflag &= ~CBAUD;
{ config.c_cflag |= BOTHER;
case 50: baudrate=B50; config.c_ispeed = baud;
break; config.c_ospeed = baud;
case 75: baudrate=B75;
break;
case 110: baudrate=B110;
break;
case 134: baudrate=B134;
break;
case 150: baudrate=B150;
break;
case 200: baudrate=B200;
break;
case 300: baudrate=B300;
break;
case 600: baudrate=B600;
break;
case 1200: baudrate=B1200;
break;
case 1800: baudrate=B1800;
break;
case 2400: baudrate=B2400;
break;
case 4800: baudrate=B4800;
break;
case 9600: baudrate=B9600;
break;
case 19200: baudrate=B19200;
break;
case 38400: baudrate=B38400;
break;
case 57600: baudrate=B57600;
break;
case 115200: baudrate=B115200;
break;
case 230400: baudrate=B230400;
break;
case 460800: baudrate=B460800;
break;
case 921600: baudrate=B921600;
break;
default: /* handle exception */
throw CRS232Exception(_HERE_,"Invalid baudrate. See the documentation for the possible values.\n",this->comm_id);
break;
}
cfsetispeed(&config,baudrate);
cfsetospeed(&config,baudrate);
this->serial_config.baud=baud; this->serial_config.baud=baud;
if(tcsetattr(this->serial_fd,TCSANOW,&config)==-1) if((error=ioctl(this->serial_fd,TCSETS2,&config))<0)
{ {
/* handle exceptions */ /* handle exceptions */
throw CRS232Exception(_HERE_,"Impossible to set up the new attribute structure.\n",this->comm_id); throw CRS232Exception(_HERE_,"Impossible to execute the IOCTL to set the configuration structure.\n",this->comm_id);
} }
} }
} }
void CRS232::set_num_bits(char num_bits) void CRS232::set_num_bits(char num_bits)
{ {
struct termios config; struct termios2 config;
int bits; int error,bits;
if(tcgetattr(this->serial_fd,&config)==-1) if((error=ioctl(this->serial_fd,TCGETS2,&config))<0)
{ {
/* handle exceptions */ /* handle exceptions */
throw CRS232Exception(_HERE_,"Impossible to get the attribute structure.\n",this->comm_id); throw CRS232Exception(_HERE_,"Impossible to execute the IOCTL to get the configuration structure.\n",this->comm_id);
} }
else else
{ {
...@@ -110,22 +66,23 @@ void CRS232::set_num_bits(char num_bits) ...@@ -110,22 +66,23 @@ void CRS232::set_num_bits(char num_bits)
config.c_cflag&=~CSIZE; config.c_cflag&=~CSIZE;
config.c_cflag|=bits; config.c_cflag|=bits;
this->serial_config.num_bits=num_bits; this->serial_config.num_bits=num_bits;
if(tcsetattr(this->serial_fd,TCSANOW,&config)==-1) if((error=ioctl(this->serial_fd,TCSETS2,&config))<0)
{ {
/* handle exceptions */ /* handle exceptions */
throw CRS232Exception(_HERE_,"Impossible to set up the new attribute structure.\n",this->comm_id); throw CRS232Exception(_HERE_,"Impossible to execute the IOCTL to set the configuration structure.\n",this->comm_id);
} }
} }
} }
void CRS232::set_parity(parity_type parity) void CRS232::set_parity(parity_type parity)
{ {
struct termios config; struct termios2 config;
int error;
if(tcgetattr(this->serial_fd,&config)==-1) if((error=ioctl(this->serial_fd,TCGETS2,&config))<0)
{ {
/* handle exception */ /* handle exceptions */
throw CRS232Exception(_HERE_,"Impossible to get the attribute structure.\n",this->comm_id); throw CRS232Exception(_HERE_,"Impossible to execute the IOCTL to get the configuration structure.\n",this->comm_id);
} }
else else
{ {
...@@ -142,23 +99,24 @@ void CRS232::set_parity(parity_type parity) ...@@ -142,23 +99,24 @@ void CRS232::set_parity(parity_type parity)
} }
else config.c_cflag&=~PARENB; else config.c_cflag&=~PARENB;
this->serial_config.parity=parity; this->serial_config.parity=parity;
if(tcsetattr(this->serial_fd,TCSANOW,&config)==-1) if((error=ioctl(this->serial_fd,TCSETS2,&config))<0)
{ {
/* handle exceptions */ /* handle exceptions */
throw CRS232Exception(_HERE_,"Impossible to set up the new attribute structure.\n",this->comm_id); throw CRS232Exception(_HERE_,"Impossible to execute the IOCTL to set the configuration structure.\n",this->comm_id);
} }
} }
} }
void CRS232::set_stop_bits(char stop_bits) void CRS232::set_stop_bits(char stop_bits)
{ {
struct termios config; struct termios2 config;
int error;
if(tcgetattr(this->serial_fd,&config)==-1) if((error=ioctl(this->serial_fd,TCGETS2,&config))<0)
{ {
/* handle exceptions */ /* handle exceptions */
throw CRS232Exception(_HERE_,"Impossible to get the attribute structure.\n",this->comm_id); throw CRS232Exception(_HERE_,"Impossible to execute the IOCTL to get the configuration structure.\n",this->comm_id);
} }
else else
{ {
if(stop_bits==2) config.c_cflag|=CSTOPB; if(stop_bits==2) config.c_cflag|=CSTOPB;
...@@ -169,10 +127,10 @@ void CRS232::set_stop_bits(char stop_bits) ...@@ -169,10 +127,10 @@ void CRS232::set_stop_bits(char stop_bits)
throw CRS232Exception(_HERE_,"Invalid number of stop bits. See the documentation for the possible values.\n",this->comm_id); throw CRS232Exception(_HERE_,"Invalid number of stop bits. See the documentation for the possible values.\n",this->comm_id);
} }
this->serial_config.stop_bits=stop_bits; this->serial_config.stop_bits=stop_bits;
if(tcsetattr(this->serial_fd,TCSANOW,&config)==-1) if((error=ioctl(this->serial_fd,TCSETS2,&config))<0)
{ {
/* handle exception */ /* handle exceptions */
throw CRS232Exception(_HERE_,"Impossible to set up the new attribute structure.\n",this->comm_id); throw CRS232Exception(_HERE_,"Impossible to execute the IOCTL to set the configuration structure.\n",this->comm_id);
} }
} }
} }
...@@ -180,7 +138,8 @@ void CRS232::set_stop_bits(char stop_bits) ...@@ -180,7 +138,8 @@ void CRS232::set_stop_bits(char stop_bits)
void CRS232::hard_open(void *comm_dev) void CRS232::hard_open(void *comm_dev)
{ {
std::string *serial_dev=(std::string *)comm_dev; std::string *serial_dev=(std::string *)comm_dev;
struct termios config; struct termios2 config;
int error;
if(serial_dev->size()==0) if(serial_dev->size()==0)
{ {
...@@ -197,10 +156,10 @@ void CRS232::hard_open(void *comm_dev) ...@@ -197,10 +156,10 @@ void CRS232::hard_open(void *comm_dev)
} }
else else
{ {
if(tcgetattr(this->serial_fd,&config)==-1) if((error=ioctl(this->serial_fd,TCGETS2,&config))<0)
{ {
/* handle exceptions */ /* handle exceptions */
throw CRS232Exception(_HERE_,"Impossible to get the attribute structure.\n",this->comm_id); throw CRS232Exception(_HERE_,"Impossible to execute the IOCTL to get the configuration structure.\n",this->comm_id);
} }
else else
{ {
...@@ -213,10 +172,10 @@ void CRS232::hard_open(void *comm_dev) ...@@ -213,10 +172,10 @@ void CRS232::hard_open(void *comm_dev)
config.c_cc[VMIN]=0; config.c_cc[VMIN]=0;
config.c_cc[VTIME]=100; config.c_cc[VTIME]=100;
this->serial_device=*serial_dev; this->serial_device=*serial_dev;
if(tcsetattr(this->serial_fd,TCSANOW,&config)==-1) if((error=ioctl(this->serial_fd,TCSETS2,&config))<0)
{ {
/* handle exceptions */ /* handle exceptions */
throw CRS232Exception(_HERE_,"Impossible to set up the new attribute structure.\n",this->comm_id); throw CRS232Exception(_HERE_,"Impossible to execute the IOCTL to set the configuration structure.\n",this->comm_id);
} }
} }
} }
...@@ -249,7 +208,13 @@ int CRS232::hard_write(unsigned char *data, int len) ...@@ -249,7 +208,13 @@ int CRS232::hard_write(unsigned char *data, int len)
{ {
int num_written=0; int num_written=0;
tcdrain(this->serial_fd); if(ioctl(this->serial_fd,TCSBRK,1)!=0)
{
// static int tcsb_warned=0;
// if (!tcsb_warned) fprintf(stderr, "TCSBRK doesn't work!\n");
// tcsb_warned=1;
}
//tcdrain(this->serial_fd);
if((num_written=::write(this->serial_fd,data,len))==-1) if((num_written=::write(this->serial_fd,data,len))==-1)
{ {
return -1; return -1;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <termios.h> #include <asm/termbits.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment