Subversion Repositories Projects

Rev

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

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