我正在使用Window 7 x64 问题是我用Arduino发送PC信息,当读取端口时,我收到了Arduino发送的奇怪工件(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库中的示例读取最多。 需要帮助。谢谢你的帮助。