1

I'm trying to communicate with a serial port using boost/asio. This is my code.

The calling function header file contains this:

    QserialPort *SerialPort;

The calling function:

    std::string port = "com1";
    SerialPort = new QserialPort(port);

The class header file contains this code:

    /* 
    * File:   QserialPort.h
    *
    * Created on December 13, 2012, 9:01 PM
    */

    #ifndef QSERIALPORT_H
    #define QSERIALPORT_H

    #include "global_variables.h"
    #include <windows.h>
    #include <winbase.h>
    #include <winsock2.h>
    #include <boost/system/error_code.hpp>
    #include <boost/system/system_error.hpp>
    #include <boost/asio.hpp>
    #include <boost/asio/serial_port.hpp>
    #include <QString>

    #define rcvBufSize 512
    #define xmtBufSize 512

    class QserialPort {
    public:
        QserialPort(std::string comPort);
        virtual ~QserialPort();
    private:
        // variables
        int m_a;
        double m_b;
        const char* m_c;
        boost::asio::io_service m_io;
        boost::asio::serial_port m_serialPort;
        char m_rcvMsg[rcvBufSize];
        char m_xmtMsg[xmtBufSize];

        // functions

    };

    #endif   /* QSERIALPORT_H */

The class source code has this constructor:

    QserialPort::QserialPort(std::string comPort) :
    m_io(),
    m_serialPort(m_io, comPort)
    {
        QString msg;
        char buf[100];
        sprintf(buf, "Serial port is on %s", comPort);
        msg.append(buf);
        QMessageBox::information(NULL, "debug", msg);
    }

The program compiles OK, but when I try to run it I get this error in a pop up window:

This application has requested the Runtime to teminate it in an unusual way.
Please contact the application's support team for more information.

This is what is in the run output tab of netbeans:

terminate called after throwing an instance of 
'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector
<boost::system::system_error> >'
  what():  open: The system cannot find the file specified
Unable to open SCSI controller 0:[\] , error code: 0x5
Unable to open SCSI controller 1:[\] , error code: 0x5

RUN FAILED (exit value 3, total time: 15s)

These are the examples I am following:

My system:

Windows Vista Home Premium Service Pack 2
Netbeans 7.2.1
QtSDK 1.2.1
Boost 1.52.0

What do I need to do to get this to run.

2
  • 1
    Are you sure that "com1" is present in your system? Dec 29, 2012 at 18:00
  • 1
    You don't show your call to open, but it appears you are using the versions of asio functions that throw exceptions when they fail. In this case, it is failing to open the specified device (com1).
    – Chad
    Dec 31, 2012 at 15:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.