QSerialConsoleWindow::QSerialConsoleWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::QSerialConsoleWindow)
{
    ui->setupUi(this);
    _portSettingsDialog = new QDialog(this);
    _dialogButtonBox = new QDialogButtonBox(_portSettingsDialog);
    _mainLayout = new QVBoxLayout(_portSettingsDialog);
    _serialPort = new QSerialDevice();
    addToolBar(_serialPort->toolbar());
    _dialogButtonBox->addButton(QDialogButtonBox::Ok);
    _portSettingsDialog->setWindowTitle("Port Settings");
    _mainLayout->addWidget(_serialPort->widget());
    _mainLayout->addWidget(_dialogButtonBox);
    _protocol = new Protocol();

    _consoleWidget = new QConsoleWidget(ui->consoleFrame);
    _consoleWidget->showBanner("Welcome to QSerialConsole v 1.0\n");
    portClosed();

    connect(_serialPort, SIGNAL(portOpened()), this, SLOT(portOpened()));
    connect(_serialPort, SIGNAL(portClosed()), this, SLOT(portClosed()));
    connect(_serialPort, SIGNAL(msgAvailable(QByteArray)), _protocol, SLOT(decode(QByteArray)));
    connect(_protocol, SIGNAL(dataDecoded(QByteArray)), _consoleWidget, SLOT(showIncomingMessage(QByteArray)));
    connect(_consoleWidget, SIGNAL(newOutgoingMessage(QByteArray)), _protocol, SLOT(encode(QByteArray)));
    connect(_protocol, SIGNAL(dataEncoded(QByteArray)), _serialPort, SLOT(write(QByteArray)));
    connect(_dialogButtonBox, SIGNAL(accepted()), _portSettingsDialog, SLOT(close()));
}
void QSerialConsoleWindow::on_actionNone_triggered() {
    disconnect(_serialPort, SIGNAL(msgAvailable(QByteArray)), _protocol, SLOT(decode(QByteArray)));
    disconnect(_protocol, SIGNAL(dataDecoded(QByteArray)), _consoleWidget, SLOT(showIncomingMessage(QByteArray)));
    disconnect(_consoleWidget, SIGNAL(newOutgoingMessage(QByteArray)), _protocol, SLOT(encode(QByteArray)));
    disconnect(_protocol, SIGNAL(dataEncoded(QByteArray)), _serialPort, SLOT(write(QByteArray)));
    if (_protocol)
        delete _protocol;
    _protocol = new Protocol();
    connect(_serialPort, SIGNAL(msgAvailable(QByteArray)), _protocol, SLOT(decode(QByteArray)));
    connect(_protocol, SIGNAL(dataDecoded(QByteArray)), _consoleWidget, SLOT(showIncomingMessage(QByteArray)));
    connect(_consoleWidget, SIGNAL(newOutgoingMessage(QByteArray)), _protocol, SLOT(encode(QByteArray)));
    connect(_protocol, SIGNAL(dataEncoded(QByteArray)), _serialPort, SLOT(write(QByteArray)));
    _consoleWidget->refreshProtocolTags(_protocol->tagsList());
    ui->actionASCII->setChecked(false);
    _consoleWidget->setFocus();
}
bool ReceiveData::processData()
{
        //static int i = 0;
        inBuffer->getN((char *)payloadDecoded, 0, PAYLOAD_SIZE);

        memcpy(dataconverted, payloadDecoded, PAYLOAD_SIZE);
        //qDebug() << "received - " << i;
        //i++;
//        for(int i = 0; i < 200; i++)
//        {
//            qDebug() << payloadDecoded[i];
//        }
        emit dataDecoded(dataconverted);
        return true;
}
QByteArray Protocol_ASCII::decode(QByteArray data) {
#ifdef _DEBUG_PROTOCOL
    qDebug() << _MODULE_NAME << "decode() - Decoding: " << data << " (" << data.size() << " bytes)";
#endif //_DEBUG_PROTOCOL
    QByteArray buffer = data;
    for (qint64 i = 0; i < data.size(); i++) {
        switch ((asciiValueType) data.at(i)) {
        case NUL:
            buffer.append("[NUL]");
            break;
        case SOH:
            buffer.append("[SOH]");
            break;
        case STX:
            buffer.append("[STX]");
            break;
        case ETX:
            buffer.append("[ETX]");
            break;
        case EOT:
            buffer.append("[EOT]");
            break;
        case ENQ:
            buffer.append("[ENQ]");
            break;
        case ACK:
            buffer.append("[ACK]");
            break;
        case BEL:
            buffer.append("[BEL]");
            break;
        case BS:
            buffer.append("[BS]");
            break;
        case HT:
            buffer.append("[HT]");
            break;
        case NL:
            buffer.append("[NL]");
            break;
        case VT:
            buffer.append("[VT]");
            break;
        case NP:
            buffer.append("[NP]");
            break;
        case CR:
            buffer.append("[CR]");
            break;
        case SO:
            buffer.append("[SO]");
            break;
        case SI:
            buffer.append("[SI]");
            break;
        case DLE:
            buffer.append("[DLE]");
            break;
        case DC1:
            buffer.append("[DC1]");
            break;
        case DC2:
            buffer.append("[DC2]");
            break;
        case DC3:
            buffer.append("[DC3]");
            break;
        case DC4:
            buffer.append("[DC4]");
            break;
        case NAK:
            buffer.append("[NAK]");
            break;
        case SYN:
            buffer.append("[SYN]");
            break;
        case ETB:
            buffer.append("[ETB]");
            break;
        case CAN:
            buffer.append("[CAN]");
            break;
        case EM:
            buffer.append("[EM]");
            break;
        case SUB:
            buffer.append("[SUB]");
            break;
        case ESC:
            buffer.append("[ESC]");
            break;
        case FS:
            buffer.append("[FS]");
            break;
        case GS:
            buffer.append("[GS]");
            break;
        case RS:
            buffer.append("[RS]");
            break;
        case US:
            buffer.append("[US]");
            break;
        case SP:
            buffer.append("[SP]");
            break;
        case DEL:
            buffer.append("[DEL]");
            break;
        default:
            buffer.append(data.at(i));
            break;
        }
    }
    emit dataDecoded(buffer);
#ifdef _DEBUG_PROTOCOL
    qDebug() << _MODULE_NAME << "decode() - Decoded: " << buffer << " (" << buffer.size() << " bytes)";
#endif //_DEBUG_PROTOCOL
    return buffer;
}