Subversion Repositories Projects

Rev

Rev 462 | Go to most recent revision | 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
/**
451 Brean 47
 * received new data from MK
48
 */
49
 
50
 
51
/**
397 Brean 52
 * send command to Mikrokopter
53
 */
450 Brean 54
void QTSerialCommunication::send_cmd(char cmd, int address, char data[150], unsigned int length, bool resend) {
449 Brean 55
    if (is_connected()) {
450 Brean 56
        Parser::create_frame(cmd, address, data, length);
57
        if (resend) {
500 Brean 58
            //resend every 2 seconds
451 Brean 59
            resendTimer.start(2000);
60
            resendData = data;
450 Brean 61
        }
500 Brean 62
        //TODO: save outgoing data for debug purpose
63
        std::cout << "send: " << data << std::endl;
450 Brean 64
        QByteArray temp(data);
65
        serial->sendData(temp);
451 Brean 66
    } else {
67
        //FIXME: Error message: not connected
449 Brean 68
    }
396 Brean 69
};
70
 
399 Brean 71
/**
72
 * stop sending commands to Mikrokopter
73
 * stop timer
74
 */
449 Brean 75
void QTSerialCommunication::stop_resend() {
500 Brean 76
    resendTimer.stop();
451 Brean 77
};
78
 
79
/**
80
 * resend timer timout
81
 */
82
void QTSerialCommunication::slot_resend_timer() {
462 Brean 83
}
84
 
500 Brean 85
/**
86
 * get incomming data from QT-Serial-Manager
87
 * and just give it to the handler.
88
 */
89
void QTSerialCommunication::slot_received_data(const QByteArray & data) {
90
    received_data((char *)data.data(), data.length());
91
}
92
 
93
/**
94
 * handel received data
95
 */
96
void QTSerialCommunication::received_data(char * data, int length) {
97
    //Debug: print oud debug data
98
    //TODO: save data in a file for debug purposes
99
    std::cout << "receive: " << data << std::endl;
100
    //handle incomming data
101
    //FIXME: HIER! Hardware-Id = Mode.id
102
 
103
    handler->receive_data(data, length);
451 Brean 104
}