Subversion Repositories Projects

Rev

Rev 358 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
250 KeyOz 1
/***************************************************************************
2
 *   Copyright (C) 2009 by Manuel Schrape                                  *
3
 *   manuel.schrape@gmx.de                                                 *
4
 *                                                                         *
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  *
7
 *   the Free Software Foundation; either version 2 of the License.        *
8
 *                                                                         *
9
 *   This program is distributed in the hope that it will be useful,       *
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12
 *   GNU General Public License for more details.                          *
13
 *                                                                         *
14
 *   You should have received a copy of the GNU General Public License     *
15
 *   along with this program; if not, write to the                         *
16
 *   Free Software Foundation, Inc.,                                       *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 ***************************************************************************/
19
#include "cConnection.h"
20
 
21
cConnection::cConnection()
22
{
263 KeyOz 23
    i_Type = 0;
24
 
250 KeyOz 25
    TTY = new ManageSerialPort();
313 KeyOz 26
    o_Timer = new QTimer(this);
250 KeyOz 27
 
313 KeyOz 28
    connect(o_Timer,   SIGNAL(timeout()),       SLOT(slot_Timer()));
29
 
358 KeyOz 30
    TTY->setBaudRate(BAUD57600);   //BaudRate
31
    TTY->setDataBits(DATA_8);      //DataBits
32
    TTY->setParity(PAR_NONE);      //Parity
33
    TTY->setStopBits(STOP_1);      //StopBits
263 KeyOz 34
    TTY->setFlowControl(FLOW_OFF); //FlowControl
35
 
36
    TTY->setTimeout(0, 10);
37
    TTY->enableSending();
38
    TTY->enableReceiving();
39
 
250 KeyOz 40
    b_isOpen = false;
41
}
42
 
263 KeyOz 43
void cConnection::new_Data(QString Data)
44
{
307 KeyOz 45
    Data = Data;
46
 
263 KeyOz 47
    while ((RxData.String.length() > 1) && (RxData.String.at(1) == '#'))
48
    {
49
        RxData.String.remove(0,1);
50
    }
51
 
52
    if (ToolBox::check_CRC(RxData.String))
53
    {
54
        RxData.Input = RxData.String.toLatin1().data();
55
        emit(newData(RxData));
56
    }
57
    else
58
    {
59
        emit(showTerminal(2, RxData.String));
60
    }
61
 
62
}
63
 
250 KeyOz 64
void cConnection::slot_newDataReceived(const QByteArray &dataReceived)
65
{
66
    const char *RXt;
67
    RXt = dataReceived.data();
68
    int a = 0;
69
 
70
    while (RXt[a] != '\0')
71
    {
72
        if (RXt[a] == '\r')
73
        {
263 KeyOz 74
            new_Data(QString(""));
250 KeyOz 75
            RxData.String = QString("");
76
        }
77
        else
78
        {
79
            RxData.String = RxData.String + QString(RXt[a]);
80
        }
81
        a++;
82
    }
83
}
84
 
85
bool cConnection::isOpen()
86
{
87
    return b_isOpen;
88
}
89
 
90
bool cConnection::Close()
91
{
263 KeyOz 92
    switch(i_Type)
93
    {
94
        case C_TTY :
95
        {
96
            TTY->close();
250 KeyOz 97
 
263 KeyOz 98
            disconnect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, 0);
250 KeyOz 99
 
263 KeyOz 100
            b_isOpen = false;
101
        }
102
        break;
103
        case C_IP :
104
        {
105
            TcpSocket->disconnectFromHost();
106
            disconnect(TcpSocket, SIGNAL(connected()), 0, 0);
107
            disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0);
108
            disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
259 KeyOz 109
 
263 KeyOz 110
            b_isOpen = false;
111
        }
112
        break;
113
    }
114
 
250 KeyOz 115
    return b_isOpen;
116
}
117
 
