Subversion Repositories Projects

Rev

Rev 500 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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