Go to most recent revision | 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 | { |
||
23 | TTY = new ManageSerialPort(); |
||
24 | |||
25 | b_isOpen = false; |
||
26 | } |
||
27 | |||
28 | void cConnection::slot_newDataReceived(const QByteArray &dataReceived) |
||
29 | { |
||
30 | const char *RXt; |
||
31 | RXt = dataReceived.data(); |
||
32 | int a = 0; |
||
33 | |||
34 | while (RXt[a] != '\0') |
||
35 | { |
||
36 | if (RXt[a] == '\r') |
||
37 | { |
||
38 | while ((RxData.String.length() > 1) && (RxData.String.at(1) == '#')) |
||
39 | { |
||
40 | RxData.String.remove(0,1); |
||
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(""); |
||
54 | } |
||
55 | else |
||
56 | { |
||
57 | RxData.String = RxData.String + QString(RXt[a]); |
||
58 | } |
||
59 | a++; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | bool cConnection::isOpen() |
||
64 | { |
||
65 | return b_isOpen; |
||
66 | } |
||
67 | |||
68 | bool cConnection::Close() |
||
69 | { |
||
70 | TTY->close(); |
||
71 | |||
72 | disconnect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, 0); |
||
73 | |||
74 | return b_isOpen; |
||
75 | } |
||
76 | |||
77 | bool cConnection::Open(QString Address) |
||
78 | { |
||
79 | TTY->setPort(Address); //Port |
||
80 | |||
81 | TTY->setBaudRate(BAUD57600); //BaudRate |
||
82 | TTY->setDataBits(DATA_8); //DataBits |
||
83 | TTY->setParity(PAR_NONE); //Parity |
||
84 | TTY->setStopBits(STOP_1); //StopBits |
||
85 | TTY->setFlowControl(FLOW_OFF); //FlowControl |
||
86 | |||
87 | TTY->setTimeout(0, 10); |
||
88 | TTY->enableSending(); |
||
89 | TTY->enableReceiving(); |
||
90 | |||
91 | TTY->open(); |
||
92 | |||
93 | if (TTY->isOpen()) |
||
94 | { |
||
95 | connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &))); |
||
96 | |||
97 | TTY->receiveData(); |
||
98 | b_isOpen = true; |
||
99 | } |
||
100 | |||
101 | return b_isOpen; |
||
102 | } |
||
103 | |||
104 | bool cConnection::send_Cmd(char CMD, int Address, char Data[150],unsigned int Length, bool Resend) |
||
105 | { |
||
106 | if (b_isOpen) |
||
107 | { |
||
108 | QByteArray Temp; |
||
109 | QString TX_Data; |
||
110 | |||
111 | if (CMD != '#') |
||
112 | { |
||
113 | TX_Data = ToolBox::Encode64(Data, Length); |
||
114 | |||
115 | TX_Data = QString("#") + (QString('a' + Address)) + QString(CMD) + TX_Data; |
||
116 | TX_Data = ToolBox::add_CRC(TX_Data) + '\r'; |
||
117 | |||
118 | if (Resend) |
||
119 | { |
||
120 | // LastSend = TX_Data; |
||
121 | // TickerEvent[0] = true; |
||
122 | } |
||
123 | Temp = QByteArray(TX_Data.toUtf8()); |
||
124 | } |
||
125 | else |
||
126 | { |
||
127 | for (unsigned int a = 0; a < Length; a++) |
||
128 | { |
||
129 | Temp[a] = Data[a]; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | TTY->sendData(Temp); |
||
134 | |||
135 | emit(showTerminal(3, TX_Data)); |
||
136 | } |
||
137 | return true; |
||
138 | } |