Subversion Repositories Projects

Rev

Rev 462 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 462 Rev 500
Line 1... Line 1...
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
 
Line 4... Line 6...
4
 
6
 
5
/**
7
/**
6
 * initiate connection and stuff
8
 * initiate connection and stuff
7
 */
9
 */
-
 
10
QTSerialCommunication::QTSerialCommunication(Handler * handler) {
8
QTSerialCommunication::QTSerialCommunication() {
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
Line 29... Line 32...
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
    }
Line 49... Line 53...
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
    }
Line 65... Line 71...
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
};
Line 72... Line 78...
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() {
Line -... Line 83...
-
 
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
/**
77
}
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
78
 
102
 
79
void QTSerialCommunication::received_data(char * data) {
103
    handler->receive_data(data, length);