Serial communication and stopbits: The SerialPort class

26 views
Skip to first unread message

Elias Strandell Erstorp

unread,
Feb 4, 2015, 4:39:44 PM2/4/15
to lsts-to...@googlegroups.com
Good evening guys,

I'm trying to do something that should be relatively simple. I've created a DUNE task that is supposed to interface with a CMP10 over
a serial port to read a compass value. The task is basically very similar to the SimpleTransport task. However I need to set stopbits to equal 2.
So I'm using the SerialPort::StopBits to declare an enumeration of that type that I then wan't to set to 'SP_STOPBITS_2' and
pass to the member function setStopBits of the SerialPort instance. 

In code (cut down for readability) something like:

    struct Task: public DUNE::Tasks::Task

    {


      // Serial port handle.

      Hardware::SerialPort* m_uart;

      // Task arguments.

      Arguments m_args;


      SerialPort::StopBits StopBits;


      Task(const std::string& name, Tasks::Context& ctx):

        DUNE::Tasks::Task(name, ctx),

m_uart(NULL) // ====================== Necessary?

      {

          // Define configuration parameters.

          param("Serial Port - Device", m_args.uart_dev)

          .defaultValue("")

          .description("Serial port device used to communicate with the sensor");


          param("Serial Port - Baud Rate", m_args.uart_baud)

          .defaultValue("9600")

          .description("Serial port baud rate");

          /*

          param("Serial Port - Stop bits", m_args.StopBits)

          .defaultValue("SP_STOPBITS_2")        

          .description("Serial port stop bits");

          */


...


      //! Acquire resources.

      void

      onResourceAcquisition(void)

      {

      m_uart = new SerialPort(m_args.uart_dev, m_args.uart_baud);

      StopBits = SP_STOPBITS_2;      // gives SP_STOPBITS_2 not declared error


      m_uart->setStopBits(StopBits);

      }


...      

      sendCommand(void)

      {

      static uint8_t cmd = 0x13;

      uint8_t buf[1];

      buf[0] = cmd;

      unsigned int len = 1;

      m_uart->doWrite(buf,len);  // gives "doWrite() is private" error

      }


So what I wonder is how to solve this / how the SerialPort class is used. 
Is my approach completely wrong?

Best regards,
Elias

Pedro Calado

unread,
Feb 5, 2015, 3:51:00 AM2/5/15
to Elias Strandell Erstorp, lsts-to...@googlegroups.com
Hello there Elias,

Ok, a couple of things here may be setting you off course.

I'd first suggest to take a look at class SerialPort in src/DUNE/Hardware/Serialport.{cpp,hpp}

The constructor for this class may take the stop bits as one of the arguments, so you could therefore do:
m_uart = new SerialPort(m_args.uart_dev, m_args.uart_baud, Hardware::SerialPort::PARITY_NONE, Hardware::SerialPort::SP_STOPBITS_2);
Indeed "SP_STOPBITS_2" is giving you an error since without the namespace (as I wrote above) the compiler cannot know where to find the enumeration.

Note: setStopBits() may still be used, I just thought that using the constructor would be more elegant.

Again, if you take a look at the SerialPort class, the method "doWrite()" is indeed private and you cannot use from this task.
The correct methods for writing can be found in the Handle class in src/DUNE/IO/Handle.{cpp,hpp}, since SerialPort derives from this class: (lines 58, 59 SerialPort.hpp)
    //! The SerialPort class encapsulates serial port access.
    class SerialPort: public IO::Handle

These methods should do: (src/DUNE/IO/Handle.hpp)
 size_t
 write(const uint8_t* data, size_t length)

 size_t
 write(const char* data, size_t length)


Another important tip here is, since you couldn't figure out how to use this class, chances are other tasks in dune have used it.
So with a simple grep command you can find where it has been used:
find -name "*.hpp" -o -name "*.cpp" | xargs grep "SerialPort"
and take it from there.
Especially tasks in src/Sensors use it a lot.

I hope this has helped you :)
cheers,

Pedro Calado

--
You received this message because you are subscribed to the Google Groups "LSTS Toolchain" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lsts-toolchai...@googlegroups.com.
Visit this group at http://groups.google.com/group/lsts-toolchain.
To view this discussion on the web visit https://groups.google.com/d/msgid/lsts-toolchain/5c27b50f-f986-47d5-837a-c1caf5f4a2f1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Elias Strandell Erstorp

unread,
Feb 5, 2015, 4:27:50 AM2/5/15
to lsts-to...@googlegroups.com, moe...@gmail.com
Thanks for your thorough explanation, it feels like I learned something fundamental about C++ from your answer.
Pretty sure I can get it to work now :)

/ Elias
Reply all
Reply to author
Forward
0 new messages