void DataModel::setValue(const QString& id, const QVariantMap& data) {
    int foundAtIndex = -1;
    for (int i = 0;i<m_data.size();++i) {
        if (m_data[i].id == id) {
            foundAtIndex = i;
        }
    }
    if (foundAtIndex==-1)
        return;
    ModelDataItem& item = m_data[foundAtIndex];
    item.data = data;
    {
        QModelIndex modelindex = createIndex(foundAtIndex,0,0);
        emit dataChanged(modelindex, modelindex);
    }
    emit dataToSend(item);
}
ClientApplication::ClientApplication(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ClientApplicationUi)
{
    QTcpSocket *socket = new QTcpSocket();
    connection = new ClientConnection(socket, this);

    ui->setupUi(this);

    connect(socket, SIGNAL(connected()), this, SLOT(enableButtons()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(disableButtons()));

    connect(connection, SIGNAL(messageReceived(QByteArray)), this, SLOT(displayMessage(QByteArray)));

    connect(ui->connectButton, SIGNAL(clicked(bool)), this, SLOT(connectToServer()));
    connect(ui->disconnectButton, SIGNAL(clicked(bool)), this, SLOT(disconnectFromServer()));
    connect(ui->sendButton, SIGNAL(clicked(bool)), this, SLOT(dataToSend()));
    connect(ui->textToSend, SIGNAL(returnPressed()), this, SLOT(dataToSend()));
}
Beispiel #3
0
bool PacketInterface::sendPacket(const unsigned char *data, unsigned int len_packet)
{    
    // Only allow firmware commands in limited mode
    if (mIsLimitedMode && data[0] > COMM_WRITE_NEW_APP_DATA) {
        return false;
    }

    static unsigned char buffer[mMaxBufferLen];
    unsigned int ind = 0;

    // If the IP is valid, send the packet over UDP
    if (QString::compare(mHostAddress.toString(), "0.0.0.0") != 0) {
        if (mSendCan) {
            buffer[ind++] = COMM_FORWARD_CAN;
            buffer[ind++] = mCanId;
        }

        memcpy(buffer + ind, data, len_packet);
        ind += len_packet;

        mUdpSocket->writeDatagram(QByteArray::fromRawData((const char*)buffer, ind), mHostAddress, mUdpPort);
        return true;
    }

    int len_tot = len_packet;

    if (mSendCan) {
        len_tot += 2;
    }

    unsigned int data_offs = 0;

    if (len_tot <= 256) {
        buffer[ind++] = 2;
        buffer[ind++] = len_tot;
        data_offs = 2;
    } else {
        buffer[ind++] = 3;
        buffer[ind++] = len_tot >> 8;
        buffer[ind++] = len_tot & 0xFF;
        data_offs = 3;
    }

    if (mSendCan) {
        buffer[ind++] = COMM_FORWARD_CAN;
        buffer[ind++] = mCanId;
    }

    memcpy(buffer + ind, data, len_packet);
    ind += len_packet;

    unsigned short crc = crc16(buffer + data_offs, len_tot);
    buffer[ind++] = crc >> 8;
    buffer[ind++] = crc;
    buffer[ind++] = 3;

    QByteArray sendData = QByteArray::fromRawData((char*)buffer, ind);

    emit dataToSend(sendData);

    return true;
}