我想使用Qt串口和伪终端测试一个应用程序。根据man手册,我实现了以下内容:
// open master
QSerialPort master("/dev/ptmx");
master.open(QSerialPort::ReadWrite);
int master_fd = master.handle();
// get device name of slave pseudoterminal
constexpr size_t PTSNAME_BUFFER_LENGTH = 128;
char ptsname_buffer[PTSNAME_BUFFER_LENGTH];
if (ptsname_r(master_fd, ptsname_buffer, PTSNAME_BUFFER_LENGTH) != 0)
return 0;
// grant access to the slave
if (grantpt(master_fd) != 0)
return 0;
// unlock the slave
if (unlockpt(master_fd) != 0)
return 0;
// open slave
std::cout << "Slave pseudoterminal: " << ptsname_buffer << std::endl;
QSerialPort slave(ptsname_buffer);
slave.open(QSerialPort::ReadWrite);
// test communication
master.write("Hello World");
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::cout << "Received: " << slave.readAll().toStdString() << std::endl;
创建从设备似乎是有效的(在我的情况下,它被创建在/dev/pts/2
)。然而,slave.readAll()
命令总是返回一个空字符串。
是否可能使用伪终端测试QSerialPort?