Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and Write on serial port in Ubuntu with C/C++ and LibSerial

I'm using LibSerial on Ubuntu to read and write data on serial port.

At the moment, I'm able to write and receive strings over the serial port, but my code does not work very well: in particular, I'd like to control the reading function in order to read only if there is something to read and exit when there is no information to read in order to send another command without bloicking the flow program.

I want to do:

  • Write a command
  • Wait for the answer
  • then Write another command
  • Wait for answer

Now, i'm able to send the first command and read the answer by using read function in a while loop but i'm not able to do nothing else. I'm not able to send the second command because the while loop never exits so the program continues to read.

Can you help me, please?

This is the code i'm using: (Read and write function are at the end of the code)

#include <SerialStream.h>
#include <iostream>
#include <unistd.h>
#include <cstdlib>
#include <string>

int
main( int    argc,
       char** argv  )
{
     //
     // Open the serial port.
     //
     using namespace std;
     using namespace LibSerial ;
     SerialStream serial_port ;
     char c;
     serial_port.Open( "/dev/ttyACM0" ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
                   << "Error: Could not open serial port."
                   << std::endl ;
         exit(1) ;
     }
     //
     // Set the baud rate of the serial port.
     //
     serial_port.SetBaudRate( SerialStreamBuf::BAUD_9600 ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not set the baud rate." <<  
std::endl ;
         exit(1) ;
     }
     //
     // Set the number of data bits.
     //
     serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not set the character size." <<  
std::endl ;
         exit(1) ;
     }
     //
     // Disable parity.
     //
     serial_port.SetParity( SerialStreamBuf::PARITY_NONE ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not disable the parity." <<  
std::endl ;
         exit(1) ;
     }
     //
     // Set the number of stop bits.
     //
     serial_port.SetNumOfStopBits( 1 ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not set the number of stop bits."
                   << std::endl ;
         exit(1) ;
     }
     //
     // Turn off hardware flow control.
     //
     serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE ) ;
     if ( ! serial_port.good() )
     {
         std::cerr << "Error: Could not use hardware flow control."
                   << std::endl ;
         exit(1) ;
     }
     //
     // Do not skip whitespace characters while reading from the
     // serial port.
     //
     // serial_port.unsetf( std::ios_base::skipws ) ;
     //
     // Wait for some data to be available at the serial port.
     //
     //
     // Keep reading data from serial port and print it to the screen.
     //
  // Wait for some data to be available at the serial port.
     //
     while( serial_port.rdbuf()->in_avail() == 0 )
     {
         usleep(100) ;
     }


     char out_buf[] = "check";
     serial_port.write(out_buf, 5);  <-- FIRST COMMAND
     while( 1  )
     {
         char next_byte;
         serial_port.get(next_byte);  HERE I RECEIVE THE FIRST ANSWER
         std::cerr << next_byte;

     }
     std::cerr << std::endl ;
     return EXIT_SUCCESS ;
}
like image 368
Marcus Barnet Avatar asked Jan 28 '12 16:01

Marcus Barnet


2 Answers

I think you just need to use while( serial_port.rdbuf()->in_avail() > 0 ) as a condition for your last while loop. Then it'll read out all the available data (“answer”) and you can send the second command after that.

like image 184
Michael Pankov Avatar answered Nov 10 '22 07:11

Michael Pankov


Using try and catch would be helpful. :)

Try this: A global pointer SerialPort *pu was already declared and the port was opened.

int rxstring(char *cstr, unsigned int len, bool print_str)

{

char temp=0;
int i=0;
while(temp!='\n')
{
    try
    {
        temp=pu->ReadByte(100);
    }
    catch(SerialPort::ReadTimeout &e)
    {
        //cout<<"Read Timeout"<<endl;
        return 1;
    }
    if((temp!='\n')&&(temp!=0)&&(temp!=' '))
    {
        cstr[i]=temp;
        ++i;
        //cout<<i++<<temp<<'x'<<endl;
    }
}
cstr[i]='\0';
if(print_str)
    puts(cstr);
return 0;
}

Reference: http://libserial.sourceforge.net/doxygen/class_serial_port.html#a15

like image 24
Bazooka Avatar answered Nov 10 '22 07:11

Bazooka