Home > OS >  A serial port problem setup serialport bad file descriptor
A serial port problem setup serialport bad file descriptor

Time:12-25

[shows the setup serialport 1: bad file descriptor where wrong oh] [
# include "serialPort. H"
# include & lt; string.h>
# include & lt; Sys/ioctl. H>
using namespace std;
SerialPort: : serialPort ()
{
Fd=1;
}
Bool serialPort: : OpenPort (const char * dev)
{

Char * _dev=new char [32].
Strcpy (_dev, dev);
Fd=open (_dev, O_RDWR | O_NOCTTY | O_NDELAY);//| O_NOCTTY | O_NDELAY
If (1==fd)
{
Perror (" Can 't Open Serial Port ");
return false;
}

Int DTR_flag;
DTR_flag=TIOCM_DTR;
The ioctl (fd, TIOCMBIS, & amp; DTR_flag);//Set the RTS pin
return true;

}
Int serialPort: : setup (int speed, int flow_ctrl, int databits, int stopbits, int parity)
{

Int I;
Int the status;
Struct termios options;
/* tcgetattr (fd, & amp; Options) with fd to objects related parameters, and save them in the options, this function can also test the configuration is correct, the serial port is available, etc., if the call is successful, the function returns a value of 0, if the call fails, the function returns a value of 1.
*/
If (tcgetattr (fd, & amp; The options).=0)
{
Perror (" SetupSerial 1 ");
Return (false);
}
Int speed_arr [14]={B38400 B19200, B9600, B4800, B2400, B1200, B300,
B38400 B19200, B9600 B4800, B2400, B1200, B300,};
Int name_arr [14]={38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300,};
//set the serial port baud rate, the input and output baud rate
for (i=0; I & lt; Sizeof (speed_arr)/sizeof (int); I++)
{
If (speed==name_arr [I])
{
Cfsetispeed (& amp; The options, speed_arr [I]);
Cfsetospeed (& amp; The options, speed_arr [I]);
}
}

//modify the control mode to ensure that the program does not use a serial port
Options. C_cflag |=CLOCAL;
//modify the control mode, can be read from the serial input data
Options. C_cflag |=CREAD;

//set data flow control
The switch (flow_ctrl)
{

Case 0://do not use the flow control
Options. C_cflag & amp;=~ CRTSCTS;
break;

Case 1://using hardware flow control
Options. C_cflag |=CRTSCTS;
break;
Case 2://use the software flow control
Options. C_cflag |=IXON | IXOFF | IXANY;
break;
}
//set the data bits
//block the other sign
Options. C_cflag & amp;=~ CSIZE;
The switch (databits)
{
Case 5:
Options. C_cflag |=CS5;
break;
Case 6:
Options. C_cflag |=CS6;
break;
Case 7:
Options. C_cflag |=CS7;
break;
Case 8:
Options. C_cflag |=CS8 gives;
break;
Default:
Fprintf (stderr, "Unsupported data size \ n");
Return (false);
}
//set the parity bit
The switch (parity)
{
Case 'n' :
Case 'N' ://and white parity bit,
Options. C_cflag & amp;=~ PARENB;
Options. C_iflag & amp;=~ INPCK;
break;
Case: 'o'
Case: 'O'//set the surprising check
Options. C_cflag |=(PARODD | PARENB);
Options. C_iflag |=INPCK;
break;
Case 'e' :
Case 'E' ://set to parity
Options. C_cflag |=PARENB;
Options. C_cflag & amp;=~ PARODD;
Options. C_iflag |=INPCK;//INPCK: open the input parity
break;
In case the 's' :
Case 'S' ://set to space
Options. C_cflag & amp;=~ PARENB;
Options. C_cflag & amp;=~ CSTOPB;
break;
Default:
Fprintf (stderr, "Unsupported parity \ n");
Return (false);
}
//set the stop bit
The switch (stopbits)
{
Case 1:
Options. C_cflag & amp;=~ CSTOPB; break;
Case 2:
Options. C_cflag |=CSTOPB; break;
Default:
Fprintf (stderr, "Unsupported stop bits \ n");
Return (false);
}

//modify output mode, the raw data output
Options. C_oflag & amp;=~ OPOST;//OPOST: perform output processing;

Options. C_lflag & amp;=~ (ICANON | ECHO | ECHOE | ISIG);//ICANON: standard input; ECHO: to ECHO; ECHOE: erase visible operator; ISIG: make the signals produced by the terminal work;
//options. C_lflag & amp;=~ (ISIG | ICANON);

//set wait time and minimum received characters
Options. C_cc [VTIME]=1;/* wait for a character to read 1 * (1/10) s */
Options. C_cc [VMIN]=1;/* read characters with a minimum of 1 * number/
//if data overflow occur, receiving data, but no longer read the refresh data received but not read
Tcflush (fd, TCIFLUSH);

//active configuration (set the modified termios data to the serial port)
If (tcsetattr (fd, TCSANOW, & amp; The options).=0)//tcsetattr function is used to set related parameters of terminal
{
Perror (" com set error! \n");
Return (false);
}
Return (true);
}

Int serialPort: : readBuffer (unsigned char * buffer, int size)
{
Return the read (fd, buffer size);
}
Int serialPort: : writeBuffer (unsigned char * buffer, int size)
{
Return the write (fd, buffer size);
}
Uint8_t serialPort: : getchar ()
{
Uint8_t t;
Read (fd, & amp; T, 1);
return t;
}
Void serialPort: : ClosePort ()
{
Close (fd);
}

]
  • Related