Rev 391 | Go to most recent revision | 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(); |
313 | KeyOz | 26 | o_Timer = new QTimer(this); |
250 | KeyOz | 27 | |
313 | KeyOz | 28 | connect(o_Timer, SIGNAL(timeout()), SLOT(slot_Timer())); |
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 | |
396 | 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 | } |
61 | |||
62 | } |
||
63 | |||
250 | KeyOz | 64 | void cConnection::slot_newDataReceived(const QByteArray &dataReceived) |
65 | { |
||
66 | const char *RXt; |
||
67 | RXt = dataReceived.data(); |
||
68 | int a = 0; |
||
69 | |||
70 | while (RXt[a] != '\0') |
||
71 | { |
||
72 | if (RXt[a] == '\r') |
||
73 | { |
||
263 | KeyOz | 74 | new_Data(QString("")); |
396 | Brean | 75 | RxData.str = string(""); |
250 | KeyOz | 76 | } |
77 | else |
||
78 | { |
||
396 | Brean | 79 | RxData.str = RxData.str + string(&RXt[a]); |
250 | KeyOz | 80 | } |
81 | a++; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | bool cConnection::isOpen() |
||
86 | { |
||
87 | return b_isOpen; |
||
88 | } |
||
89 | |||
90 | bool cConnection::Close() |
||
91 | { |
||
263 | KeyOz | 92 | switch(i_Type) |
93 | { |
||
94 | case C_TTY : |
||
95 | { |
||
96 | TTY->close(); |
||
250 | KeyOz | 97 | |
263 | KeyOz | 98 | disconnect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, 0); |
250 | KeyOz | 99 | |
263 | KeyOz | 100 | b_isOpen = false; |
101 | } |
||
102 | break; |
||
103 | case C_IP : |
||
104 | { |
||
105 | TcpSocket->disconnectFromHost(); |
||
106 | disconnect(TcpSocket, SIGNAL(connected()), 0, 0); |
||
107 | disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0); |
||
108 | disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0); |
||
259 | KeyOz | 109 | |
263 | KeyOz | 110 | b_isOpen = false; |
111 | } |
||
112 | break; |
||
113 | } |
||
114 | |||
250 | KeyOz | 115 | return b_isOpen; |
116 | } |
||
117 | |||
263 | KeyOz | 118 | bool cConnection::Open(int Typ, QString Address) |
250 | KeyOz | 119 | { |
263 | KeyOz | 120 | switch(Typ) |
121 | { |
||
122 | case C_TTY : |
||
123 | { |
||
124 | TTY->setPort(Address); //Port |
||
125 | TTY->open(); |
||
250 | KeyOz | 126 | |
396 | Brean | 127 | ToolBox::wait(1000); |
250 | KeyOz | 128 | |
263 | KeyOz | 129 | if (TTY->isOpen()) |
130 | { |
||
131 | connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &))); |
||
250 | KeyOz | 132 | |
263 | KeyOz | 133 | TTY->receiveData(); |
134 | b_isOpen = true; |
||
135 | i_Type = C_TTY; |
||
136 | } |
||
137 | } |
||
138 | break; |
||
139 | case C_IP : |
||
140 | { |
||
141 | QStringList Host = Address.split(":"); |
||
250 | KeyOz | 142 | |
263 | KeyOz | 143 | TcpSocket = new(QTcpSocket); |
144 | TcpSocket->connectToHost (Host[1], Host[2].toInt()); |
||
145 | |||
146 | connect(TcpSocket, SIGNAL(connected()), this, SLOT(slot_IP_Connected()) ); |
||
147 | // connect(TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_Error(QAbstractSocket::SocketError))); |
||
148 | b_isOpen = true; |
||
149 | i_Type = C_IP; |
||
150 | } |
||
151 | break; |
||
250 | KeyOz | 152 | } |
153 | |||
154 | return b_isOpen; |
||
155 | } |
||
156 | |||
263 | KeyOz | 157 | void cConnection::slot_IP_Connected() |
158 | { |
||
159 | connect(TcpSocket, SIGNAL(readyRead()), SLOT(slot_IP_ReadLine())); |
||
160 | connect(TcpSocket, SIGNAL(disconnected()),TcpSocket, SLOT(deleteLater())); |
||
161 | connect(TcpSocket, SIGNAL(disconnected()),this, SLOT(slot_IP_Disconnect())); |
||
162 | |||
163 | // emit sig_Connected(); |
||
164 | } |
||
165 | |||
166 | void cConnection::slot_IP_Disconnect() |
||
167 | { |
||
168 | disconnect(TcpSocket, SIGNAL(disconnected()), 0, 0); |
||
169 | disconnect(TcpSocket, SIGNAL(readyRead()), 0, 0); |
||
170 | // emit sig_Disconnected(1); |
||
171 | } |
||
172 | |||
173 | void cConnection::slot_IP_ReadLine() |
||
174 | { |
||
175 | QString Input = QString(TcpSocket->readLine(TcpSocket->bytesAvailable())).remove(QChar(' ')); |
||
176 | |||
177 | int Len = Input.length(); |
||
178 | |||
179 | for (int z = 0; z < Len; z++) |
||
180 | { |
||
181 | if (Input[z] == '\r') |
||
182 | { |
||
183 | new_Data(QString("")); |
||
396 | Brean | 184 | RxData.str = string(""); |
263 | KeyOz | 185 | } |
186 | else |
||
187 | { |
||
396 | Brean | 188 | RxData.str = RxData.str + Input[z].toAscii(); |
263 | KeyOz | 189 | } |
190 | } |
||
191 | } |
||
192 | |||
250 | KeyOz | 193 | bool cConnection::send_Cmd(char CMD, int Address, char Data[150],unsigned int Length, bool Resend) |
194 | { |
||
195 | if (b_isOpen) |
||
196 | { |
||
197 | QByteArray Temp; |
||
396 | Brean | 198 | string TX_Data; |
250 | KeyOz | 199 | |
200 | if (CMD != '#') |
||
201 | { |
||
396 | Brean | 202 | TX_Data = Parser::encode64(Data, Length); |
250 | KeyOz | 203 | |
396 | Brean | 204 | char addr = 'a' + Address; |
205 | TX_Data = string("#") + (string(&addr)) + string(&CMD) + TX_Data; |
||
206 | TX_Data = Parser::add_CRC(TX_Data) + '\r'; |
||
250 | KeyOz | 207 | |
396 | Brean | 208 | Temp = QByteArray(TX_Data.c_str()); |
313 | KeyOz | 209 | |
250 | KeyOz | 210 | if (Resend) |
211 | { |
||
313 | KeyOz | 212 | o_Timer->start(2000); |
213 | s_ReSend = Temp; |
||
250 | KeyOz | 214 | } |
215 | } |
||
216 | else |
||
217 | { |
||
218 | for (unsigned int a = 0; a < Length; a++) |
||
219 | { |
||
220 | Temp[a] = Data[a]; |
||
221 | } |
||
222 | } |
||
223 | |||
263 | KeyOz | 224 | switch(i_Type) |
225 | { |
||
226 | case C_TTY : |
||
227 | { |
||
228 | TTY->sendData(Temp); |
||
229 | } |
||
230 | break; |
||
231 | case C_IP : |
||
232 | { |
||
233 | TcpSocket->write(Temp); |
||
234 | } |
||
235 | break; |
||
236 | } |
||
250 | KeyOz | 237 | |
396 | Brean | 238 | emit(showTerminal(3, QString(TX_Data.c_str()))); |
250 | KeyOz | 239 | } |
240 | return true; |
||
241 | } |
||
313 | KeyOz | 242 | |
243 | void cConnection::stop_ReSend() |
||
244 | { |
||
245 | o_Timer->stop(); |
||
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 | } |