Rev 711 | Go to most recent revision | Details | 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); |
||
84 | |||
85 | // qDebug(t_Data.toLatin1().data()); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | void Input_TCP::send_RawData(char *t_Data) |
||
90 | { |
||
91 | t_Data = t_Data; |
||
92 | } |
||
93 | |||
94 | void Input_TCP::stop_Resend(int ID) |
||
95 | { |
||
96 | if ((ID > 0) && (ID < MAX_Confirm)) |
||
97 | { |
||
98 | Confirm[ID].ID = false; |
||
99 | Confirm[ID].Data = ""; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | void Input_TCP::slot_Timer() |
||
104 | { |
||
105 | bool Active = false; |
||
106 | |||
107 | for (int x = 0; x < MAX_Confirm; x++) |
||
108 | { |
||
109 | if (Confirm[x].ID) |
||
110 | { |
||
111 | Active = true; |
||
112 | send_Data(Confirm[x].Data); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | if (Active == false) |
||
117 | { |
||
118 | Timer->stop(); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | void Input_TCP::slot_TCP_Connected() |
||
123 | { |
||
124 | b_Open = true; |
||
125 | connect(TCP_Socket, SIGNAL(readyRead()), SLOT(slot_TCP_ReadLine())); |
||
126 | connect(TCP_Socket, SIGNAL(disconnected()),TCP_Socket, SLOT(deleteLater())); |
||
127 | connect(TCP_Socket, SIGNAL(disconnected()),this, SLOT(slot_TCP_Disconnect())); |
||
128 | |||
129 | emit sig_Connected(); |
||
130 | } |
||
131 | |||
132 | void Input_TCP::slot_TCP_Disconnect() |
||
133 | { |
||
134 | disconnect(TCP_Socket, SIGNAL(disconnected()), 0, 0); |
||
135 | disconnect(TCP_Socket, SIGNAL(readyRead()), 0, 0); |
||
136 | // emit sig_Disconnected(1); |
||
137 | } |
||
138 | |||
139 | void Input_TCP::slot_TCP_ReadLine() |
||
140 | { |
||
141 | // if (TCP_Socket->canReadLine()) |
||
142 | { |
||
143 | QString t_Data = QString(TCP_Socket->readLine(TCP_Socket->bytesAvailable())).remove(QChar('\n')); |
||
144 | |||
145 | if ((t_Data.length() > 3) && (t_Data[t_Data.length() - 1] == '\r')) |
||
146 | { |
||
147 | emit(sig_NewData(t_Data)); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | |||
152 | /* int Len = Input.length(); |
||
153 | |||
154 | for (int z = 0; z < Len; z++) |
||
155 | { |
||
156 | if (Input[z] == '\r') |
||
157 | { |
||
158 | new_Data(QString("")); |
||
159 | RxData.String = QString(""); |
||
160 | } |
||
161 | else |
||
162 | { |
||
163 | RxData.String = RxData.String + Input[z]; |
||
164 | } |
||
165 | } |
||
166 | */ |
||
167 | } |
||
168 | |||
169 | void Input_TCP::slot_TCP_Error(QAbstractSocket::SocketError Error) |
||
170 | { |
||
171 | b_Open = false; |
||
172 | |||
173 | disconnect(TCP_Socket, SIGNAL(disconnected()), 0, 0); |
||
174 | disconnect(TCP_Socket, SIGNAL(error(QAbstractSocket::SocketError)), 0, 0); |
||
175 | disconnect(TCP_Socket, SIGNAL(connected()), 0, 0); |
||
176 | disconnect(TCP_Socket, SIGNAL(readyRead()), 0, 0); |
||
177 | |||
178 | // qDebug("Error"); |
||
179 | switch (Error) |
||
180 | { |
||
181 | case QAbstractSocket::ConnectionRefusedError: |
||
182 | emit sig_Disconnected(REFUSED); |
||
183 | break; |
||
184 | case QAbstractSocket::RemoteHostClosedError: |
||
185 | emit sig_Disconnected(REMOTECLOSED); |
||
186 | break; |
||
187 | default: |
||
188 | emit sig_Disconnected(CLOSED); |
||
189 | break; |
||
190 | } |
||
191 | // emit sig_Disconnected(CLOSED); |
||
192 | } |
||
193 | |||
194 | eMode Input_TCP::Mode() |
||
195 | { |
||
196 | return TCP; |
||
197 | } |
||
198 |