【发布时间】:2014-09-11 21:18:43
【问题描述】:
const QSerialPortInfo* serialPortInfo = nullptr;
bool PortManager::setPort(QString portName) {
const QList<QSerialPortInfo> infoList = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo portInfo : infoList) {
if (portInfo.portName() == portName && serialPortInfo != &portInfo) {
serialPortInfo = &portInfo;
}
}
if (serialPortInfo != nullptr) {
if (portName != "" && serialPortInfo->isValid()) { //segmentation fault
if (serialPort->isOpen()) {
serialPort->close();
}
serialPort = new QSerialPort(*serialPortInfo, this);
if (serialPort->open(QIODevice::ReadWrite)) {
if (serialPort->clear()) {
if (serialPort->setBaudRate(QSerialPort::Baud38400, QSerialPort::AllDirections)
&& serialPort->setFlowControl(QSerialPort::NoFlowControl)
&& serialPort->setParity(QSerialPort::NoParity)) {
isPortSet = true;
}
.
.
.
这是我的代码,适用于 Linux 和 Windows 7。现在我正在 Windows 8 上测试它,我在 this->serialPortInfo->isValid() (以及 serialPortInfo 的任何其他函数)上遇到分段错误.任何特定 QSerialPortInfo 对象的所有数据都是“不可用的”(如调试器所述),在我看来,我没有使用它们的特权。在 Linux 上,我必须是 uupc(如果我没记错的话)组的成员才能没有这样的错误,但在 Windows 7 上我不必做任何事情。我以“管理员身份”运行 Qt Creator,但没有帮助;也许我必须以某种方式告诉它也以管理员身份运行 qmake?不过这只是我的猜测,可能原因不一样……
【问题讨论】:
-
稍微吹毛求疵,您知道 Windows 没有任何命名“分段错误”吗?
-
@JoachimPileborg:不管它有什么,它被 Qt Creator 称为“SIGSEGV: Segmentation fault”;但我在 MinGW 上工作,它基本上是一个 Linux,所以......我对 Windows 不太了解。 ;-) 它有什么改变吗?
标签: c++ windows qt qt5 qtserialport