Question

I have quite a headache about this seemingly easy task: send a break signal to my device, like the wxTerm (or any similar Terminal application) does.

This signal has to be 125ms long, according to my tests and the devices specification.

It should result in a specific response, but what I get is a longer response than expected, and the transmitted date is false.

e.g.:

what it should respond 08 00 81 00 00 01 07 00

what it does respond 08 01 0A 0C 10 40 40 07 00 7F

What really boggles me is, that after I have used wxTerm to look at my available com-ports (without connecting or sending anything), my code starts to work! I can send then as many breaks as I like, I get my response right from then on. I have to reset my PC in order to try it again.

What the heck is going on here?!

Here is my code for a reset through a break-signal:

 minicom_client(boost::asio::io_service& io_service, unsigned int baud, const string& device)
            : active_(true),
              io_service_(io_service),
              serialPort(io_service, device)
    {
        if (!serialPort.is_open())
        {
                cerr << "Failed to open serial port\n";
                return;
        }           
        boost::asio::serial_port_base::flow_control FLOW( boost::asio::serial_port_base::flow_control::hardware );
        boost::asio::serial_port_base::baud_rate baud_option(baud);
        serialPort.set_option(FLOW);
        serialPort.set_option(baud_option);
        read_start();
        std::cout << SetCommBreak(serialPort.native_handle()) << std::endl;     
        std::cout << GetLastError() << std::endl;
        boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time();
        boost::this_thread::sleep(boost::posix_time::millisec(125));
        boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time();    
        std::cout << ClearCommBreak(serialPort.native_handle()) << std::endl;       
        std::cout << GetLastError() << std::endl;           
        boost::posix_time::time_duration msdiff = mst2 - mst1;
        std::cout << msdiff.total_milliseconds() << std::endl;
    }

Edit:

It was only necessary to look at the combo-box selection of com-ports of wxTerm - no active connection was needed to be established in order to make my code work.

I am guessing, that there is some sort of initialisation missing, which is done, when wxTerm is creating the list for the serial-port combo-box.

Was it helpful?

Solution

After looking into this more deeply I found out, that I had to setup character_size properly, in order for this to work.

However I did not really forget this, it was just not necessary until now. When I had my devices already in a post-reset state, they would send their data according to my request - so everything was fine and I only had to specifiy the baud_rate.

Because no active flow control is an often used default, I thought, here might be some error. However it is really only necessary to setup character_size. Then the break-signal is responded with the expected answer.

This is the minimal setup:

minicom_client(boost::asio::io_service& io_service, unsigned int baud, const string& device)
        : active_(true),
          io_service_(io_service),
          serialPort(io_service, device)
{
    if (!serialPort.is_open())
    {
            cerr << "Failed to open serial port\n";
            return;
    }           
    boost::asio::serial_port_base::character_size CSIZE( 8 );
    boost::asio::serial_port_base::baud_rate baud_option(baud);
    serialPort.set_option( CSIZE );
    serialPort.set_option(baud_option);
    read_start();
    std::cout << SetCommBreak(serialPort.native_handle()) << std::endl;     
    std::cout << GetLastError() << std::endl;
    boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time();
    boost::this_thread::sleep(boost::posix_time::millisec(125));
    boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time();    
    std::cout << ClearCommBreak(serialPort.native_handle()) << std::endl;       
    std::cout << GetLastError() << std::endl;           
    boost::posix_time::time_duration msdiff = mst2 - mst1;
    std::cout << msdiff.total_milliseconds() << std::endl;
}

However, just to be sure, I am setting up all the serial port parameters now.

The example file found here Boost Asio serial_port - need help with io helped on the way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top