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