[solved] Qt creator ( serial write )



  • I need make a "serial->write(data);" but i can't change data. i'm bad in english and c++.

    my program is :
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "console.h"
    #include "settingsdialog.h"

    #include <QMessageBox>
    #include <QThread>

    int flag = 0,flag1 = 0,flag2=0,flag3=0,flag6=0;
    char etat_led=8;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    console = new Console;
    console->setEnabled(false);
    ui->gridLayout->addWidget(console,1,0,4,4);
    serial = new QSerialPort(this);
    settings = new SettingsDialog;

    ui->actionConnect->setEnabled(true);
    ui->actionDisconnect->setEnabled(false);
    ui->actionQuit->setEnabled(true);
    ui->actionConfigure->setEnabled(true);
    
    initActionsConnections();
    
    connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this,
            SLOT(handleError(QSerialPort::SerialPortError)));
    connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    connect(console, SIGNAL(getData(QByteArray)), this, SLOT(writeData(QByteArray)));
    

    }

    MainWindow::~MainWindow()
    {
    delete settings;
    delete ui;
    }

    void MainWindow::openSerialPort()
    {
    SettingsDialog::Settings p = settings->settings();
    serial->setPortName(p.name);
    serial->setBaudRate(p.baudRate);
    serial->setDataBits(p.dataBits);
    serial->setParity(p.parity);
    serial->setStopBits(p.stopBits);
    serial->setFlowControl(p.flowControl);
    if (serial->open(QIODevice::ReadWrite)) {
    console->setEnabled(true);
    console->setLocalEchoEnabled(p.localEchoEnabled);
    ui->actionConnect->setEnabled(false);
    ui->actionDisconnect->setEnabled(true);
    ui->actionConfigure->setEnabled(false);
    ui->statusBar->showMessage(tr("Connecté à %1 : %2, %3, %4, %5, %6")
    .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
    .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
    } else {
    QMessageBox::critical(this, tr("Error"), serial->errorString());

        ui->statusBar->showMessage(tr("Open error"));
    }
    

    }

    void MainWindow::closeSerialPort()
    {
    serial->close();
    console->setEnabled(false);
    ui->actionConnect->setEnabled(true);
    ui->actionDisconnect->setEnabled(false);
    ui->actionConfigure->setEnabled(true);
    ui->statusBar->showMessage(tr("Déconnecté"));
    }

    void MainWindow::about()
    {
    QMessageBox::about(this, tr("Terminal QT"),
    tr("Trop"
    "Génial"
    "non?"));
    }

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

    void MainWindow::readData()
    {
    QByteArray data = serial->readAll();
    console->putData(data);
    }

    void MainWindow::handleError(QSerialPort::SerialPortError error)
    {
    if (error == QSerialPort::ResourceError) {
    QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
    closeSerialPort();
    }
    }

    void MainWindow::initActionsConnections()
    {
    connect(ui->actionConnect, SIGNAL(triggered()), this, SLOT(openSerialPort()));
    connect(ui->actionDisconnect, SIGNAL(triggered()), this, SLOT(closeSerialPort()));
    connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close()));
    connect(ui->actionConfigure, SIGNAL(triggered()), settings, SLOT(show()));
    connect(ui->actionClear, SIGNAL(triggered()), console, SLOT(clear()));
    connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
    connect(ui->actionAboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    }

    void MainWindow::on_pushButton_clicked()
    {

    • QByteArray data;*
    if(flag == 0)
    {
        ui->pushButton->setText("Activé");
        ui->pushButton->setToolTip("désactivé ?");
        ui->pushButton->setStyleSheet("* { background-color: rgb(125,255,100) }");
        flag = 1;
        
       * serial->write(data); // I need change data here !! for example : data = "1000"*
    }
    else
    {
        ui->pushButton->setText("Désactivé");
        ui->pushButton->setToolTip("activé ?");
        ui->pushButton->setStyleSheet("* { background-color: rgb(255,125,100) }");
        flag = 0;
    }
    

    }

    void MainWindow::on_pushButton_2_clicked()
    {
    if(flag1 == 0)
    {
    ui->pushButton_2->setText("Activé");
    ui->pushButton_2->setToolTip("désactivé ?");
    ui->pushButton_2->setStyleSheet("* { background-color: rgb(125,255,100) }");
    flag1 = 1;
    }
    else
    {
    ui->pushButton_2->setText("Désactivé");
    ui->pushButton_2->setToolTip("activé ?");
    ui->pushButton_2->setStyleSheet("* { background-color: rgb(255,125,100) }");
    flag1 = 0;
    }
    }

    void MainWindow::on_pushButton_3_clicked()
    {
    if(flag2 == 0)
    {
    ui->pushButton_3->setText("Activé");
    ui->pushButton_3->setToolTip("désactivé ?");
    ui->pushButton_3->setStyleSheet("* { background-color: rgb(125,255,100) }");
    flag2 = 1;
    }
    else
    {
    ui->pushButton_3->setText("Désactivé");
    ui->pushButton_3->setToolTip("activé ?");
    ui->pushButton_3->setStyleSheet("* { background-color: rgb(255,125,100) }");
    flag2 = 0;
    }
    }

    void MainWindow::on_pushButton_4_clicked()
    {
    if(flag3 == 0)
    {
    ui->pushButton_4->setText("Activé");
    ui->pushButton_4->setToolTip("désactivé ?");
    ui->pushButton_4->setStyleSheet("* { background-color: rgb(125,255,100) }");
    flag3 = 1;
    }
    else{
    ui->pushButton_4->setText("Désactivé");
    ui->pushButton_4->setToolTip("activé ?");
    ui->pushButton_4->setStyleSheet("* { background-color: rgb(255,125,100) }");
    flag3 = 0;
    }
    }@

    [edit: added missing coding tags @ SGaist]


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What exactly would you like to write ?



  • Hi thank you,

    I want change data but i can't compile if i try to write that.

    "data = 1000";


  • Lifetime Qt Champion

    So something like:

    @serial->write("1000");@

    or

    @serial->write(QByteArray::number(1000));@

    ?



  • My compilation works, but my PIC need to receive 1000 and not "0031 0030 0030 0030" because QByteArray is coded in 16 bits no ?

    i try to look for an issue. I need to send only 4 bit if i want communicate with my PIC. i think.

    i hope you can understand my explication.

    Thank you for respond as quickly.



  • Nice i have to find. i use a 4 mask and i can send my bits. thank you for help.

    Have a nice day guy.

    Zirtek


  • Lifetime Qt Champion

    It's not the same use case, looks like you need to send the ascii code of each digit of 1000.

    If you have to do it for everything you send then you should consider creating a little function that does the conversion



  • I made a function for send more easily now, i think my problem is solved. I will make that with my five capacitiv touch.

    Thank you SGaist.


  • Lifetime Qt Champion

    You're welcome !

    Since you have your communication working, please update the thread title prepending [solved] so other forum users may know a solution has been found :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.