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