写入串行端口时Qt崩溃
Qt crashing when writing to serial port
嗨,QT中有一个程序,每当我向串行端口写入时,它似乎都会崩溃。我使用的是Mac OSx 15英寸retina。以下是相关代码:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
//set central widget for the Main Window
centralWidget = new QWidget(this);
this->setCentralWidget(centralWidget);
//creation and attribution of slider
slider = new QSlider();
slider->resize(255, 20);
slider->setOrientation(Qt::Horizontal);
slider->setRange(0, 255); //0-255 is range we can read
//layout with slider and lcd
main_layout = new QVBoxLayout();
main_layout->addWidget(slider);
//set layout to the widget inside MainWindow
centralWidget->setLayout(main_layout);
/*Connection Events*/
//connection between the slider event and the transmission function
QObject::connect(slider, SIGNAL(valueChanged(int)), this, SLOT(transmitCmd(int)));
}
void MainWindow::init_port()
{
port = new QSerialPort("COM3"); //create port
port->open(QIODevice::ReadWrite | QIODevice::Unbuffered); //open port
if(!port->isOpen())
{
QMessageBox::warning(this, "port error", "Can't open port!");
}
//set port properties
port->setBaudRate(QSerialPort::Baud9600); //9600 FOR ARDUINO
port->setFlowControl(QSerialPort::NoFlowControl);
port->setParity(QSerialPort::NoParity);
port->setDataBits(QSerialPort::Data8);
port->setStopBits(QSerialPort::OneStop);
}
void MainWindow::transmitCmd(int value)
{
//if value wasn't between 0-255, no transmission
if(value < 0 || value > 255)
return;
char *buffer = (char*) new int(value);
//send buffer to serial port
port->write(buffer);
}
它在线路端口->写入(缓冲区)上崩溃。我使用的是QT 5.5和QTSerialPort。
尝试
port->write (buffer, sizeof (int));
您使用了QIODevice::write(const char* data)
重载,它需要一个以null结尾的字符串(您的缓冲区不是)。很自然,io设备不知道何时停止。。。
无论如何,这应该可以修复您的崩溃。顺便说一句,同样可以说:
port->write (reinterpret_cast<const char*> (&value), sizeof (int))
但是,请注意,上面的两个命令都将通过您的端口发送4个字节(sizeof int)的数据(按照系统的字节顺序,可能是little endian)。也许(从你在函数开始时的0-255检查来看),这实际上不是你想要的。如果你只想发送一个字节:
unsigned char valueCh = static_cast<unsigned char> (value);
port->write (reinterpret_cast<const char*> (&valueCh), 1)
附录:
正如您所写的,您只是忘记了init调用。很好的捕获@perencia!但是,仍然值得理解为什么transmitCmd()
确实有效——因为乍一看,它不应该。
我仍然认为您使用了错误的write()
调用,但事实上,它可以正常工作。发生的是:
假设我们有value == 17
。然后,在一个小的endian体系结构上,您的缓冲区看起来是这样的:
// "17" little-endian
11 00 00 00
^
|
buffer
并且您对write(buffer)
的调用将看到您想要发送的正确数据字节,后面跟着一个导致其停止的nul字节。
您没有调用init_port
。至少在您提交的代码中是这样。
相关文章:
- Qt 图表条形图调整大小崩溃
- 在从Qt调用的Python脚本中导入OpenCV崩溃
- 在 Ubuntu 服务器上运行 QT 应用程序时崩溃
- Qt应用程序找不到第三方DLL并崩溃
- Popen使Qt调试器崩溃
- Qt 5 应用程序崩溃并出现"qLineEdit::setText"
- Qt 5.9中的QML崩溃-帮助读取堆栈跟踪
- Qt-工作线程崩溃时将cv::Mat转换为QImage
- 使用MSVC编译的Qt程序在app.exec()上崩溃
- libCURL:上传数据崩溃(Qt,SMTP)
- 需要有关在qt c ++应用程序中使用崩溃问题的建议
- 加密任务使用QT崩溃,Openssl该怎么办
- 为什么ChangeWindowMessageFilter会导致Qt崩溃
- Unix 串行端口程序与 qt 崩溃
- 通过Qt崩溃运行外部程序
- 写入串行端口时Qt崩溃
- 将字符指针设置为Int值时QT崩溃
- QDockWidget导致qt崩溃
- Qt崩溃/当我使用Qthread::sleep更新进度条时没有出现
- Qt崩溃时工作线程的删除和清理