arash javanmard
arash javanmard

Reputation: 1387

libserial readsome returns strange value (not error)

my OS: Ubuntu 14.04

IDE: Ecplipse Mars

Libs: GCC, libserial

i am using libserial to communicate via serial-bus.

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

/*****************************************************************************/
int main(int argc, char** argv)
{
    using namespace LibSerial ;
    SerialStream serial_port ;
    serial_port.Open("/dev/ttyACM0") ;

    if ( ! serial_port.good() )
    {
        std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
                  << "Error: Could not open serial port."
                  << std::endl ;
        exit(1) ;
    }

    serial_port.SetBaudRate( SerialStreamBuf::BAUD_115200 ) ;
    if ( ! serial_port.good() )
    {
        std::cerr << "Error: Could not set the baud rate." << std::endl ;
        exit(1) ;
    }

    serial_port.SetCharSize( SerialStreamBuf::CHAR_SIZE_8 ) ;
    if ( ! serial_port.good() )
    {
        std::cerr << "Error: Could not set the character size." << std::endl ;
        exit(1) ;
    }

    serial_port.SetParity( SerialStreamBuf::PARITY_NONE ) ;
    if ( ! serial_port.good() )
    {
        std::cerr << "Error: Could not disable the parity." << std::endl ;
        exit(1) ;
    }

    serial_port.SetNumOfStopBits( 1 ) ;
    if ( ! serial_port.good() )
    {
        std::cerr << "Error: Could not set the number of stop bits."
                  << std::endl ;
        exit(1) ;
    }

    serial_port.SetFlowControl( SerialStreamBuf::FLOW_CONTROL_NONE ) ;
    if ( ! serial_port.good() )
    {
        std::cerr << "Error: Could not use hardware flow control."
                  << std::endl ;
        exit(1) ;
    }

    while(run)
    {
        int inCount = serial_port.rdbuf()->in_avail();
        if (inCount > 0)
        {
            char buffer[1024];
             // here is the problem     
            int nbytes = serial_port.readsome(buffer, inCount);
            if (!serial_port.good())
            {
                std::cerr << "Error: readsome";
            }

            serial_port.write(buffer, strlen(buffer));
            std::cerr << "read: " << nbytes << "available: " << inCount << "\n";
        }
    }

    serial_port.Close();
    std::cerr << std::endl ;
    return EXIT_SUCCESS ;
}

the point

readsome()

reads correctly and fills the buffer, but its return value is not as expeted the number of read values. it alway returns "6394433".

Any idea what is going wrong here?

Upvotes: 1

Views: 240

Answers (0)

Related Questions