Subversion Repositories Projects

Rev

Rev 250 | Go to most recent revision | Details | Compare with Previous | 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
 
259 KeyOz 74
    b_isOpen = false;
75
 
250 KeyOz 76
    return b_isOpen;
77
}
78
 
79
bool cConnection::Open(QString Address)
80
{
81
    TTY->setPort(Address); //Port
82
 
83
    TTY->setBaudRate(BAUD57600); //BaudRate
84
    TTY->setDataBits(DATA_8); //DataBits
85
    TTY->setParity(PAR_NONE); //Parity
86
    TTY->setStopBits(STOP_1); //StopBits
87
    TTY->setFlowControl(FLOW_OFF); //FlowControl
88
 
89
    TTY->setTimeout(0, 10);
90
    TTY->enableSending();
91
    TTY->enableReceiving();
92
 
93
    TTY->open();
259 KeyOz 94
    ToolBox::Wait(1000);
95
    qDebug("Try Open");
250 KeyOz 96
    if (TTY->isOpen())
97
    {
259 KeyOz 98
        qDebug("Opend");
250 KeyOz 99
        connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));
100
 
101
        TTY->receiveData();
102
        b_isOpen = true;
103
    }
104
 
105
    return b_isOpen;
106
}
107
 
108
bool cConnection::send_Cmd(char CMD, int Address, char Data[150],unsigned int Length, bool Resend)
109
{
110
    if (b_isOpen)
111
    {
112
        QByteArray Temp;
113
        QString TX_Data;
114
 
115
        if (CMD != '#')
116
        {
117
            TX_Data = ToolBox::Encode64(Data, Length);
118
 
119
            TX_Data = QString("#") + (QString('a' + Address)) + QString(CMD) + TX_Data;
120
            TX_Data = ToolBox::add_CRC(TX_Data) + '\r';
121
 
122
            if (Resend)
123
            {
124
//                LastSend = TX_Data;
125
//                TickerEvent[0] = true;
126
            }
127
            Temp = QByteArray(TX_Data.toUtf8());
128
        }
129
        else
130
        {
131
            for (unsigned int a = 0; a < Length; a++)
132
            {
133
                Temp[a] = Data[a];
134
            }
135
        }
136
 
137
        TTY->sendData(Temp);
138
 
139
        emit(showTerminal(3, TX_Data));
140
    }
141
    return true;
142
}