【问题标题】:Unable to migrate simple read/write serial port code from boost::asio::serial_port to QSerialPort无法将简单的读/写串口代码从 boost::asio::serial_port 迁移到 QSerialPort
【发布时间】:2014-11-14 11:34:47
【问题描述】:

我需要一段非常简单的代码来连接串行端口(连接自制设备的地方),向其中写入一些数据,然后读取回复。写入的数据使设备上的 LED 亮起(便于查看 PC 发送的数据是否被设备实际接收)

我让它与boost/asio 一起工作,需要将它移植到QSerialPort。使用QSerialPort,我可以连接、发送数据(我知道它已发送,因为 LED 亮起),但无法接收任何...

这里是 boost asio 代码的工作:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/serial_port_base.hpp>
#include <boost/thread.hpp>

static std::vector<char> sReceived;
void readThread( boost::asio::serial_port* port )
{
    try
    {
        char read;
        while ( boost::asio::read( *port, boost::asio::buffer(&read,1) ) == 1 )
            sReceived.push_back( read );
    }
    catch (...)
    {
        // connection most likely closed
    }
}

int main( int argc, char* argv[] )
{
    int res = 1;
    if ( argc != 2 )
    {
        std::cout << "Specify COM port name as first and unic parameter" << std::endl;
    }
    else
    {
        try
        {
            std::string portName = argv[1];

            boost::asio::io_service ioservice;
            boost::asio::serial_port port( ioservice );

            port.open( portName );
            port.set_option( boost::asio::serial_port_base::baud_rate( 921600 ) );
            port.set_option( boost::asio::serial_port_base::parity( boost::asio::serial_port_base::parity::none ) );
            port.set_option( boost::asio::serial_port_base::stop_bits( boost::asio::serial_port_base::stop_bits::one ) );
            port.set_option( boost::asio::serial_port_base::character_size( 8 ) );
            port.set_option( 
                boost::asio::serial_port_base::flow_control( 
                    boost::asio::serial_port_base::flow_control::hardware ) );

            if ( port.is_open() )
            {
                boost::thread thrd( readThread, &port );

                static const size_t requestMessageSize = 10;
                unsigned char request[requestMessageSize] = { 0x01, 0x00, 0x07, 0x01, 0x01, 0x00, 0x00, 0xBE, 0x0B, 0x00 };
                if ( boost::asio::write( port,
                                         boost::asio::buffer(request,requestMessageSize) ) == requestMessageSize )
                {
                    boost::this_thread::sleep( boost::posix_time::milliseconds( 1000 ) );

                    if ( !sReceived.empty() )
                    {
                        std::cout << "Received:" << std::hex;
                        for ( size_t i = 0; i != sReceived.size(); ++i )
                        {
                            std::cout << " 0x" << static_cast<int>( sReceived[i] );
                        }
                        std::cout << std::endl;
                        std::cout << "Could open port, send and receive data" << std::endl;
                        res = 0;
                    }
                    else
                    {
                        std::cout << "Could open port, send data, but did not received any reply" << std::endl;
                    }
                }
                else
                {
                    std::cout << "Could not send data" << std::endl;
                }

                port.close();
            }
            else
            {
                std::cout << "Could not open connection with " << portName << std::endl;
            }
        }
        catch (...)
        {
            std::cout << "Exception raised by boost asio" << std::endl;
        }
    }

    return res;
}

这是 Qt 代码(不工作):

#include <QApplication>
#include <QThread>
#include <QMessageBox>
#include <QtSerialPort/QSerialPort>

#include <sstream>

int main( int argc, char* argv[] )
{
    QApplication app(argc, argv);

    int res = 1;
    if ( argc != 2 )
    {
        QMessageBox::critical( NULL, "Invalid argument", "Specify COM port name as first and unic parameter" );
    }
    else
    {
        QString portName = argv[1];

        QSerialPort port( portName );

        if ( port.open( QSerialPort::ReadWrite ) )
        {
            if ( port.setBaudRate( 921600 ) &&
                 port.setFlowControl( QSerialPort::HardwareControl ) &&
                 port.setStopBits( QSerialPort::OneStop ) &&
                 port.setParity( QSerialPort::NoParity ) &&
                 port.setDataBits( QSerialPort::Data8 ) )
            {
                static const size_t requestMessageSize = 10;
                char request[requestMessageSize] = { 0x01, 0x00, 0x07, 0x01, 0x01, 0x00, 0x00, 0xBE, 0x0B, 0x00 };
                if ( port.write( request, requestMessageSize ) == requestMessageSize )
                {
                    QThread::sleep( 1 );

                    QByteArray reply = port.readAll();

                    if ( !reply.isEmpty() )
                    {
                        std::stringstream str;
                        str << "Received:" << std::hex;
                        for ( size_t i = 0; i != reply.size(); ++i )
                        {
                            str << " 0x" << static_cast<int>( reply.at(i) );
                        }
                        str << std::endl;
                        str << "Could open port, send and receive data" << std::endl;
                        QMessageBox::information( NULL, "OK", str.str().c_str() );
                        res = 0;
                    }
                    else
                    {
                        QMessageBox::critical( NULL, "Error", "Could open port, send data, but did not received any reply" );
                    }
                }
                else
                {
                    QMessageBox::critical( NULL, "Error", "Unable to send request to port" );
                }
            }
            else
            {
                QMessageBox::critical( NULL, "Error", "Unable to configure port" );
            }
            port.close();
        }
        else
        {
            QMessageBox::critical( NULL, "Unable to connect", QString("Could not open connection with %1").arg( portName ) );
        }
    }

    return res;
}

当我使用作为参数传递的好端口名称运行这两个代码时:

  • boost 的一个点亮 LED 并报告Received: 0x.....Could open port, send and receive data
  • Qt 的一个点亮 LED,但报告 Could open port, send data, but did not received any reply

我的 QSerialPort 代码有什么问题?为什么能发送数据却收不到数据?

【问题讨论】:

    标签: c++ qt boost serial-port boost-asio


    【解决方案1】:

    port.readAll() 在读取某些内容之前不会阻塞,实际上 没有数据 在您返回事件循环或执行waitForReadyRead 接收数据和waitForBytesWritten 写出缓冲区。

    所以用port.waitForBytesWritten(1000);port.waitForReadyRead(1000);替换Thread::sleep(1)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多