Rev 449 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
250 | 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 "cConnection.h" |
||
20 | |||
21 | cConnection::cConnection() |
||
22 | { |
||
263 | KeyOz | 23 | i_Type = 0; |
24 | |||
250 | KeyOz | 25 | TTY = new ManageSerialPort(); |
451 | Brean | 26 | //o_Timer = new QTimer(this); |
250 | KeyOz | 27 | |
451 | Brean | 28 | //connect(o_Timer, SIGNAL(timeout()), SLOT(slot_Timer())); |
313 | KeyOz | 29 | |
358 | KeyOz | 30 | TTY->setBaudRate(BAUD57600); //BaudRate |
31 | TTY->setDataBits(DATA_8); //DataBits |
||
32 | TTY->setParity(PAR_NONE); //Parity |
||
33 | TTY->setStopBits(STOP_1); //StopBits |
||
263 | KeyOz | 34 | TTY->setFlowControl(FLOW_OFF); //FlowControl |
35 | |||
36 | TTY->setTimeout(0, 10); |
||
37 | TTY->enableSending(); |
||
38 | TTY->enableReceiving(); |
||
39 | |||
250 | KeyOz | 40 | b_isOpen = false; |
41 | } |
||
42 | |||
263 | KeyOz | 43 | void cConnection::new_Data(QString Data) |
44 | { |
||
396 | Brean | 45 | //Data = Data; |
307 | KeyOz | 46 | |
449 | Brean | 47 | /* while ((RxData.str.size() > 1) && (RxData.str.substr(1,1) == string("#"))) |
263 | KeyOz | 48 | { |
396 | Brean | 49 | RxData.str = RxData.str.substr(1, RxData.str.size()); |
263 | KeyOz | 50 | } |
51 | |||
396 | Brean | 52 | if (Parser::check_CRC(RxData.str)) |
263 | KeyOz | 53 | { |
396 | Brean | 54 | RxData.input = (char *)RxData.str.c_str(); |
263 | KeyOz | 55 | emit(newData(RxData)); |
56 | } |
||
57 | else |
||
58 | { |
||
396 | Brean | 59 | emit(showTerminal(2, QString(RxData.str.c_str()))); |
263 | KeyOz | 60 | } |
449 | Brean | 61 | */ |
263 | KeyOz | 62 | } |
63 | |||
250 | KeyOz | 64 | void cConnection::slot_newDataReceived(const QByteArray &dataReceived) |
65 | { |
||
449 | Brean | 66 | /* |
250 | KeyOz | 67 | const char *RXt; |
68 | RXt = dataReceived.data(); |
||
69 | int a = 0; |
||
70 | |||
71 | while (RXt[a] != '\0') |
||
72 | { |
||
73 | if (RXt[a] == '\r') |
||
74 | { |
||
263 | KeyOz | 75 | new_Data(QString("")); |
396 | Brean | 76 | RxData.str = string(""); |
250 | KeyOz | 77 | } |
78 | else |
||
79 | { |
||
396 | Brean | 80 | RxData.str = RxData.str + string(&RXt[a]); |
250 | KeyOz | 81 | } |
82 | a++; |
||
83 | } |
||
449 | Brean | 84 | */ |
250 | KeyOz | 85 | } |
86 | |||
87 | bool cConnection::isOpen() |
||
88 | { |
||
89 | return b_isOpen; |
||
90 | } |
||
91 | |||
92 | bool cConnection::Close() |
||
93 | { |
||
263 | KeyOz | 94 | switch(i_Type) |
95 | { |
||
96 | case C_TTY : |
||
97 | { |
||
98 | TTY->close(); |
||
250 | KeyOz | 99 | |
263 | KeyOz | 100 | disconnect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, 0); |
250 | KeyOz | 101 | |
263 | KeyOz | 102 | b_isOpen = false; |
103 | } |
||
104 | break; |
||
105 | case C_IP : |
||
106 | { |
||
107 | TcpSocket->disconnectFromHost(); |
||
108 | disconnect(TcpSocket, SIGNAL(connected()), 0, 0); |
||
109 | disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0); |
||
110 | disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0); |
||
259 | KeyOz | 111 | |
263 | KeyOz | 112 | b_isOpen = false; |
113 | } |
||
114 | break; |
||
115 | } |
||
116 | |||
250 | KeyOz | 117 | return b_isOpen; |
118 | } |
||
119 | |||
263 | KeyOz | 120 | bool cConnection::Open(int Typ, QString Address) |
250 | KeyOz | 121 | { |
263 | KeyOz | 122 | switch(Typ) |
123 | { |
||
124 | case C_TTY : |
||
125 | { |
||
126 | TTY->setPort(Address); //Port |
||
127 | TTY->open(); |
||
250 | KeyOz | 128 | |
396 | Brean | 129 | ToolBox::wait(1000); |
250 | KeyOz | 130 | |
263 | KeyOz | 131 | if (TTY->isOpen()) |
132 | { |
||
133 | connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &))); |
||
250 | KeyOz | 134 | |
263 | KeyOz | 135 | TTY->receiveData(); |
136 | b_isOpen = true; |
||
137 | i_Type = C_TTY; |
||
138 | } |
||
139 | } |
||
140 | break; |
||
141 | case C_IP : |
||
142 | { |
||
143 | QStringList Host = Address.split(":"); |
||
250 | KeyOz | 144 | |
263 | KeyOz | 145 | TcpSocket = new(QTcpSocket); |
146 | TcpSocket->connectToHost (Host[1], Host[2].toInt()); |
||
147 | |||
148 | connect(TcpSocket, SIGNAL(connected()), this, SLOT(slot_IP_Connected()) ); |
||
149 | // connect(TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_Error(QAbstractSocket::SocketError))); |
||
150 | b_isOpen = true; |
||
151 | i_Type = C_IP; |
||
152 | } |
||
153 | break; |
||
250 | KeyOz | 154 | } |
155 | |||
156 | return b_isOpen; |
||
157 | } |
||
158 | |||
263 | KeyOz | 159 | void cConnection::slot_IP_Connected() |
160 | { |
||
161 | connect(TcpSocket, SIGNAL(readyRead()), SLOT(slot_IP_ReadLine())); |
||
162 | connect(TcpSocket, SIGNAL(disconnected()),TcpSocket, SLOT(deleteLater())); |
||
163 | connect(TcpSocket, SIGNAL(disconnected()),this, SLOT(slot_IP_Disconnect())); |
||
164 | |||
165 | // emit sig_Connected(); |
||
166 | } |
||
167 | |||
168 | void cConnection::slot_IP_Disconnect() |
||
169 | { |
||
170 | disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0); |
||
171 | disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0); |
||
172 | // emit sig_Disconnected(1); |
||
173 | } |
||
174 | |||
175 | void cConnection::slot_IP_ReadLine() |
||
176 | { |
||
177 | QString Input = QString(TcpSocket->readLine(TcpSocket->bytesAvailable())).remove(QChar(' ')); |
||
178 | |||
179 | int Len = Input.length(); |
||
180 | |||
181 | for (int z = 0; z < Len; z++) |
||
182 | { |
||
183 | if (Input[z] == '\r') |
||
184 | { |
||
185 | new_Data(QString("")); |
||
449 | Brean | 186 | //RxData.str = string(""); |
263 | KeyOz | 187 | } |
188 | else |
||
189 | { |
||
449 | Brean | 190 | //RxData.str = RxData.str + Input[z].toAscii(); |
263 | KeyOz | 191 | } |
192 | } |
||
193 | } |
||
194 | |||
449 | Brean | 195 | bool cConnection::send_Cmd(char CMD, int Address, char Data[MAX_DATA_SIZE],unsigned int Length, bool Resend) |
250 | KeyOz | 196 | { |
449 | Brean | 197 | if (b_isOpen) //is_connected() |
250 | KeyOz | 198 | { |
199 | QByteArray Temp; |
||
449 | Brean | 200 | /* |
250 | KeyOz | 201 | if (CMD != '#') |
202 | { |
||
396 | Brean | 203 | TX_Data = Parser::encode64(Data, Length); |
449 | Brean | 204 | string TX_Data; |
396 | Brean | 205 | char addr = 'a' + Address; |
206 | TX_Data = string("#") + (string(&addr)) + string(&CMD) + TX_Data; |
||
207 | TX_Data = Parser::add_CRC(TX_Data) + '\r'; |
||
250 | KeyOz | 208 | |
396 | Brean | 209 | Temp = QByteArray(TX_Data.c_str()); |
313 | KeyOz | 210 | |
250 | KeyOz | 211 | if (Resend) |
212 | { |
||
313 | KeyOz | 213 | o_Timer->start(2000); |
214 | s_ReSend = Temp; |
||
250 | KeyOz | 215 | } |
449 | Brean | 216 | emit(showTerminal(3, QString(TX_Data.c_str()))); |
250 | KeyOz | 217 | } |
218 | else |
||
219 | { |
||
220 | for (unsigned int a = 0; a < Length; a++) |
||
221 | { |
||
222 | Temp[a] = Data[a]; |
||
223 | } |
||
224 | } |
||
449 | Brean | 225 | */ |
263 | KeyOz | 226 | switch(i_Type) |
227 | { |
||
228 | case C_TTY : |
||
229 | { |
||
230 | TTY->sendData(Temp); |
||
231 | } |
||
232 | break; |
||
233 | case C_IP : |
||
234 | { |
||
235 | TcpSocket->write(Temp); |
||
236 | } |
||
237 | break; |
||
238 | } |
||
250 | KeyOz | 239 | } |
240 | return true; |
||
241 | } |
||
313 | KeyOz | 242 | |
243 | void cConnection::stop_ReSend() |
||
244 | { |
||
451 | Brean | 245 | //o_Timer->stop(); |
313 | KeyOz | 246 | } |
247 | |||
248 | void cConnection::slot_Timer() |
||
249 | { |
||
250 | switch(i_Type) |
||
251 | { |
||
252 | case C_TTY : |
||
253 | { |
||
254 | TTY->sendData(s_ReSend); |
||
255 | } |
||
256 | break; |
||
257 | case C_IP : |
||
258 | { |
||
259 | TcpSocket->write(s_ReSend); |
||
260 | } |
||
261 | break; |
||
262 | } |
||
263 | |||
264 | emit(showTerminal(3, QString(s_ReSend))); |
||
265 | } |