#include "hfile.h"
#define TO_B110 3200000 /* These values are the timeout delays */
#define TO_B300 1600000 /* at the end of packets of data. */
#define TO_B600 800000 /* At this stage a true calculation */
#define TO_B1200 400000 /* has not been worked out. So these */
#define TO_B2400 200000 /* values are just a guess. */
#define TO_B4800 100000 /* */
#define TO_B9600 50000 /* The spec says that a message frame */
#define TO_B19200 25000 /* starts after a silent interval of */
#define TO_B38400 12500 /* at least 3.5 character times. */
#define TO_B57600 8333 /* These are uS times. */
#define TO_B115200 4167
int set_up_comms( char *device, int baud_i, char *parity )
{
int ttyfd;
struct termios settings;
int char_interval_timeout;
speed_t baud_rate;
printf("set_up_comms device=%s\n",device);
if(( ttyfd = open( device,(O_RDWR | O_NOCTTY /*| O_SYNC | ~O_NONBLOCK*/) ) ) < 0 )
{
fprintf( stderr, "set_up_comms Error opening device %s.\n", device );
fprintf( stderr, "Error no. %d = %s\n", errno, strerror(errno));
exit( 1 );
}
tcgetattr(ttyfd,&settings);
#ifdef DEBUG
fprintf( stderr, "opening %s\n", device );
#endif
switch( baud_i )
{
case 110:
baud_rate = 110;
char_interval_timeout = TO_B110;
break;
case 300:
baud_rate = 300;
char_interval_timeout = TO_B300;
break;
case 600:
baud_rate = 600;
char_interval_timeout = TO_B600;
break;
case 1200:
baud_rate = 1200;
char_interval_timeout = TO_B1200;
break;
case 2400:
baud_rate = 2400;
char_interval_timeout = TO_B2400;
break;
case 4800:
baud_rate = 4800;
char_interval_timeout = TO_B4800;
break;
case 9600: case 0:
baud_rate = 9600; //baud_rate = B9600;
char_interval_timeout = TO_B9600;
//char_interval_timeout = 9600;
break;
case 19200:
baud_rate = 19200;
char_interval_timeout = TO_B19200;
break;
case 38400:
baud_rate = 38400;
char_interval_timeout = TO_B38400;
break;
case 57600:
baud_rate = 57600;
char_interval_timeout = TO_B57600;
break;
case 115200:
baud_rate = 115200;
char_interval_timeout = TO_B115200;
break;
default:
baud_rate = 9600;
char_interval_timeout = TO_B9600;
fprintf(stderr, "Unknown baud rate %d for %s.", baud_i, device);
}
#ifdef DEBUG
fprintf( stderr, "%s open\n", device );
#endif
/* read your man page for the meaning of all this. # man termios */
/* Its a bit to involved to comment here */
cfsetispeed( &settings, baud_rate );/* Set the baud rate */
cfsetospeed( &settings, baud_rate );
settings.c_cflag &=~ CSIZE;
settings.c_cflag |= CS8;
settings.c_cflag &=~ CSTOPB;
// settings.c_cflag |= CREAD;
if( strncmp( parity, "none", 4 ) == 0 )
{
settings.c_cflag &=~ PARENB;
settings.c_cflag &=~ PARODD;
}
else
if( strncmp( parity, "even", 4 ) == 0 )
{
settings.c_cflag |= PARENB;
settings.c_cflag &=~ PARODD;
}
else
{
settings.c_cflag |= PARENB;
settings.c_cflag |= PARODD;
}
// settings.c_cflag |= ~CNEW_RTSCTS;
// settings.c_cflag |= ~CRTSCTS;
// settings.c_cflag &= ~IHFLOW;
// settings.c_cflag &= ~OHFLOW;
// settings.c_cflag &= ~LKHFLOW;
settings.c_cflag &= ~HUPCL;
settings.c_cflag &= ~CLOCAL;
settings.c_cc[VMIN] = 1;
settings.c_cc[VTIME] = 0;
if( tcsetattr( ttyfd, TCSANOW, &settings ) < 0 )
{
fprintf( stderr, "tcsetattr failed\n");
exit( 1 );
}
return( ttyfd );
}