Subversion Repositories Projects

Rev

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

Rev 250 Rev 259
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) 2009 by Manuel Schrape                                  *
2
 *   Copyright (C) 2009 by Manuel Schrape                                  *
3
 *   manuel.schrape@gmx.de                                                 *
3
 *   manuel.schrape@gmx.de                                                 *
4
 *                                                                         *
4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or modify  *
5
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   it under the terms of the GNU General Public License as published by  *
6
 *   it under the terms of the GNU General Public License as published by  *
7
 *   the Free Software Foundation; either version 2 of the License.        *
7
 *   the Free Software Foundation; either version 2 of the License.        *
8
 *                                                                         *
8
 *                                                                         *
9
 *   This program is distributed in the hope that it will be useful,       *
9
 *   This program is distributed in the hope that it will be useful,       *
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 *   GNU General Public License for more details.                          *
12
 *   GNU General Public License for more details.                          *
13
 *                                                                         *
13
 *                                                                         *
14
 *   You should have received a copy of the GNU General Public License     *
14
 *   You should have received a copy of the GNU General Public License     *
15
 *   along with this program; if not, write to the                         *
15
 *   along with this program; if not, write to the                         *
16
 *   Free Software Foundation, Inc.,                                       *
16
 *   Free Software Foundation, Inc.,                                       *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 ***************************************************************************/
18
 ***************************************************************************/
19
#include "cConnection.h"
19
#include "cConnection.h"
20
 
20
 
21
cConnection::cConnection()
21
cConnection::cConnection()
22
{
22
{
23
    TTY = new ManageSerialPort();
23
    TTY = new ManageSerialPort();
24
 
24
 
25
    b_isOpen = false;
25
    b_isOpen = false;
26
}
26
}
27
 
27
 
28
void cConnection::slot_newDataReceived(const QByteArray &dataReceived)
28
void cConnection::slot_newDataReceived(const QByteArray &dataReceived)
29
{
29
{
30
    const char *RXt;
30
    const char *RXt;
31
    RXt = dataReceived.data();
31
    RXt = dataReceived.data();
32
    int a = 0;
32
    int a = 0;
33
 
33
 
34
    while (RXt[a] != '\0')
34
    while (RXt[a] != '\0')
35
    {
35
    {
36
        if (RXt[a] == '\r')
36
        if (RXt[a] == '\r')
37
        {
37
        {
38
            while ((RxData.String.length() > 1) && (RxData.String.at(1) == '#'))
38
            while ((RxData.String.length() > 1) && (RxData.String.at(1) == '#'))
39
            {
39
            {
40
                RxData.String.remove(0,1);
40
                RxData.String.remove(0,1);
41
            }
41
            }
42
 
42
 
43
            if (ToolBox::check_CRC(RxData.String))
43
            if (ToolBox::check_CRC(RxData.String))
44
            {
44
            {
45
                RxData.Input = RxData.String.toLatin1().data();
45
                RxData.Input = RxData.String.toLatin1().data();
46
                emit(newData(RxData));
46
                emit(newData(RxData));
47
//                emit(showTerminal(1, RxData.String));
47
//                emit(showTerminal(1, RxData.String));
48
            }
48
            }
49
            else
49
            else
50
            {
50
            {
51
                emit(showTerminal(2, RxData.String));
51
                emit(showTerminal(2, RxData.String));
52
            }
52
            }
53
            RxData.String = QString("");
53
            RxData.String = QString("");
54
        }
54
        }
55
        else
55
        else
56
        {
56
        {
57
            RxData.String = RxData.String + QString(RXt[a]);
57
            RxData.String = RxData.String + QString(RXt[a]);
58
        }
58
        }
59
        a++;
59
        a++;
60
    }
60
    }
61
}
61
}
62
 
62
 
63
bool cConnection::isOpen()
63
bool cConnection::isOpen()
64
{
64
{
65
    return b_isOpen;
65
    return b_isOpen;
66
}
66
}
67
 
67
 
68
bool cConnection::Close()
68
bool cConnection::Close()
69
{
69
{
70
    TTY->close();
70
    TTY->close();
71
 
71
 
72
    disconnect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, 0);
72
    disconnect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, 0);
-
 
