示例#1
0
bool
CAI302::SetBaudRate(Port &port, unsigned baud_rate, OperationEnvironment &env)
{
  unsigned n = ConvertBaudRate(baud_rate);
  if (n == 0)
    return false;

  char cmd[16];
  sprintf(cmd, "BAUD %u\r", n);
  return SendCommandQuick(port, cmd, env);
}
示例#2
0
int OpenSerialPort(int *fd,int iflow,struct termios *oldtio,char *sName,int nBauds,int parity,int nBits,int nStop)
{
	int Of_Baud;
	int Of_Par;
	int Of_Bits;
	int Of_Stop;
	int HardFlow=0;
	int SoftFlow=0;
	struct termios newtio;

  switch(iflow)
	{
		case 1: HardFlow=CRTSCTS; break;
		case 2: SoftFlow=IXON | IXOFF | IXANY; break;
		case 3: HardFlow=CRTSCTS; SoftFlow=IXON | IXOFF | IXANY; break;
	}

	if ( (Of_Baud = ConvertBaudRate(nBauds)) == -1 ) return 1;
	if ( (Of_Par = ConvertParity(parity)) == -1 ) return 2;
	if ( (Of_Bits = ConvertDataBits(nBits)) == -1 ) return 3;
	if ( (Of_Stop = ConvertStopBits(nStop)) == -1 ) return 4;

  *fd=open(sName,O_RDWR | O_NOCTTY | O_NDELAY );
	if ( (*fd) < 0 ) return 5;
	if (oldtio)
	{
		if ( tcgetattr (*fd,oldtio) == -1)
		{
			close(*fd);
			return 6;
		}
	}

	if (tcgetattr (*fd,&newtio) == -1 )
	{
		close(*fd);
		return 6;
	}
	// cleaning default options
	newtio.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | CRTSCTS);
	newtio.c_iflag &= ~( INPCK | ISTRIP | IGNPAR | IXON | IXOFF | IXANY | ICRNL | INLCR );
	newtio.c_lflag &= ~( ICANON | ECHO | ECHOE | ISIG );
	// setting options
	newtio.c_cflag=  Of_Bits | Of_Stop | Of_Par | CLOCAL | CREAD | HUPCL | HardFlow;

	if ( Of_Par & PARENB )
		newtio.c_iflag |= INPCK; // Why stripping the eight bit when parity is enabled?? | ISTRIP;
	else
		newtio.c_iflag |= IGNPAR;

	newtio.c_iflag |= SoftFlow;
	
	newtio.c_oflag=0;
	
	newtio.c_cc[VMIN]=1;
	newtio.c_cc[VTIME]=1;
	newtio.c_cc[VSTART]=17; //DC1;
	newtio.c_cc[VSTOP]=19;  //DC3;
	
	cfsetispeed(&newtio,Of_Baud);
	cfsetospeed(&newtio,Of_Baud);
	tcflush(*fd,TCIFLUSH);
	if ( tcsetattr(*fd,TCSANOW,&newtio) == -1)
	{
		close(*fd);
		return 7;
	}

	return 0;
}