Subversion Repositories Projects

Rev

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

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