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