Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
279 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
    i_Type = 0;
24
 
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();
36
 
37
    b_isOpen = false;
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
}
58
 
59
void cConnection::slot_newDataReceived(const QByteArray &dataReceived)
60
{
61
    const char *RXt;
62
    RXt = dataReceived.data();
63
    int a = 0;
64
 
65
    while (RXt[a] != '\0')
66
    {
67
        if (RXt[a] == '\r')
68
        {
69
            new_Data(QString(""));
70
            RxData.String = QString("");
71
        }
72
        else
73
        {
74
            RxData.String = RxData.String + QString(RXt[a]);
75
        }
76
        a++;
77
    }
78
}
79
 
80
bool cConnection::isOpen()
81
{
82
    return b_isOpen;
83
}
84
 
85
bool cConnection::Close()
86
{
87
    switch(i_Type)
88
    {
89
        case C_TTY :
90
        {
91
            TTY->close();
92
 
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
        }
107
        break;
108
    }
109
 
110
    return b_isOpen;
111
}
112
 
113
bool cConnection::Open(int Typ, QString Address)
114
{
115
    switch(Typ)
116
    {
117
        case C_TTY :
118
        {
119
            TTY->setPort(Address); //Port
120
            TTY->open();
121
 
122
            ToolBox::Wait(1000);
123
 
124
            if (TTY->isOpen())
125
            {
126
                connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));
127
 
128
                TTY->receiveData();
129
                b_isOpen = true;
130
                i_Type = C_TTY;
131
            }
132
        }
133
        break;
134
        case C_IP :
135
        {
136
            QStringList Host = Address.split(":");
137
 
138
            TcpSocket = new(QTcpSocket);
139
            TcpSocket->connectToHost (Host[1], Host[2].toInt());
140
 
141
            connect(TcpSocket, SIGNAL(connected()), this, SLOT(slot_IP_Connected()) );
142
//            connect(TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slot_Error(QAbstractSocket::SocketError)));
143
            b_isOpen = true;
144
            i_Type = C_IP;
145
        }
146
        break;
147
    }
148
 
149
    return b_isOpen;
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
}
187
 
188
bool cConnection::send_Cmd(char CMD, int Address, char Data[150],unsigned int Length, bool Resend)
189
{
190
    if (b_isOpen)
191
    {
192
        QByteArray Temp;
193
        QString TX_Data;
194
 
195
        if (CMD != '#')
196
        {
197
            TX_Data = ToolBox::Encode64(Data, Length);
198
 
199
            TX_Data = QString("#") + (QString('a' + Address)) + QString(CMD) + TX_Data;
200
            TX_Data = ToolBox::add_CRC(TX_Data) + '\r';
201
 
202
            if (Resend)
203
            {
204
//                LastSend = TX_Data;
205
//                TickerEvent[0] = true;
206
            }
207
            Temp = QByteArray(TX_Data.toUtf8());
208
        }
209
        else
210
        {
211
            for (unsigned int a = 0; a < Length; a++)
212
            {
213
                Temp[a] = Data[a];
214
            }
215
        }
216
 
217
        switch(i_Type)
218
        {
219
            case C_TTY :
220
            {
221
                TTY->sendData(Temp);
222
            }
223
            break;
224
            case C_IP :
225
            {
226
                TcpSocket->write(Temp);
227
            }
228
            break;
229
        }
230
 
231
        emit(showTerminal(3, TX_Data));
232
    }
233
    return true;
234
}