Subversion Repositories Projects

Rev

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

Rev 449 Rev 451
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
    i_Type = 0;
23
    i_Type = 0;
24
 
24
 
25
    TTY = new ManageSerialPort();
25
    TTY = new ManageSerialPort();
26
    o_Timer = new QTimer(this);
26
    //o_Timer = new QTimer(this);
27
 
27
 
28
    connect(o_Timer,   SIGNAL(timeout()),       SLOT(slot_Timer()));
28
    //connect(o_Timer,   SIGNAL(timeout()),       SLOT(slot_Timer()));
29
 
29
 
30
    TTY->setBaudRate(BAUD57600);   //BaudRate
30
    TTY->setBaudRate(BAUD57600);   //BaudRate
31
    TTY->setDataBits(DATA_8);      //DataBits
31
    TTY->setDataBits(DATA_8);      //DataBits
32
    TTY->setParity(PAR_NONE);      //Parity
32
    TTY->setParity(PAR_NONE);      //Parity
33
    TTY->setStopBits(STOP_1);      //StopBits
33
    TTY->setStopBits(STOP_1);      //StopBits
34
    TTY->setFlowControl(FLOW_OFF); //FlowControl
34
    TTY->setFlowControl(FLOW_OFF); //FlowControl
35
 
35
 
36
    TTY->setTimeout(0, 10);
36
    TTY->setTimeout(0, 10);
37
    TTY->enableSending();
37
    TTY->enableSending();
38
    TTY->enableReceiving();
38
    TTY->enableReceiving();
39
 
39
 
40
    b_isOpen = false;
40
    b_isOpen = false;
41
}
41
}
42
 
42
 
43
void cConnection::new_Data(QString Data)
43
void cConnection::new_Data(QString Data)
44
{
44
{
45
    //Data = Data;
45
    //Data = Data;
46
 
46
 
47
/*    while ((RxData.str.size() > 1) && (RxData.str.substr(1,1) == string("#")))
47
/*    while ((RxData.str.size() > 1) && (RxData.str.substr(1,1) == string("#")))
48
    {
48
    {
49
        RxData.str = RxData.str.substr(1, RxData.str.size());
49
        RxData.str = RxData.str.substr(1, RxData.str.size());
50
    }
50
    }
51
 
51
 
52
    if (Parser::check_CRC(RxData.str))
52
    if (Parser::check_CRC(RxData.str))
53
    {
53
    {
54
        RxData.input = (char *)RxData.str.c_str();
54
        RxData.input = (char *)RxData.str.c_str();
55
        emit(newData(RxData));
55
        emit(newData(RxData));
56
    }
56
    }
57
    else
57
    else
58
    {
58
    {
59
        emit(showTerminal(2, QString(RxData.str.c_str())));
59
        emit(showTerminal(2, QString(RxData.str.c_str())));
60
    }
60
    }
61
*/
61
*/
62
}
62
}
63
 
63
 
64
void cConnection::slot_newDataReceived(const QByteArray &dataReceived)
64
void cConnection::slot_newDataReceived(const QByteArray &dataReceived)
65
{
65
{
66
/*
66
/*
67
    const char *RXt;
67
    const char *RXt;
68
    RXt = dataReceived.data();
68
    RXt = dataReceived.data();
69
    int a = 0;
69
    int a = 0;
70
 
70
 
71
    while (RXt[a] != '\0')
71
    while (RXt[a] != '\0')
72
    {
72
    {
73
        if (RXt[a] == '\r')
73
        if (RXt[a] == '\r')
74
        {
74
        {
75
            new_Data(QString(""));
75
            new_Data(QString(""));
76
            RxData.str = string("");
76
            RxData.str = string("");
77
        }
77
        }
78
        else
78
        else
79
        {
79
        {
80
            RxData.str = RxData.str + string(&RXt[a]);
80
            RxData.str = RxData.str + string(&RXt[a]);
81
        }
81
        }
82
        a++;
82
        a++;
83
    }
83
    }
84
*/
84
*/
85
}
85
}
86
 
86
 
87
bool cConnection::isOpen()
87
bool cConnection::isOpen()
88
{
88
{
89
    return b_isOpen;
89
    return b_isOpen;
90
}
90
}
91
 
91
 
92
bool cConnection::Close()
92
bool cConnection::Close()
93
{
93
{
94
    switch(i_Type)
94
    switch(i_Type)
95
    {
95
    {
96
        case C_TTY :
96
        case C_TTY :
97
        {
97
        {
98
            TTY->close();
98
            TTY->close();
99
 
99
 
100
            disconnect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, 0);
100
            disconnect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, 0);
101
 
101
 
102
            b_isOpen = false;
102
            b_isOpen = false;
103
        }
103
        }
104
        break;
104
        break;
105
        case C_IP :
105
        case C_IP :
106
        {
106
        {
107
            TcpSocket->disconnectFromHost();
107
            TcpSocket->disconnectFromHost();
108
            disconnect(TcpSocket, SIGNAL(connected()), 0, 0);
108
            disconnect(TcpSocket, SIGNAL(connected()), 0, 0);
109
            disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0);
109
            disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0);
110
            disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
110
            disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
111
 
111
 
112
            b_isOpen = false;
112
            b_isOpen = false;
113
        }
113
        }
114
        break;
114
        break;
115
    }
115
    }
116
 
116
 
117
    return b_isOpen;
117
    return b_isOpen;
118
}
118
}
119
 
119
 
120
bool cConnection::Open(int Typ, QString Address)
120
bool cConnection::Open(int Typ, QString Address)
121
{
121
{
122
    switch(Typ)
122
    switch(Typ)
123
    {
123
    {
124
        case C_TTY :
124
        case C_TTY :
125
        {
125
        {
126
            TTY->setPort(Address); //Port
126
            TTY->setPort(Address); //Port
127
            TTY->open();
127
            TTY->open();
128
 
128
 
129
            ToolBox::wait(1000);
129
            ToolBox::wait(1000);
130
 
130
 
131
            if (TTY->isOpen())
131
            if (TTY->isOpen())
132
            {
132
            {
133
                connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));
133
                connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));
134
 
134
 
135
                TTY->receiveData();
135
                TTY->receiveData();
136
                b_isOpen = true;
136
                b_isOpen = true;
137
                i_Type = C_TTY;
137
                i_Type = C_TTY;
138
            }
138
            }
139
        }
139
        }
140
        break;
140
        break;
141
        case C_IP :
141
        case C_IP :
142
        {
142
        {
143
            QStringList Host = Address.split(":");
143
            QStringList Host = Address.split(":");
144
 
144
 
145
            TcpSocket = new(QTcpSocket);
145
            TcpSocket = new(QTcpSocket);
146
            TcpSocket->connectToHost (Host[1], Host[2].toInt());
146
            TcpSocket->connectToHost (Host[1], Host[2].toInt());
147
 
147
 
148
            connect(TcpSocket, SIGNAL(connected()), this, SLOT(slot_IP_Connected()) );
148
            connect(TcpSocket, SIGNAL(connected()), this, SLOT(slot_IP_Connected()) );
149
//            connect(TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_Error(QAbstractSocket::SocketError)));
149
//            connect(TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_Error(QAbstractSocket::SocketError)));
150
            b_isOpen = true;
150
            b_isOpen = true;
151
            i_Type = C_IP;
151
            i_Type = C_IP;
152
        }
152
        }
153
        break;
153
        break;
154
    }
154
    }
155
 
155
 
156
    return b_isOpen;
156
    return b_isOpen;
157
}
157
}
158
 
158
 
159
void cConnection::slot_IP_Connected()
159
void cConnection::slot_IP_Connected()
160
{
160
{
161
    connect(TcpSocket, SIGNAL(readyRead()), SLOT(slot_IP_ReadLine()));
161
    connect(TcpSocket, SIGNAL(readyRead()), SLOT(slot_IP_ReadLine()));
162
    connect(TcpSocket, SIGNAL(disconnected()),TcpSocket, SLOT(deleteLater()));
162
    connect(TcpSocket, SIGNAL(disconnected()),TcpSocket, SLOT(deleteLater()));
163
    connect(TcpSocket, SIGNAL(disconnected()),this, SLOT(slot_IP_Disconnect()));
163
    connect(TcpSocket, SIGNAL(disconnected()),this, SLOT(slot_IP_Disconnect()));
164
 
164
 
165
//    emit sig_Connected();
165
//    emit sig_Connected();
166
}
166
}
167
 
167
 
168
void cConnection::slot_IP_Disconnect()
168
void cConnection::slot_IP_Disconnect()
169
{
169
{
170
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
170
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
171
    disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0);
171
    disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0);
172
//    emit sig_Disconnected(1);
172
//    emit sig_Disconnected(1);
173
}
173
}
174
 
174
 
175
void cConnection::slot_IP_ReadLine()
175
void cConnection::slot_IP_ReadLine()
176
{
176
{
177
    QString Input = QString(TcpSocket->readLine(TcpSocket->bytesAvailable())).remove(QChar(' '));
177
    QString Input = QString(TcpSocket->readLine(TcpSocket->bytesAvailable())).remove(QChar(' '));
178
 
178
 
179
    int Len = Input.length();
179
    int Len = Input.length();
180
 
180
 
181
    for (int z = 0; z < Len; z++)
181
    for (int z = 0; z < Len; z++)
182
    {
182
    {
183
        if (Input[z] == '\r')
183
        if (Input[z] == '\r')
184
        {
184
        {
185
            new_Data(QString(""));
185
            new_Data(QString(""));
186
            //RxData.str = string("");
186
            //RxData.str = string("");
187
        }
187
        }
188
        else
188
        else
189
        {
189
        {
190
            //RxData.str = RxData.str + Input[z].toAscii();
190
            //RxData.str = RxData.str + Input[z].toAscii();
191
        }
191
        }
192
    }
192
    }
193
}
193
}
194
 
194
 
195
bool cConnection::send_Cmd(char CMD, int Address, char Data[MAX_DATA_SIZE],unsigned int Length, bool Resend)
195
bool cConnection::send_Cmd(char CMD, int Address, char Data[MAX_DATA_SIZE],unsigned int Length, bool Resend)
196
{
196
{
197
    if (b_isOpen) //is_connected()
197
    if (b_isOpen) //is_connected()
198
    {
198
    {
199
        QByteArray Temp;
199
        QByteArray Temp;
200
/*
200
/*
201
        if (CMD != '#')
201
        if (CMD != '#')
202
        {
202
        {
203
            TX_Data = Parser::encode64(Data, Length);
203
            TX_Data = Parser::encode64(Data, Length);
204
            string TX_Data;
204
            string TX_Data;
205
            char addr = 'a' + Address;
205
            char addr = 'a' + Address;
206
            TX_Data = string("#") + (string(&addr)) + string(&CMD) + TX_Data;
206
            TX_Data = string("#") + (string(&addr)) + string(&CMD) + TX_Data;
207
            TX_Data = Parser::add_CRC(TX_Data) + '\r';
207
            TX_Data = Parser::add_CRC(TX_Data) + '\r';
208
 
208
 
209
            Temp = QByteArray(TX_Data.c_str());
209
            Temp = QByteArray(TX_Data.c_str());
210
 
210
 
211
            if (Resend)
211
            if (Resend)
212
            {
212
            {
213
                o_Timer->start(2000);
213
                o_Timer->start(2000);
214
                s_ReSend = Temp;
214
                s_ReSend = Temp;
215
            }
215
            }
216
            emit(showTerminal(3, QString(TX_Data.c_str())));
216
            emit(showTerminal(3, QString(TX_Data.c_str())));
217
        }
217
        }
218
        else
218
        else
219
        {
219
        {
220
            for (unsigned int a = 0; a < Length; a++)
220
            for (unsigned int a = 0; a < Length; a++)
221
            {
221
            {
222
                Temp[a] = Data[a];
222
                Temp[a] = Data[a];
223
            }
223
            }
224
        }
224
        }
225
*/
225
*/
226
        switch(i_Type)
226
        switch(i_Type)
227
        {
227
        {
228
            case C_TTY :
228
            case C_TTY :
229
            {
229
            {
230
                TTY->sendData(Temp);
230
                TTY->sendData(Temp);
231
            }
231
            }
232
            break;
232
            break;
233
            case C_IP :
233
            case C_IP :
234
            {
234
            {
235
                TcpSocket->write(Temp);
235
                TcpSocket->write(Temp);
236
            }
236
            }
237
            break;
237
            break;
238
        }
238
        }
239
    }
239
    }
240
    return true;
240
    return true;
241
}
241
}
242
 
242
 
243
void cConnection::stop_ReSend()
243
void cConnection::stop_ReSend()
244
{
244
{
245
    o_Timer->stop();
245
    //o_Timer->stop();
246
}
246
}
247
 
247
 
248
void cConnection::slot_Timer()
248
void cConnection::slot_Timer()
249
{
249
{
250
        switch(i_Type)
250
        switch(i_Type)
251
        {
251
        {
252
            case C_TTY :
252
            case C_TTY :
253
            {
253
            {
254
                TTY->sendData(s_ReSend);
254
                TTY->sendData(s_ReSend);
255
            }
255
            }
256
            break;
256
            break;
257
            case C_IP :
257
            case C_IP :
258
            {
258
            {
259
                TcpSocket->write(s_ReSend);
259
                TcpSocket->write(s_ReSend);
260
            }
260
            }
261
            break;
261
            break;
262
        }
262
        }
263
 
263
 
264
        emit(showTerminal(3, QString(s_ReSend)));
264
        emit(showTerminal(3, QString(s_ReSend)));
265
}
265
}
266
 
266