我需要一段非常直接的代码来连接串口(自制设备连接的地方),将一些数据写入其中,然后读取回复。写入的数据打开设备上的LED(这样可以很容易地看到PC发送的数据是否实际被设备接收)
我让它与boost/asio
合作,需要将其移植到QSerialPort
。使用QSerialPort
,我能够连接,发送数据(我知道它是因为LED打开而发送的),但是无法接收任何数据......
以下是提升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;
}
当我使用作为参数传递的良好端口名称运行两个代码时:
Received: 0x.....Could open port, send and receive data
Could open port, send data, but did not received any reply
我的QSerialPort代码出了什么问题?为什么它能够发送数据却无法接收任何数据?
答案 0 :(得分:2)
port.readAll()
在读取内容之前不会阻止,实际上在您返回事件循环或执行waitForReadyRead
之前,将无法发送或读取任何数据接收数据和waitForBytesWritten
写出缓冲区。
所以用Thread::sleep(1)
port.waitForBytesWritten(1000);port.waitForReadyRead(1000);