Qt Serial с собственным портом USB Arduino Due и настройкой Bluetooth NR 42 через Xbee explorer

Я пытаюсь сделать заявку в Qt для настройки модуля bluetooth RN42 http://ww1.microchip.com/downloads/en/DeviceDoc/rn-42-ds-v2.32r.pdf . Для того, чтобы настроить модуль, некоторая команда должна быть отправлена ​​через последовательный порт, но именно здесь начинают происходить посторонние события. Я подключаю его к компьютеру через xbee xplorer и могу без проблем настроить его, используя мое приложение, но когда я пытаюсь получить текущую конфигурацию, не получен ответ, я вижу, что устройство переходит в режим конфигурации, потому что светодиод мигает быстрее , но я не могу получить обратную связь от устройства через последовательный порт.

Поэтому я решил начать отладку с помощью arduino из-за того, что, если я подключу его через порт программирования, я могу сообщать о проблемах без проблем, но если я подключу его через собственный порт USB, я не получу никакой информации от устройства. Но если я открываю порт с помощью последовательного монитора arduino, начинается коммуникация, затем закрываю последовательный монитор arduino, открываю мое приложение, и я без проблем общаюсь.

Я также пытался соединиться с Docklight (приложение последовательного монитора) и снова у меня нет проблем с соединением, но если после соединения с Docklight я пытаюсь соединиться с моим приложением, в отличие от последовательного монитора Arduino, не будет получен ответ.

Несмотря на то, что я отлажен с помощью осциллографа, я вижу, что информация отправляется после закрытия порта.

Я пробовал много разных вещей, и я не знаю, что я делаю неправильно. Я думаю, это связано с конфигурацией порта, но не уверен, может ли это быть ошибкой в ​​QtSerialport?

Вот мои программы. Я использую Qt Creator 5.9.1 в Windows с MinGW53_32

QT

.профессионал

#-------------------------------------------------
#
# Project created by QtCreator 2017-07-25T08:39:59
#
#-------------------------------------------------

QT       += core gui
QT       += serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = BT_Config_Tool
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \
main.cpp \
bluetooth_config.cpp

HEADERS += \
bluetooth_config.h

FORMS += \
bluetooth_config.ui

bluetooth_config.h

#ifndef BLUETOOTH_CONFIG_H
#define BLUETOOTH_CONFIG_H

#include <QMainWindow>
#include <QTimer>
#include <QSerialPort>
#include <QtSerialPort>
#include <QtSerialPort/QtSerialPort>
#include <stdio.h>
#include <math.h>
#include <QSerialPortInfo>

#define BUFFMAX 1000

namespace Ui {
class bluetooth_config;
}

class bluetooth_config : public QMainWindow
{
Q_OBJECT
public slots:
void ReadDevice(void);
void FlashDevice(void);
void UpdateCombobox(void);
void ReadSerial(void);

public:
explicit bluetooth_config(QWidget *parent = 0);
~bluetooth_config();

private slots:
void on_pushButton_clicked();

void on_pushButton_2_clicked();

void on_pushButton_3_clicked();

private:
Ui::bluetooth_config *ui;
QSerialPort *_port;
QTimer *_timerRead;
QTimer *_timerFlash;
QTimer *_timerUpdateCombobox;
QSerialPortInfo *_serialPortInfo;
int _cFlash = 0;
int _cRead = 0;
char _buffer[BUFFMAX];
//QByteArray _buffer;
int c;
};

#endif // BLUETOOTH_CONFIG_H

bluetooth_config.cpp

