Boost logo

Boost Users :

Subject: Re: [Boost-users] [ASIO] Synchronous write on serial_port
From: Daniele Barzotti (daniele.barzotti_at_[hidden])
Date: 2009-04-08 05:59:33


Daniele Barzotti wrote:
>
> Hi Igor,
> first thanks for your reply. I wrong to put a file the the zip. The one
> in attach should compile also with _USE_SERIAL_OK.
> But anyway...using that directive, the code use the mini_com client
> example found on the net and, of course, it works.
> So starting from this, I've substituted it with my Serial class and the
> write on COM is extremely slow.
> When I've said I've isolated the problem I would say that the issue is
> in my serial class wrapper (Serial.cpp) and not in the others classes.

Ok, I've found the problem.

In SerialPort::Open() I've deleted the line:

SetSerialDCS(&_dcs);

which is :
-------------------------------------------------------------
void SerialPort::SetSerialDCS(SerialDCS *DCS)
{
  memcpy(&_dcs, DCS, sizeof(SerialDCS));

  try {
    if (_serialPort.is_open())
    {
      serial_port_base::baud_rate baud_option(_dcs.baud);
      _serialPort.set_option( baud_option );

      serial_port_base::baud_rate flow_option(_dcs.flowcontrol);
      _serialPort.set_option( flow_option );

      serial_port_base::baud_rate parity_option(_dcs.parity);
      _serialPort.set_option( parity_option );

      serial_port_base::baud_rate stopbits_option(_dcs.stopbits);
      _serialPort.set_option( stopbits_option );
    }
  }
  catch (...) {}
};
-------------------------------------------------------------

And now the write works ok.
But I've another issue on reading...

My reading procedure is:

SerialPort::read_start
          |
          |
SerialPort::read_complete
          |
          |
  (Start an rx timer)
          |
          |
SerialPort::Timer_Expired
          |
          |
   Raise the 'event'
     DataReveived

Ok, if I put a breakpoint on one of these levels, all works and I see
the data received, if I run the code without breakpoints the
SerialPort::read_complete is not called.

Follow the code...

//------------------------------------------------------------

void SerialPort::Post_OnData()
{
  // Raise the event
  std::cout << "SerialPort::Post_OnData" << std::endl;
  signal_OnData();
};

//------------------------------------------------------------

void SerialPort::timer_exipred(const boost::system::error_code& error)
{
  if (!error)
  {
    std::cout << "SerialPort::timer_exipred" << std::endl;
    // Timer expired-> read completed, so process the data
    _io_service.post( boost::bind(&SerialPort::Post_OnData, this) );
  }
}

//------------------------------------------------------------

void SerialPort::read_complete(const boost::system::error_code& error,
size_t bytes_transferred)
{
  // the asynchronous read operation has now completed or failed and
returned an error
  if (!error)
  {

    std::cout << "SerialPort::read_complete" << std::endl;

    // saving data to vector
    _read_buffer.append(_buf, bytes_transferred);

    //Start a new timer
    //_rx_timer.expires_from_now(
boost::posix_time::milliseconds(ReadTimeOut) );
    _rx_timer.expires_from_now( boost::posix_time::milliseconds(50) );
    // Start new asynchronous wait.
    _rx_timer.async_wait( boost::bind(&SerialPort::timer_exipred, this,
_1) );

    // start waiting for another asynchronous read again
    read_start();
  }
};

//------------------------------------------------------------

void SerialPort::read_start()
{
  std::cout << "SerialPort::read_start" << std::endl;

  // Start an asynchronous read and call read_complete when it completes
or fails
  _serialPort.async_read_some(boost::asio::buffer(_buf, m_buffer_size),
                              boost::bind(&SerialPort::read_complete,
                              this,
                              boost::asio::placeholders::error,

boost::asio::placeholders::bytes_transferred));
};
//------------------------------------------------------------

Thanks,
Daniele.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net