Subversion Repositories Projects

Rev

Rev 462 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 462 Rev 500
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>
-
 
5
 
4
 
6
 
5
/**
7
/**
6
 * initiate connection and stuff
8
 * initiate connection and stuff
7
 */
9
 */
8
QTSerialCommunication::QTSerialCommunication() {
10
QTSerialCommunication::QTSerialCommunication(Handler * handler) {
-
 
11
    this->handler = handler;
9
    connection_lost();
12
    connection_lost();
10
    serial = new ManageSerialPort();
13
    serial = new ManageSerialPort();
11
    serial->setBaudRate(BAUD57600);   //BaudRate
14
    serial->setBaudRate(BAUD57600);   //BaudRate
12
    serial->setDataBits(DATA_8);      //DataBits
15
    serial->setDataBits(DATA_8);      //DataBits
13
    serial->setParity(PAR_NONE);      //Parity
16
    serial->setParity(PAR_NONE);      //Parity
14
    serial->setStopBits(STOP_1);      //StopBits
17
    serial->setStopBits(STOP_1);      //StopBits
15
    serial->setFlowControl(FLOW_OFF); //FlowControl
18
    serial->setFlowControl(FLOW_OFF); //FlowControl
16
 
19
 
17
    serial->setTimeout(0, 10);
20
    serial->setTimeout(0, 10);
18
    serial->enableSending();
21
    serial->enableSending();
19
    serial->enableReceiving();
22
    serial->enableReceiving();
20
}
23
}
21
 
24
 
22
/**
25
/**
23
 * connect to Mikrokopter
26
 * connect to Mikrokopter
24
 */
27
 */
25
void QTSerialCommunication::connect_MK(char * addr) {
28
void QTSerialCommunication::connect_MK(char * addr) {
26
    serial->setPort(addr);
29
    serial->setPort(addr);
27
    serial->open();
30
    serial->open();
28
    int timeout = 5;
31
    int timeout = 5;
29
    while (timeout > 0) {
32
    while (timeout > 0) {
30
        ToolBox::wait(200);
33
        ToolBox::wait(200);
31
        timeout--;
34
        timeout--;
32
        if (serial->isOpen()) {
35
        if (serial->isOpen()) {
33
            serial->receiveData();
36
            serial->receiveData();
-
 
37
            connect(serial, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_received_data(const QByteArray &)));
34
            connection_established();
38
            connection_established();
35
            //break while loop
39
            //break while loop
36
            return;
40
            return;
37
        }
41
        }
38
    }
42
    }
39
    //TODO: Error message, because communication could not established
43
    //TODO: Error message, because communication could not established
40
};
44
};
41
 
45
 
42
/**
46
/**
43
 * received new data from MK
47
 * received new data from MK
44
 */
48
 */
45
 
49
 
46
 
50
 
47
/**
51
/**
48
 * send command to Mikrokopter
52
 * send command to Mikrokopter
49
 */
53
 */
50
void QTSerialCommunication::send_cmd(char cmd, int address, char data[150], unsigned int length, bool resend) {
54
void QTSerialCommunication::send_cmd(char cmd, int address, char data[150], unsigned int length, bool resend) {
51
    if (is_connected()) {
55
    if (is_connected()) {
52
        Parser::create_frame(cmd, address, data, length);
56
        Parser::create_frame(cmd, address, data, length);
53
        if (resend) {
57
        if (resend) {
54
            //resend evrey 2 seconds
58
            //resend every 2 seconds
55
            resendTimer.start(2000);
59
            resendTimer.start(2000);
56
            resendData = data;
60
            resendData = data;
57
        }
61
        }
-
 
62
        //TODO: save outgoing data for debug purpose
-
 
63
        std::cout << "send: " << data << std::endl;
58
        QByteArray temp(data);
64
        QByteArray temp(data);
59
        serial->sendData(temp);
65
        serial->sendData(temp);
60
    } else {
66
    } else {
61
        //FIXME: Error message: not connected
67
        //FIXME: Error message: not connected
62
    }
68
    }
63
};
69
};
64
 
70
 
65
/**
71
/**
66
 * stop sending commands to Mikrokopter
72
 * stop sending commands to Mikrokopter
67
 * stop timer
73
 * stop timer
68
 */
74
 */
69
void QTSerialCommunication::stop_resend() {
75
void QTSerialCommunication::stop_resend() {
70
   
-
 
-
 
76
    resendTimer.stop();
71
};
77
};
72
 
78
 
73
/**
79
/**
74
 * resend timer timout
80
 * resend timer timout
75
 */
81
 */
76
void QTSerialCommunication::slot_resend_timer() {
82
void QTSerialCommunication::slot_resend_timer() {
77
}
83
}
-
 
84
 
-
 
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
78
 
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
 
79
void QTSerialCommunication::received_data(char * data) {
103
    handler->receive_data(data, length);
80
}
104
}