Changeset - 9add38e39c0a
[Not reviewed]
Hasan Yavuz Ă–ZDERYA - 10 years ago 2015-10-10 14:47:28
hy@ozderya.net
refactored sendCommand signal to carry QByteArray instead of QString+mode
4 files changed with 16 insertions and 22 deletions:
0 comments (0 inline, 0 general)
commandpanel.cpp
Show inline comments
 
@@ -47,7 +47,7 @@ void CommandPanel::newCommand()
 
    connect(command, &CommandWidget::sendCommand, this, &CommandPanel::sendCommand);
 
}
 

	
 
void CommandPanel::sendCommand(QString command, bool ascii)
 
void CommandPanel::sendCommand(QByteArray command)
 
{
 
    if (!serialPort->isOpen())
 
    {
 
@@ -55,19 +55,8 @@ void CommandPanel::sendCommand(QString c
 
        return;
 
    }
 

	
 
    if (ascii)
 
    if (serialPort->write(command) < 0)
 
    {
 
        qDebug() << "Sending" << command;
 
        if (serialPort->write(command.toLatin1()) < 0)
 
        {
 
            qCritical() << "Send command failed!";
 
        }
 
    }
 
    else
 
    {
 
        if (serialPort->write(QByteArray::fromHex(command.toLatin1())) < 0)
 
        {
 
            qCritical() << "Send command failed!";
 
        }
 
        qCritical() << "Send command failed!";
 
    }
 
}
commandpanel.h
Show inline comments
 
@@ -22,6 +22,7 @@
 

	
 
#include <QWidget>
 
#include <QSerialPort>
 
#include <QByteArray>
 

	
 
#include "commandwidget.h"
 

	
 
@@ -43,7 +44,7 @@ private:
 

	
 
private slots:
 
    void newCommand();
 
    void sendCommand(QString command, bool ascii);
 
    void sendCommand(QByteArray command);
 
};
 

	
 
#endif // COMMANDPANEL_H
commandwidget.cpp
Show inline comments
 
@@ -57,7 +57,12 @@ void CommandWidget::onSendClicked()
 
        return;
 
    }
 

	
 
    if (!isASCIIMode()) // hex mode
 
    if (isASCIIMode())
 
    {
 
        qDebug() << "Sending:" << command;
 
        emit sendCommand(command.toLatin1());
 
    }
 
    else // hex mode
 
    {
 
        command = command.remove(' ');
 
        // check if nibbles are missing
 
@@ -65,13 +70,14 @@ void CommandWidget::onSendClicked()
 
        {
 
            qWarning() << "HEX command is missing a nibble at the end!";
 
            ui->leCommand->setFocus(Qt::OtherFocusReason);
 
            // highlight the byte that is missing a nibble (last byte obviously)
 
            int textSize = ui->leCommand->text().size();
 
            ui->leCommand->setSelection(textSize-1, textSize);
 
            return;
 
        }
 
        qDebug() << "Sending HEX:" << command;
 
        emit sendCommand(QByteArray::fromHex(command.toLatin1()));
 
    }
 

	
 
    emit sendCommand(command, ui->pbASCII->isChecked());
 
}
 

	
 
void CommandWidget::onASCIIToggled(bool checked)
commandwidget.h
Show inline comments
 
@@ -21,7 +21,7 @@
 
#define COMMANDWIDGET_H
 

	
 
#include <QWidget>
 
#include <QString>
 
#include <QByteArray>
 

	
 
namespace Ui {
 
class CommandWidget;
 
@@ -42,12 +42,10 @@ signals:
 
    //
 
    // in case of hex mode, command text should be a hexadecimal
 
    // string containing hexadecimal characters only (not even spaces)
 
    void sendCommand(QString command, bool ascii);
 
    void sendCommand(QByteArray command);
 

	
 
private:
 
    Ui::CommandWidget *ui;
 
    QString storedAsciiText;
 
    QString storedHexText;
 

	
 
    bool isASCIIMode(); // true: ascii mode, false hex mode
 

	
0 comments (0 inline, 0 general)