Subversion Repositories Projects

Rev

Rev 259 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 259 Rev 263
Line 18... Line 18...
18
 ***************************************************************************/
18
 ***************************************************************************/
19
#include "cConnection.h"
19
#include "cConnection.h"
Line 20... Line 20...
20
 
20
 
21
cConnection::cConnection()
21
cConnection::cConnection()
-
 
22
{
-
 
23
    i_Type = 0;
22
{
24
 
Line -... Line 25...
-
 
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();
23
    TTY = new ManageSerialPort();
35
    TTY->enableReceiving();
24
 
36
 
Line -... Line 37...
-
 
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
    }
25
    b_isOpen = false;
56
 
26
}
57
}
27
 
58
 
28
void cConnection::slot_newDataReceived(const QByteArray &dataReceived)
59
void cConnection::slot_newDataReceived(const QByteArray &dataReceived)
29
{
60
{
Line 30... Line 61...
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')
-
 
35
    {
-
 
36
        if (RXt[a] == '\r')
65
    while (RXt[a] != '\0')
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
66
    {
50
            {
67
        if (RXt[a] == '\r')
51
                emit(showTerminal(2, RxData.String));
68
        {
52
            }
69
            new_Data(QString(""));
53
            RxData.String = QString("");
70
            RxData.String = QString("");
Line 65... Line 82...
65
    return b_isOpen;
82
    return b_isOpen;
66
}
83
}
Line 67... Line 84...
67
 
84
 
68
bool cConnection::Close()
85
bool cConnection::Close()
-
 
86
{
-
 
87
    switch(i_Type)
-
 
88
    {
-
 
89
        case C_TTY :
69
{
90
        {
Line 70... Line 91...
70
    TTY->close();
91
            TTY->close();
Line 71... Line 92...
71
 
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;
Line 72... Line 106...
72
    disconnect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, 0);
106
        }
73
 
107
        break;
Line 74... Line 108...
74
    b_isOpen = false;
108
    }
75
 
109
 
-
 
110
    return b_isOpen;
-
 
111
}
-
 
112
 
-
 
113
bool cConnection::Open(int Typ, QString Address)
76
    return b_isOpen;
114
{
-
 
115
    switch(Typ)
Line 77... Line -...
77
}
-
 
78
 
-
 
79
bool cConnection::Open(QString Address)
-
 
80
{
116
    {
81
    TTY->setPort(Address); //Port
-
 
Line 82... Line 117...
82
 
117
        case C_TTY :
83
    TTY->setBaudRate(BAUD57600); //BaudRate
118
        {
84
    TTY->setDataBits(DATA_8); //DataBits
119
            TTY->setPort(Address); //Port
Line 85... Line 120...
85
    TTY->setParity(PAR_NONE); //Parity
120
            TTY->open();
-
 
121
 
86
    TTY->setStopBits(STOP_1); //StopBits
122
            ToolBox::Wait(1000);
-
 
123
 
-
 
124
            if (TTY->isOpen())
87
    TTY->setFlowControl(FLOW_OFF); //FlowControl
125
            {
88
 
126
                connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));
89
    TTY->setTimeout(0, 10);
127
 
-
 
128
                TTY->receiveData();
-
 
129
                b_isOpen = true;
90
    TTY->enableSending();
130
                i_Type = C_TTY;
91
    TTY->enableReceiving();
131
            }
Line -... Line 132...
-
 
132
        }
-
 
133
        break;
92
 
134
        case C_IP :
93
    TTY->open();
135
        {
-
 
136
            QStringList Host = Address.split(":");
-
 
137
 
94
    ToolBox::Wait(1000);
138
            TcpSocket = new(QTcpSocket);
Line 95... Line 139...
95
    qDebug("Try Open");
139
            TcpSocket->connectToHost (Host[1], Host[2].toInt());
96
    if (TTY->isOpen())
140
 
Line -... Line 141...
-
 
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')
97
    {
177
        {
98
        qDebug("Opend");
178
            new_Data(QString(""));
99
        connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));
179
            RxData.String = QString("");
100
 
180
        }
101
        TTY->receiveData();
181
        else
Line 132... Line 212...
132
            {
212
            {
133
                Temp[a] = Data[a];
213
                Temp[a] = Data[a];
134
            }
214
            }
135
        }
215
        }
Line -... Line 216...
-
 
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;
Line 137... Line 229...
137
        TTY->sendData(Temp);
229
        }
138
 
230
 
139
        emit(showTerminal(3, TX_Data));
231
        emit(showTerminal(3, TX_Data));
140
    }
232
    }