简体   繁体   English

使用Xbee PRO S1在Arduino和Qt之间发生通信错误

[英]Communication error between Arduino and Qt using Xbee PRO S1

I've been trying to do a Lights GUI with an Arduino Mega 2560 with its Xbee Shield and two Xbee Pro S1, one connected to the Arduino and the other one to the PC. 我一直在尝试使用带有其Xbee Shield和两个Xbee Pro S1的Arduino Mega 2560进行Lights GUI,一个连接到Arduino,另一个连接到PC。 My problem is: however I can send data from Qt to my arduino and read it, i can't do the same in the other way. 我的问题是:但是我可以将数据从Qt发送到我的arduino并读取它,但我不能用其他方式做同样的事情。 When trying to send a String as "Confirmado\\r\\n", it arrives to my Qt label wrong, sometimes I get the full String, other ones I receive half of it. 尝试将字符串发送为“ Confirmado \\ r \\ n”时,它到达我的Qt标签错误,有时我会收到完整的字符串,而其他字符串我会收到一半。

My arduino code is 我的arduino代码是

char buffer[50];
String trama, dir, com, data;
int indexdir, indexcom, indexdata;

void setup(){
  Serial.begin(9600);
}

void loop(){

   trama= "Confirmado\r\n";
   const char *bf = trama.c_str();

   if(Serial.available() > 0)
   {
       Serial.readBytesUntil('/', buffer,500);
       Serial.print(bf);
       Serial.flush();
   }
}

My Qt QSerialPort config is 我的Qt QSerialPort配置是

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    serial = new QSerialPort(this);
    serial->setPortName("COM3"); //COM-port your Arduino is connected to
    serial->open(QIODevice::ReadWrite);
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
}

And I send and read data like this 我这样发送和读取数据

void MainWindow::serialReceived()
{
  QByteArray  readData = serial->readAll();
    //while (serial->waitForReadyRead(500))
        // readData.append(serial->readAll());
    ui->label->setText(readData);
}

void MainWindow::writeData(const QByteArray &data)
{
    serial->write(data);
    serial->flush();
    serial->waitForBytesWritten(500);
}

The toogle lines means I've tried both options... 竖线表示我已经尝试了两种选择...

I've noticed, doing Debug, that if I place a breakpoint in ui->label->setText(readData); 我注意到,在进行调试时,如果我在ui->label->setText(readData);放置一个断点ui->label->setText(readData); ; ; if it doesnt arrive well (the full "Confirmado\\r\\n" string), this breakpoint gets twice in this line, the first one readData equals the second half of the string (ie "mado\\r\\n") and the other one it values the rest of the string (ie "Confir"). 如果它不能很好地到达(完整的“ Confirmado \\ r \\ n”字符串),则此断点在该行中获取两次,第一个readData等于字符串的后半部分(即“ mado \\ r \\ n”),另一个其中一个对字符串的其余部分(即“ Confir”)进行赋值。

I've also tried to set a higher baudrate, 57600, but I cant send or receive any data, though I've set the baudrate in the XCTU app before. 我还尝试设置了更高的波特率57600,但我无法发送或接收任何数据,尽管我之前已经在XCTU应用中设置了波特率。

Does anyone know a way to receive the full string from Arduino? 有谁知道从Arduino接收完整字符串的方法吗? Or at leats how to setup properly Arduino's and PC's Xbee to work with higher baudrates? 或者至少如何正确设置Arduino和PC的Xbee以使用更高的波特率?

Thanks for the answers, and sorry for my writing skills... 感谢您的回答,也很抱歉我的写作能力...

尝试使用serial->readLine()而不是serial->readall() ,例如,您可以在serial->canReadLine()返回true之后循环等待,然后确保收到的数据是完整字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM