Subversion Repositories Projects

Rev

Rev 500 | 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
};

/**
 * send command to Mikrokopter
 */

void QTSerialCommunication::send_cmd(char cmd, int address, char data[150], unsigned int length, bool resend) {
    char send_data[150];
    if (is_connected()) {
        Parser::create_frame(send_data, cmd, address, data, length);
        if (resend) {
            //resend every 2 seconds
            resendTimer.start(2000);
            resendData = send_data;
        }
        //TODO: save outgoing data for debug purpose
        std::cout << "send: ";
        FlightLog::log_data(send_data, length+3);
        QByteArray temp(send_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) {
    std::cout << "receive something" << std::endl;
    received_data((char *)data.data(), data.length());
}

/**
 * handel received data from MK
 */

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: ";
    FlightLog::log_data(data, length);
    //handle incomming data
    handler->receive_data(data, length);
}