我使用 Window 7 x64 问题在于我在 PC 上使用 Arduino 发送信息,并且在读取端口时,我收到 Arduino send(0000000) PC 读取 (0000+000) 或类似的东西 (00000+00) - ( 0+000000) 等等。
例如,我将显示一个屏幕截图
和
打开端口
bool MainWindow::openPort()
{
QString strPort = LineConnect->currentText();
port = new QextSerialPort(strPort, QextSerialPort::EventDriven);
if (port->open(QIODevice::ReadWrite | QIODevice::Unbuffered) == true) {
port->setBaudRate(BAUD9600);
port->setDataBits(DATA_8);
port->setParity(PAR_NONE);
port->setStopBits(STOP_1);
port->setFlowControl(FLOW_OFF);
port->setTimeout(100);
port->waitForReadyRead(10);
port->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
connect(port,SIGNAL(readyRead()), this, SLOT(onReadyRead()));
INFO_conn->append("port "+ port->portName() + " opened..");
INFO_conn->append("port " + port->portName() + " reading..");
return true;
} else {
INFO_conn->append("port " + port->portName() + " error connection..");
}
return false;
}
读取数据
void MainWindow::onReadyRead()
{
/*first option read data*/
QByteArray msg = port->readAll();
int size = msg.size();
QString data = "Data: " + msg + " size: " + QString::number(size);
INFO_conn->append(data);
/*second option read data*/
// char data[1024];
// QTextStream out(stdout);
// out << "data received: ";
// int bytesRead = port->read(data, 1024);
// data[bytesRead] = '\0';
// out << data << " (" << bytesRead << " bytes)" << endl;
// //QString messRead = "data received: " + data +" "+ bytesRead + " bytes";
// INFO_conn->append(data);
}
为 PC 发送数据 arduino
void setup()
{
Serial.begin(9600);
.....
}
void loop()
{
Serial.print(K38);
Serial.print(K40);
Serial.print(K42);
Serial.print(K44);
Serial.print(K46);
Serial.print(K48);
Serial.print(K50);
or
Serial.print("0000000"); // result same
}
我无法理解可能有什么问题,尝试了一切。QexSerialPort 库中的示例读得太多。需要帮助。感谢您的帮助。