int setup_serial_port( LibSerial::SerialStream& serial_port, const char* SERIAL_PORT_DEVICE, LibSerial::SerialStreamBuf::BaudRateEnum baud_rate ) { using namespace LibSerial; //Open the port SERIAL_PORT_DEVICE serial_port.Open( SERIAL_PORT_DEVICE ); if ( ! serial_port.good() ) { std::cout << "Not able to open serial port at " << SERIAL_PORT_DEVICE << std::endl; return 0; } //Set Baudrate serial_port.SetBaudRate( baud_rate ); if ( ! serial_port.good() ) { std::cout << "Unable to set Baud Rate " << std::endl; return 0; } //Set Character Size serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ); if( ! serial_port.good() ) { std::cout << "Unable to set Char Size 8" << std::endl; return 0; } //Set Parity serial_port.SetParity( SerialStreamBuf::PARITY_NONE ); if( ! serial_port.good() ) { std::cout << "Unable to set Parity None " << std::endl; return 0; } //Set Number of Stop Bits serial_port.SetNumOfStopBits( 1 ); if( ! serial_port.good() ) { std::cout << "Unable to set Num of Stop Bits 1 " << std::endl; return 0; } //Set Hardware Flow Control serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE ); if( ! serial_port.good() ) { std::cout << "Unable to set Hardware Flow Control None" << std::endl; return 0; } return 1; }
void init_serial() { // Open the serial port. const char *const SERIAL_PORT_DEVICE = "/dev/ttyACM0"; serial_port.Open(SERIAL_PORT_DEVICE); CHECK_S("Could not open serial port") //SET_AND_CHECK( SetBaudRate, BAUD_9600, "Could not set the baud rate." ); SET_AND_CHECK( SetCharSize, CHAR_SIZE_8, "Could not set the character size." ); SET_AND_CHECK( SetParity , PARITY_NONE, "Could not disable the parity." ); // Set the number of stop bits serial_port.SetNumOfStopBits(1); CHECK_S("Could not set the number of stop bits") // Turn on hardware flow control serial_port.SetFlowControl(LibSerial::SerialStreamBuf::FLOW_CONTROL_NONE); CHECK_S("Could not use hardware flow control") }