Subversion Repositories Projects

Rev

Rev 500 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
449 Brean 1
#include "QTSerialCommunication.h"
2
#include "../libMK/Parser.h"
450 Brean 3
#include "../Classes/ToolBox.h"
500 Brean 4
#include <iostream>
392 Brean 5
 
500 Brean 6
 
397 Brean 7
/**
449 Brean 8
 * initiate connection and stuff
9
 */
500 Brean 10
QTSerialCommunication::QTSerialCommunication(Handler * handler) {
11
    this->handler = handler;
451 Brean 12
    connection_lost();
449 Brean 13
    serial = new ManageSerialPort();
14
    serial->setBaudRate(BAUD57600);   //BaudRate
15
    serial->setDataBits(DATA_8);      //DataBits
16
    serial->setParity(PAR_NONE);      //Parity
17
    serial->setStopBits(STOP_1);      //StopBits
18
    serial->setFlowControl(FLOW_OFF); //FlowControl
19
 
20
    serial->setTimeout(0, 10);
21
    serial->enableSending();
22
    serial->enableReceiving();
23
}
24
 
25
/**
397 Brean 26
 * connect to Mikrokopter
27
 */
450 Brean 28
void QTSerialCommunication::connect_MK(char * addr) {
29
    serial->setPort(addr);
30
    serial->open();
31
    int timeout = 5;
32
    while (timeout > 0) {
33
        ToolBox::wait(200);
34
        timeout--;
35
        if (serial->isOpen()) {
36
            serial->receiveData();
500 Brean 37
            connect(serial, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_received_data(const QByteArray &)));
450 Brean 38
            connection_established();
39
            //break while loop
40
            return;
41
        }
42
    }
43
    //TODO: Error message, because communication could not established
392 Brean 44
};
45
 
397 Brean 46
/**
47
 * send command to Mikrokopter
48
 */
450 Brean 49
void QTSerialCommunication::send_cmd(char cmd, int address, char data[150], unsigned int length, bool resend) {
513 Brean 50
    char send_data[150];
449 Brean 51
    if (is_connected()) {
513 Brean 52
        Parser::create_frame(send_data, cmd, address, data, length);
450 Brean 53
        if (resend) {
500 Brean 54
            //resend every 2 seconds
451 Brean 55
            resendTimer.start(2000);
513 Brean 56
            resendData = send_data;
450 Brean 57
        }
500 Brean 58
        //TODO: save outgoing data for debug purpose
513 Brean 59
        std::cout << "send: ";
60
        FlightLog::log_data(send_data, length+3);
61
        QByteArray temp(send_data);
450 Brean 62
        serial->sendData(temp);
451 Brean 63
    } else {
64
        //FIXME: Error message: not connected
449 Brean 65
    }
396 Brean 66
};
67
 
399 Brean 68
/**
69
 * stop sending commands to Mikrokopter
70
 * stop timer
71
 */
449 Brean 72
void QTSerialCommunication::stop_resend() {
500 Brean 73
    resendTimer.stop();
451 Brean 74
};
75
 
76
/**
77
 * resend timer timout
78
 */
79
void QTSerialCommunication::slot_resend_timer() {
462 Brean 80
}
81
 
500 Brean 82
/**
83
 * get incomming data from QT-Serial-Manager
84
 * and just give it to the handler.
85
 */
86
void QTSerialCommunication::slot_received_data(const QByteArray & data) {
513 Brean 87
    std::cout << "receive something" << std::endl;
500 Brean 88
    received_data((char *)data.data(), data.length());
89
}
90
 
91
/**
513 Brean 92
 * handel received data from MK
500 Brean 93
 */
94
void QTSerialCommunication::received_data(char * data, int length) {
95
    //Debug: print oud debug data
96
    //TODO: save data in a file for debug purposes
513 Brean 97
    std::cout << "receive: ";
98
    FlightLog::log_data(data, length);
500 Brean 99
    //handle incomming data
100
    handler->receive_data(data, length);
451 Brean 101
}