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