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