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