263 KeyOz 118
bool cConnection::Open(int Typ, QString Address)
250 KeyOz 119
{
263 KeyOz 120
    switch(Typ)
121
    {
122
        case C_TTY :
123
        {
124
            TTY->setPort(Address); //Port
125
            TTY->open();
250 KeyOz 126
 
263 KeyOz 127
            ToolBox::Wait(1000);
250 KeyOz 128
 
263 KeyOz 129
            if (TTY->isOpen())
130
            {
131
                connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));
250 KeyOz 132
 
263 KeyOz 133
                TTY->receiveData();
134
                b_isOpen = true;
135
                i_Type = C_TTY;
136
            }
137
        }
138
        break;
139
        case C_IP :
140
        {
141
            QStringList Host = Address.split(":");
250 KeyOz 142
 
263 KeyOz 143
            TcpSocket = new(QTcpSocket);
144
            TcpSocket->connectToHost (Host[1], Host[2].toInt());
145
 
146
            connect(TcpSocket, SIGNAL(connected()), this, SLOT(slot_IP_Connected()) );
147
//            connect(TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_Error(QAbstractSocket::SocketError)));
148
            b_isOpen = true;
149
            i_Type = C_IP;
150
        }
151
        break;
250 KeyOz 152
    }
153
 
154
    return b_isOpen;
155
}
156
 
263 KeyOz 157
void cConnection::slot_IP_Connected()
158
{
159
    connect(TcpSocket, SIGNAL(readyRead()), SLOT(slot_IP_ReadLine()));
160
    connect(TcpSocket, SIGNAL(disconnected()),TcpSocket, SLOT(deleteLater()));
161
    connect(TcpSocket, SIGNAL(disconnected()),this, SLOT(slot_IP_Disconnect()));
162
 
163
//    emit sig_Connected();
164
}
165
 
166
void cConnection::slot_IP_Disconnect()
167
{
168
    disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0);
169
    disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0);
170
//    emit sig_Disconnected(1);
171
}
172
 
173
void cConnection::slot_IP_ReadLine()
174
{
175
    QString Input = QString(TcpSocket->readLine(TcpSocket->bytesAvailable())).remove(QChar(' '));
176
 
177
    int Len = Input.length();
178
 
179
    for (int z = 0; z < Len; z++)
180
    {
181
        if (Input[z] == '\r')
182
        {
183
            new_Data(QString(""));
184
            RxData.String = QString("");
185
        }
186
        else
187
        {
188
            RxData.String = RxData.String + Input[z];
189
        }
190
    }
191
}
192
 
250 KeyOz 193
bool cConnection::send_Cmd(char CMD, int Address, char Data[150],unsigned int Length, bool Resend)
194
{
195
    if (b_isOpen)
196
    {
197
        QByteArray Temp;
198
        QString TX_Data;
199
 
200
        if (CMD != '#')
201
        {
202
            TX_Data = ToolBox::Encode64(Data, Length);
203
 
204
            TX_Data = QString("#") + (QString('a' + Address)) + QString(CMD) + TX_Data;
205
            TX_Data = ToolBox::add_CRC(TX_Data) + '\r';
206
 
313 KeyOz 207
            Temp = QByteArray(TX_Data.toUtf8());
208
 
250 KeyOz 209
            if (Resend)
210
            {
525 KeyOz 211
                o_Timer->start(TICKER);
313 KeyOz 212
                s_ReSend = Temp;
250 KeyOz 213
            }
214
        }
215
        else
216
        {
217
            for (unsigned int a = 0; a < Length; a++)
218
            {
219
                Temp[a] = Data[a];
220
            }
221
        }
222
 
263 KeyOz 223
        switch(i_Type)
224
        {
225
            case C_TTY :
226
            {
227
                TTY->sendData(Temp);
228
            }
229
            break;
230
            case C_IP :
231
            {
232
                TcpSocket->write(Temp);
233
            }
234
            break;
235
        }
250 KeyOz 236
 
237
        emit(showTerminal(3, TX_Data));
238
    }
239
    return true;
240
}
313 KeyOz 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
}