Unix 串行端口程序与 qt 崩溃
Unix serialport program with qt crashes
我正在使用qt开发一个简单的串行端口应用程序。我已经配置了ttyUSB0,并设法打开了端口。但是当从串行端口接收的数据时,我的应用程序关闭了。这是我的代码,任何人都可以知道这段代码有什么问题吗?
提前感谢,
#include "linkasunixserialport.h"
LinkasUnixSerialPort::LinkasUnixSerialPort()
{
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
qDebug()<<"open_port: Unable to open /dev/ttyO1n";
exit(1);
}
qDebug()<<"devttyUSB0 opened";
fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, O_ASYNC );
tcgetattr(fd,&termAttr);
cfsetispeed(&termAttr,B115200);
cfsetospeed(&termAttr,B115200);
termAttr.c_cflag &= ~PARENB;
termAttr.c_cflag &= ~CSTOPB;
termAttr.c_cflag &= ~CSIZE;
termAttr.c_cflag |= CS8;
termAttr.c_cflag |= (CLOCAL | CREAD);
termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
termAttr.c_oflag &= ~OPOST;
tcsetattr(fd,TCSANOW,&termAttr);
qDebug()<<"UART1 configured....n";
this->start();
}
int LinkasUnixSerialPort::bytes_available()
{
int bytesQueued;
if (::ioctl(fd, FIONREAD, &bytesQueued) == -1) {
return -1;
}
qDebug()<<bytesQueued;
return bytesQueued;
}
void LinkasUnixSerialPort::run()
{
char receive_data[2];
forever
{
int bytes_read = bytes_available();
if(bytes_read > -1)
{
int retVal = ::read(fd, receive_data, 1);
if(retVal != -1)
{
emit dataReceived(QChar(receive_data[0]));
}
}
else
{
this->msleep(1);
}
}
}
对于
此用例,最好使用 Qt 5.1 SerialPort 支持:
有关QSerialPort文档的更多信息,请参阅Qt项目主页上的示例。
相关文章:
- 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崩溃时工作线程的删除和清理