int set_up_comms( char *device, int baud_i, char *parity, int stopb )
{
int ttyfd;
struct termios settings;
int char_interval_timeout;
speed_t baud_rate;
#if QNX4 | QNX6
if(( ttyfd = open( device,(O_RDWR | O_NOCTTY | O_NONBLOCK | O_BINARY) ) ) < 0 )
{
printf("ERROR: set_up_comms: %s\n",strerror(errno));
return(-1);
}
#else
if(( ttyfd = open( device,(O_RDWR | O_NOCTTY | O_NONBLOCK) ) ) < 0 )
//if(( ttyfd = open( device,(O_RDWR | O_NOCTTY & ~O_NONBLOCK) ) ) < 0 )
{
printf("ERROR: set_up_comms: device=%s: %s\n",device, strerror(errno));
return(-1);
}
#endif
tcgetattr(ttyfd,&settings);
cfmakeraw(&settings);
#if DEBUG
fprintf( stderr, "opening %s\n", device );
#endif
switch( baud_i )
{
case 110:
baud_rate = B110;
char_interval_timeout = TO_B110;
break;
case 300:
baud_rate = B300;
char_interval_timeout = TO_B300;
break;
case 600:
baud_rate = B600;
char_interval_timeout = TO_B600;
break;
case 1200:
baud_rate = B1200;
char_interval_timeout = TO_B1200;
break;
case 2400:
baud_rate = B2400;
char_interval_timeout = TO_B2400;
break;
case 4800:
baud_rate = B4800;
char_interval_timeout = TO_B4800;
break;
case 9600: case 0:
baud_rate = B9600; //baud_rate = B9600;
char_interval_timeout = TO_B9600;
//char_interval_timeout = 9600;
break;
case 19200:
baud_rate = B19200;
char_interval_timeout = TO_B19200;
break;
case 38400:
baud_rate = B38400;
char_interval_timeout = TO_B38400;
break;
case 57600:
baud_rate = B57600;
char_interval_timeout = TO_B57600;
break;
case 115200:
baud_rate = B115200;
char_interval_timeout = TO_B115200;
break;
default:
baud_rate = B9600;
char_interval_timeout = TO_B9600;
fprintf(stderr, "Unknown baud rate %d for %s.", baud_i, device);
}
cfsetispeed( &settings, baud_rate );/* Set the baud rate */
cfsetospeed( &settings, baud_rate );
#if DEBUG
fprintf( stderr, "%s open\n", device );
#endif
settings.c_lflag &= ~ICANON;//raw mode
settings.c_cflag &= ~HUPCL;
settings.c_cflag &= ~CLOCAL;
#if QNX4 | QNX6
settings.c_cflag &= ~IHFLOW;
settings.c_cflag &= ~OHFLOW;
//---Off Flow Control Flags
settings.c_qflag &= ~(TC_PROTECT_HFLOW|TC_PROTECT_SFLOW|TC_WAIT_SFLOW|TC_ECHOI);
//---Set Flag -ohpaged
tcflow(ttyfd, TCOONHW);
#endif
settings.c_cc[VMIN] = 1;
settings.c_cc[VTIME] = 0;
settings.c_cflag &=~ CSIZE;
settings.c_cflag |= CS8;
if(stopb==1)
{ settings.c_cflag &=~ CSTOPB; }//one STOP BIT
else
{ settings.c_cflag |= CSTOPB; }//two STOP BIT
if( strncmp( parity, "none", 4 ) == 0 )
{
settings.c_cflag &=~ PARENB;
settings.c_cflag &=~ PARODD;
}
else
if( strncmp( parity, "even", 4 ) == 0 )
{
settings.c_iflag |= (INPCK);
settings.c_iflag &= ~(IGNPAR | ISTRIP | PARMRK);
settings.c_cflag |= PARENB;
settings.c_cflag &=~ PARODD;
}
else
{
settings.c_iflag |= (INPCK);
settings.c_iflag &= ~(IGNPAR | ISTRIP | PARMRK);
settings.c_cflag |= PARENB;
settings.c_cflag |= PARODD;
}
if( tcsetattr( ttyfd, TCSANOW, &settings ) < 0 )
{
fprintf( stderr, "tcsetattr failed: %s\n",strerror(errno));
}
//tcflush(ttyfd, TCIOFLUSH);
dtr_off(ttyfd);
close(ttyfd);
#if QNX4 | QNX6
if(( ttyfd = open( device,(O_RDWR | O_NOCTTY | O_NONBLOCK | O_BINARY) ) ) < 0 )
{
printf("ERROR: set_up_comms: %s\n",strerror(errno));
return(-1);
}
#else
if(( ttyfd = open( device,(O_RDWR | O_NOCTTY | O_NONBLOCK) ) ) < 0 )
//if(( ttyfd = open( device,(O_RDWR | O_NOCTTY & ~O_NONBLOCK) ) ) < 0 )
{
printf("ERROR: set_up_comms: device=%s: %s\n",device, strerror(errno));
return(-1);
}
#endif
return( ttyfd );
}
Если у тебя будет время напиши статью плиз...