73
 
-
 
74
    b_isOpen = false;
73
 
75
 
74
    return b_isOpen;
76
    return b_isOpen;
75
}
77
}
76
 
78
 
77
bool cConnection::Open(QString Address)
79
bool cConnection::Open(QString Address)
78
{
80
{
79
    TTY->setPort(Address); //Port
81
    TTY->setPort(Address); //Port
80
 
82
 
81
    TTY->setBaudRate(BAUD57600); //BaudRate
83
    TTY->setBaudRate(BAUD57600); //BaudRate
82
    TTY->setDataBits(DATA_8); //DataBits
84
    TTY->setDataBits(DATA_8); //DataBits
83
    TTY->setParity(PAR_NONE); //Parity
85
    TTY->setParity(PAR_NONE); //Parity
84
    TTY->setStopBits(STOP_1); //StopBits
86
    TTY->setStopBits(STOP_1); //StopBits
85
    TTY->setFlowControl(FLOW_OFF); //FlowControl
87
    TTY->setFlowControl(FLOW_OFF); //FlowControl
86
 
88
 
87
    TTY->setTimeout(0, 10);
89
    TTY->setTimeout(0, 10);
88
    TTY->enableSending();
90
    TTY->enableSending();
89
    TTY->enableReceiving();
91
    TTY->enableReceiving();
90
 
92
 
91
    TTY->open();
93
    TTY->open();
92
 
-
 
-
 
94
    ToolBox::Wait(1000);
-
 
95
    qDebug("Try Open");
93
    if (TTY->isOpen())
96
    if (TTY->isOpen())
94
    {
97
    {
-
 
98
        qDebug("Opend");
95
        connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));
99
        connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));
96
 
100
 
97
        TTY->receiveData();
101
        TTY->receiveData();
98
        b_isOpen = true;
102
        b_isOpen = true;
99
    }
103
    }
100
 
104
 
101
    return b_isOpen;
105
    return b_isOpen;
102
}
106
}
103
 
107
 
104
bool cConnection::send_Cmd(char CMD, int Address, char Data[150],unsigned int Length, bool Resend)
108
bool cConnection::send_Cmd(char CMD, int Address, char Data[150],unsigned int Length, bool Resend)
105
{
109
{
106
    if (b_isOpen)
110
    if (b_isOpen)
107
    {
111
    {
108
        QByteArray Temp;
112
        QByteArray Temp;
109
        QString TX_Data;
113
        QString TX_Data;
110
 
114
 
111
        if (CMD != '#')
115
        if (CMD != '#')
112
        {
116
        {
113
            TX_Data = ToolBox::Encode64(Data, Length);
117
            TX_Data = ToolBox::Encode64(Data, Length);
114
 
118
 
115
            TX_Data = QString("#") + (QString('a' + Address)) + QString(CMD) + TX_Data;
119
            TX_Data = QString("#") + (QString('a' + Address)) + QString(CMD) + TX_Data;
116
            TX_Data = ToolBox::add_CRC(TX_Data) + '\r';
120
            TX_Data = ToolBox::add_CRC(TX_Data) + '\r';
117
 
121
 
118
            if (Resend)
122
            if (Resend)
119
            {
123
            {
120
//                LastSend = TX_Data;
124
//                LastSend = TX_Data;
121
//                TickerEvent[0] = true;
125
//                TickerEvent[0] = true;
122
            }
126
            }
123
            Temp = QByteArray(TX_Data.toUtf8());
127
            Temp = QByteArray(TX_Data.toUtf8());
124
        }
128
        }
125
        else
129
        else
126
        {
130
        {
127
            for (unsigned int a = 0; a < Length; a++)
131
            for (unsigned int a = 0; a < Length; a++)
128
            {
132
            {
129
                Temp[a] = Data[a];
133
                Temp[a] = Data[a];
130
            }
134
            }
131
        }
135
        }
132
 
136
 
133
        TTY->sendData(Temp);
137
        TTY->sendData(Temp);
134
 
138
 
135
        emit(showTerminal(3, TX_Data));
139
        emit(showTerminal(3, TX_Data));
136
    }
140
    }
137
    return true;
141
    return true;
138
}
142
}
139
 
143