0

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.

Jook
  • 4,564
  • 3
  • 26
  • 53
  • Here's a wild guess: Your "device" needs to be put into the appropriate state before it will respond according to the spec you're looking at. `wxTerm` puts it in that state before interrupting, your code does not. – alexis Oct 22 '12 at 13:17
  • @alexis - yes and no - it seems, that it is enought to start wxTerm and go to the selection combobox for com-ports - without connection to anything! after this, my code works for every device, and I have to reboot, in order to reconstruct this behaviour. – Jook Oct 22 '12 at 13:33
  • Then maybe it's your computer's port that needs to be put into the proper state? – alexis Oct 22 '12 at 13:53
  • @alexis yes, I think so too, but how can I find out, which state this might be?! I am already stepping through some wild ideas looking at possible methods from the microsoft communication functions reference - e.g. `FlushCommBuffers` did not help - any ideas which settings/switches could result in such a behaviour? – Jook Oct 22 '12 at 13:57
  • You might start by giving more specific information. I'm no good with hardware issues, but there are others who could help. But nobody can help you with "my device doesn't work"-- you're going to get your question closed. – alexis Oct 22 '12 at 14:05
  • @alexis ok, solved - the default value for `character_size` was wrong or not set or whatever. think I delete this question my self in a few ;) thanks for your attention, it helped diggin in the right direction – Jook Oct 22 '12 at 14:32
  • please do not delete the question, add an answer so others can search and find this in the future – Sam Miller Oct 22 '12 at 19:07

1 Answers1

0

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.

Community
  • 1
  • 1
Jook
  • 4,564
  • 3
  • 26
  • 53