Subversion Repositories Projects

Rev

Rev 715 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
674 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 "Input_TCP.h"
20
 
21
void Input_TCP::Init()
22
{
23
    b_Open = false;
24
    Timer = new QTimer();
715 KeyOz 25
    s_Buffer = "";
674 KeyOz 26
    connect(Timer, SIGNAL(timeout()), this, SLOT(slot_Timer()));
27
 
28
    for (int z = 0; z < MAX_Confirm; z++)
29
    {
30
        Confirm[z].ID = false;
31
    }
32
}
33
 
34
bool Input_TCP::Open(set_Input s_Input)
35
{
36
    TCP_Socket = new(QTcpSocket);
37
    TCP_Socket->connectToHost (s_Input.Main, s_Input.Sub.toInt());
38
 
39
    connect(TCP_Socket, SIGNAL(connected()), this, SLOT(slot_TCP_Connected()) );
40
    connect(TCP_Socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_TCP_Error(QAbstractSocket::SocketError)));
41
 
42
    return true;
43
}
44
 
45
bool Input_TCP::IsOpen()
46
{
47
    return b_Open;
48
}
49
 
50
bool Input_TCP::Close()
51
{
52
    TCP_Socket->disconnectFromHost();
53
    disconnect(TCP_Socket, SIGNAL(connected()), 0, 0);
54
    disconnect(TCP_Socket, SIGNAL(readyRead()), 0, 0);
55
    disconnect(TCP_Socket, SIGNAL(disconnected()), 0, 0);
56
 
57
    b_Open = false;
58
    Timer->stop();
59
 
60
    return true;
61
}
62
 
63
void Input_TCP::send_Data(QString t_Data, int ID)
64
{
65
    if ((ID > 0) && (ID < MAX_Confirm))
66
    {
67
        Timer->start(2500);
68
        Confirm[ID].ID = true;
69
        Confirm[ID].Data = t_Data;
70
    }
71
 
72
    if (b_Open)
73
    {
74
        usleep(50000);
75
 
76
        if (t_Data[t_Data.length() - 1] != '\r')
77
        {
78
            t_Data = t_Data + "\r";
79
        }
80
 
81
        QByteArray Temp;
715 KeyOz 82
        Temp = QByteArray(QString(t_Data + "\n").toAscii());
674 KeyOz 83
 
84
        TCP_Socket->write(Temp);
711 KeyOz 85
        TCP_Socket->flush();
674 KeyOz 86
 
87
//        qDebug(t_Data.toLatin1().data());
88
    }
89
}
90
 
91
void Input_TCP::send_RawData(char *t_Data)
92
{
93
    t_Data = t_Data;
94
}
95
 
96
void Input_TCP::stop_Resend(int ID)
97
{
98
    if ((ID > 0) && (ID < MAX_Confirm))
99
    {
100
        Confirm[ID].ID = false;
101
        Confirm[ID].Data = "";
102
    }
103
}
104
 
105
void Input_TCP::slot_Timer()
106
{
107
    bool Active = false;
108
 
109
    for (int x = 0; x < MAX_Confirm; x++)
110
    {
111
        if (Confirm[x].ID)
112
        {
113
            Active = true;
114
            send_Data(Confirm[x].Data);
115
        }
116
    }
117
 
118
    if (Active == false)
119
    {
120
        Timer->stop();
121
    }
122
}
123
 
124
void Input_TCP::slot_TCP_Connected()
125
{
126
    b_Open = true;
127
    connect(TCP_Socket, SIGNAL(readyRead()), SLOT(slot_TCP_ReadLine()));
128
    connect(TCP_Socket, SIGNAL(disconnected()),TCP_Socket, SLOT(deleteLater()));
129
    connect(TCP_Socket, SIGNAL(disconnected()),this, SLOT(slot_TCP_Disconnect()));
130
 
131
    emit sig_Connected();
132
}
133
 
134
void Input_TCP::slot_TCP_Disconnect()
135
{
136
    disconnect(TCP_Socket, SIGNAL(disconnected()), 0, 0);
137
    disconnect(TCP_Socket, SIGNAL(readyRead()), 0, 0);
138
//    emit sig_Disconnected(1);
139
}
140
 
141
void Input_TCP::slot_TCP_ReadLine()
142
{
711 KeyOz 143
//    QString t_Data = QString(TCP_Socket->readLine(TCP_Socket->bytesAvailable())).remove(QChar('\n'));
801 - 144
    QString t_Data = s_Buffer + QString(TCP_Socket->readAll()).remove(QChar('\n'));
674 KeyOz 145
 
715 KeyOz 146
    s_Buffer = "";
147
 
801 - 148
    t_Data = t_Data.replace('\r', "\r\n");
149
 
711 KeyOz 150
    QStringList l_Data;
151
    l_Data = t_Data.split('\n');
674 KeyOz 152
 
711 KeyOz 153
    for (int z = 0; z < l_Data.count(); z++)
154
    {
674 KeyOz 155
 
715 KeyOz 156
        if ((l_Data[z][l_Data[z].length() - 1] == '\r'))
674 KeyOz 157
        {
711 KeyOz 158
            emit(sig_NewData(l_Data[z]));
159
//                qDebug(QString("DATA: _" + l_Data[z] + "_ < END").toLatin1().data());
674 KeyOz 160
        }
715 KeyOz 161
        else
162
        {
163
            s_Buffer = s_Buffer + l_Data[z];
164
        }
674 KeyOz 165
    }
166
}
167
 
168
void Input_TCP::slot_TCP_Error(QAbstractSocket::SocketError Error)
169
{
170
    b_Open = false;
171
 
172
    disconnect(TCP_Socket, SIGNAL(disconnected()), 0, 0);
173
    disconnect(TCP_Socket, SIGNAL(error(QAbstractSocket::SocketError)), 0, 0);
174
    disconnect(TCP_Socket, SIGNAL(connected()), 0, 0);
175
    disconnect(TCP_Socket, SIGNAL(readyRead()), 0, 0);
176
 
177
//    qDebug("Error");
178
    switch (Error)
179
    {
180
        case QAbstractSocket::ConnectionRefusedError:
181
            emit sig_Disconnected(REFUSED);
182
        break;
183
        case QAbstractSocket::RemoteHostClosedError:
184
            emit sig_Disconnected(REMOTECLOSED);
185
        break;
186
        default:
187
            emit sig_Disconnected(CLOSED);
188
        break;
189
    }
190
//    emit sig_Disconnected(CLOSED);
191
}
192
 
193
eMode Input_TCP::Mode()
194
{
195
    return TCP;
196
}
197