Subversion Repositories Projects

Rev

Rev 462 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "QTSerialCommunication.h"
#include "../libMK/Parser.h"
#include "../Classes/ToolBox.h"
#include <iostream>


/**
 * initiate connection and stuff
 */

QTSerialCommunication::QTSerialCommunication(Handler * handler) {
    this->handler = handler;
    connection_lost();
    serial = new ManageSerialPort();
    serial->setBaudRate(BAUD57600);   //BaudRate
    serial->setDataBits(DATA_8);      //DataBits
    serial->setParity(PAR_NONE);      //Parity
    serial->setStopBits(STOP_1);      //StopBits
    serial->setFlowControl(FLOW_OFF); //FlowControl

    serial->setTimeout(0, 10);
    serial->enableSending();
    serial->enableReceiving();
}

/**
 * connect to Mikrokopter
 */

void QTSerialCommunication::connect_MK(char * addr) {
    serial->setPort(addr);
    serial->open();
    int timeout = 5;
    while (timeout > 0) {
        ToolBox::wait(200);
        timeout--;
        if (serial->isOpen()) {
            serial->receiveData();
            connect(serial, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_received_data(const QByteArray &)));
            connection_established();
            //break while loop
            return;
        }
    }
    //TODO: Error message, because communication could not established
};

/**
 * received new data from MK
 */



/**
 * send command to Mikrokopter
 */

void QTSerialCommunication::send_cmd(char cmd, int address, char data[150], unsigned int length, bool resend) {
    if (is_connected()) {
        Parser::create_frame(cmd, address, data, length);
        if (resend) {
            //resend every 2 seconds
            resendTimer.start(2000);
            resendData = data;
        }
        //TODO: save outgoing data for debug purpose
        std::cout << "send: " << data << std::endl;
        QByteArray temp(data);
        serial->sendData(temp);
    } else {
        //FIXME: Error message: not connected
    }
};

/**
 * stop sending commands to Mikrokopter
 * stop timer
 */

void QTSerialCommunication::stop_resend() {
    resendTimer.stop();
};

/**
 * resend timer timout
 */

void QTSerialCommunication::slot_resend_timer() {
}

/**
 * get incomming data from QT-Serial-Manager
 * and just give it to the handler.
 */

void QTSerialCommunication::slot_received_data(const QByteArray & data) {
    received_data((char *)data.data(), data.length());
}

/**
 * handel received data
 */

void QTSerialCommunication::received_data(char * data, int length) {
    //Debug: print oud debug data
    //TODO: save data in a file for debug purposes
    std::cout << "receive: " << data << std::endl;
    //handle incomming data
    //FIXME: HIER! Hardware-Id = Mode.id

    handler->receive_data(data, length);
}