gpt4 book ai didi

c++ - 连接 Arduino 与 Qt

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:43:52 26 4
gpt4 key购买 nike

我正在从 Qt 向 Arduino 发送“R”字符
并等待那个 R,让 LED 在 Arduino 上闪烁。但它不起作用......

我认为这可能是 waitFotBytesWritten 或 ASCI 的问题...
N.B: 任何信号都很好;不需要是一个字符。

我的Qt代码:

QSerialPort serial;
serial.setPortName("PORT_NAME");
serial.open(QIODevice::ReadWrite);
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);

if (serial.isOpen() && serial.isWritable())
{
qDebug() << "Ready..." << endl;
serial.write("R");

if(serial.flush())
{
qDebug() << "ok" << endl;
}
qDebug() <<"value sent!!! "<< endl;
serial.close();
}

我的 Arduino 代码

int led = 13; 

void setup()
{
pinMode(led, OUTPUT);
}

void loop()
{

if (Serial.read() == 'R') //blink on 5sec
{
digitalWrite(led, HIGH);
delay(5000);
digitalWrite(led, LOW);
}

else //Blink on and off 1 sec,1sec
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}

}

Qt 输出显示:

 Manufacturer:  "Arduino (www.arduino.cc)"  
Busy: false
Ready...
ok
value sent!!!

任何帮助将不胜感激


完整代码如下:

Qt:

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>

#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QString>

QT_USE_NAMESPACE

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QSerialPortInfo info("usbmodem621");
// Check info of the port
qDebug() << "Name : " << info.portName();
qDebug() << "Manufacturer: " << info.manufacturer(); //if showing manufacturer, means Qstring &name is good
qDebug() << "Busy: " << info.isBusy() << endl;

// Initialize Serial
QSerialPort serial;
serial.setPortName("usbmodem621");
serial.open(QIODevice::ReadWrite);
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);

if (serial.isOpen() && serial.isWritable())
{
qDebug() << "Is open : " << serial.isOpen() << endl;
qDebug() << "Is writable : " << serial.isWritable() << endl;

QString string("R\n");
QByteArray ba("R\n");
serial.write(ba);
if (serial.bytesToWrite() > 0)
{
serial.flush();
if(serial.waitForBytesWritten(1000))
{
qDebug() << "data has been send" << endl;
}

}
if(serial.flush())
{
qDebug() << "ok" << endl;
}
qDebug() <<"value sent!!! "<< endl;
serial.close();
}

else
{
qDebug() << "An error occured" << endl;
}
return 0;
}

阿杜伊诺:

int led = 13; 

void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
}

void loop()
{

if (Serial.available() > 0)
{
digitalWrite(led, HIGH);
delay(5000);

if(Serial.read() == 'R')
{
digitalWrite(led, LOW);
delay(1000);
}
}

else
{
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}

}

最佳答案

问题已经解决:

Qt:

if (serial.isOpen() && serial.isWritable())
{

QByteArray ba("R");
serial.write(ba);
serial.flush();
qDebug() << "data has been send" << endl;
serial.close();
}

阿杜伊诺:

int led = 13, avlb = 0; 

void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
Serial.println("started");
}

void loop()
{
if (Serial.available() > 0)
{
Serial.println("available");
Serial.println(Serial.available());
delay(2000);
if(Serial.read())
{
Serial.println("read");
Serial.println(Serial.read());
delay(2000);
}
}

else
{
Serial.println("not available");
delay(1000);
}

}

注意:不要忘记在 Arduino IDE 中正确设置波特率;
监听,9600波特率

关于c++ - 连接 Arduino 与 Qt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24415243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com