#include "bluetooth_config.h"#include "ui_bluetooth_config.h"
const char *nameFlash;
const char *passFlash;
const char *nameRead;
char trama[BUFFMAX];
int item = 0;bluetooth_config::bluetooth_config(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::bluetooth_config)
{
ui->setupUi(this);
ui->label_4->setVisible(false);
ui->label_5->setVisible(false);

_timerRead = new QTimer(this);
connect(_timerRead,SIGNAL(timeout()),this, SLOT(ReadDevice()));

_timerFlash = new QTimer(this);
connect(_timerFlash,SIGNAL(timeout()),this, SLOT(FlashDevice()));

_timerUpdateCombobox = new QTimer(this);
connect(_timerUpdateCombobox,SIGNAL(timeout()),this, SLOT(UpdateCombobox()));
_timerUpdateCombobox->start(2000);

_port = new QSerialPort();
_port->setBaudRate(QSerialPort::Baud115200);
_port->setDataBits(QSerialPort::Data8);
_port->setParity(QSerialPort::NoParity);
_port->setStopBits(QSerialPort::OneStop);
_port->setFlowControl(QSerialPort::NoFlowControl);
connect(_port,SIGNAL(readyRead()),this,SLOT(ReadSerial()));

//ui->tabWidget->setTabEnabled(0, false);

}

bluetooth_config::~bluetooth_config()
{
delete ui;
}

void bluetooth_config::ReadDevice()
{
_cRead ++;
if (_cRead == 1){
_port->write("$$$");
ui->lcdNumber_2->display(_cRead);
_timerRead->stop();
}
else if (_cRead == 2){
_port->write("---\r\n");
_port->waitForBytesWritten();
ui->lcdNumber_2->display(_cRead);
_timerRead->stop();
}
else if (_cRead == 3){
ui->label_5->setVisible(true);
ui->lcdNumber_2->display(_cRead);
_timerRead->stop();
_cRead = 0;
}
}

void bluetooth_config::FlashDevice()
{
_cFlash ++;
if (_cFlash == 1){
_port->write("$$$");
}
else if (_cFlash == 2){
nameFlash = ui->lineEdit->displayText().toUtf8().constData();
strcpy(trama, "SN,");
strcat(trama,nameFlash);
strcat(trama,"\n\r");
_port->write(trama);
}
else if (_cFlash == 3){
passFlash = ui->lineEdit_2->displayText().toUtf8().constData();
strcpy(trama, "SP,");
strcat(trama,passFlash);
strcat(trama,"\n\r");
_port->write(trama);
}
else if (_cFlash == 4){
_port->write("---\n\r");
}
else if (_cFlash == 5){
_cFlash = 0;
_timerFlash->stop();
ui->label_4->setVisible(true);
}
}

void bluetooth_config::UpdateCombobox()
{
item = ui->comboBox->currentIndex();
ui->comboBox->clear();

foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts())
{
ui->comboBox->addItem(serialPortInfo.portName());
}

if (item <= (ui->comboBox->count() - 1) && item >= 0){
ui->comboBox->setCurrentIndex(item);
}
else {
ui->comboBox->setCurrentIndex(ui->comboBox->count() - 1);
}
}

void bluetooth_config::ReadSerial()
{
int ojete = _port->bytesAvailable();
ui->lcdNumber->display(ojete);
//_port->read(_buffer,BUFFMAX);
QByteArray a = _port->readAll();
//ui->plainTextEdit->appendPlainText(_buffer);
ui->plainTextEdit->appendPlainText(a);
if (_cRead == 1 || _cRead == 2){
_timerRead->start(50);
}
}

void bluetooth_config::on_pushButton_clicked()
{
ui->label_5->setVisible(false);
_timerRead->start(50);
_port->flush();
}

void bluetooth_config::on_pushButton_2_clicked()
{
ui->label_4->setVisible(false);
_timerFlash->start(50);
_port->flush();
}

void bluetooth_config::on_pushButton_3_clicked()
{
if (!_port->isOpen()){
_port->setPortName(ui->comboBox->currentText());
if (_port->open(QIODevice::ReadWrite)){
ui->label_3->setText("Puerto abierto");
ui->pushButton_3->setText("Cerrar");
ui->pushButton->setEnabled(true);
ui->pushButton_2->setEnabled(true);
}
else {
ui->label_3->setText("Ha ocurrido un error al abrir el puerto");
}
}
else {
_port->close();
ui->label_3->setText("Puerto cerrado");
ui->pushButton_3->setText("Abrir");
ui->pushButton->setEnabled(false);
ui->pushButton_2->setEnabled(false);
}
}

main.cpp

#include "bluetooth_config.h"#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
bluetooth_config w;
w.show();

return a.exec();
}

Большое спасибо.

Alejandro

1

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …