Subversion Repositories Projects

Rev

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

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