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
 
@@ -44,30 +44,19 @@ void CommandPanel::newCommand()
 
{
 
    auto command = new CommandWidget();
 
    ui->scrollAreaWidgetContents->layout()->addWidget(command);
 
    connect(command, &CommandWidget::sendCommand, this, &CommandPanel::sendCommand);
 
}
 

	
 
void CommandPanel::sendCommand(QString command, bool ascii)
 
void CommandPanel::sendCommand(QByteArray command)
 
{
 
    if (!serialPort->isOpen())
 
    {
 
        qCritical() << "Port is not open!";
 
        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
 
@@ -19,12 +19,13 @@
 

	
 
#ifndef COMMANDPANEL_H
 
#define COMMANDPANEL_H
 

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

	
 
#include "commandwidget.h"
 

	
 
namespace Ui {
 
class CommandPanel;
 
}
 
@@ -40,10 +41,10 @@ public:
 
private:
 
    Ui::CommandPanel *ui;
 
    QSerialPort* serialPort;
 

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

	
 
#endif // COMMANDPANEL_H
commandwidget.cpp
Show inline comments
 
@@ -54,27 +54,33 @@ void CommandWidget::onSendClicked()
 
    {
 
        qWarning() << "Enter a command to send!";
 
        ui->leCommand->setFocus(Qt::OtherFocusReason);
 
        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
 
        if (command.size() % 2 == 1)
 
        {
 
            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)
 
{
 
    ui->leCommand->setMode(checked);
 
}
commandwidget.h
Show inline comments
 
@@ -18,13 +18,13 @@
 
*/
 

	
 
#ifndef COMMANDWIDGET_H
 
#define COMMANDWIDGET_H
 

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

	
 
namespace Ui {
 
class CommandWidget;
 
}
 

	
 
class CommandWidget : public QWidget
 
@@ -39,18 +39,16 @@ signals:
 
    void deleteRequested(CommandWidget* thisWidget); // emitted when delete button is clicked
 

	
 
    // emitted when send button is clicked
 
    //
 
    // 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
 

	
 
private slots:
 
    void onDeleteClicked();
 
    void onSendClicked();
0 comments (0 inline, 0 general)