Subversion Repositories Projects

Rev

Rev 250 | Rev 263 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/***************************************************************************
 *   Copyright (C) 2009 by Manuel Schrape                                  *
 *   manuel.schrape@gmx.de                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License.        *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "cConnection.h"

cConnection::cConnection()
{
    TTY = new ManageSerialPort();

    b_isOpen = false;
}

void cConnection::slot_newDataReceived(const QByteArray &dataReceived)
{
    const char *RXt;
    RXt = dataReceived.data();
    int a = 0;

    while (RXt[a] != '\0')
    {
        if (RXt[a] == '\r')
        {
            while ((RxData.String.length() > 1) && (RxData.String.at(1) == '#'))
            {
                RxData.String.remove(0,1);
            }

            if (ToolBox::check_CRC(RxData.String))
            {
                RxData.Input = RxData.String.toLatin1().data();
                emit(newData(RxData));
//                emit(showTerminal(1, RxData.String));
            }
            else
            {
                emit(showTerminal(2, RxData.String));
            }
            RxData.String = QString("");
        }
        else
        {
            RxData.String = RxData.String + QString(RXt[a]);
        }
        a++;
    }
}

bool cConnection::isOpen()
{
    return b_isOpen;
}

bool cConnection::Close()
{
    TTY->close();

    disconnect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, 0);

    b_isOpen = false;

    return b_isOpen;
}

bool cConnection::Open(QString Address)
{
    TTY->setPort(Address); //Port

    TTY->setBaudRate(BAUD57600); //BaudRate
    TTY->setDataBits(DATA_8); //DataBits
    TTY->setParity(PAR_NONE); //Parity
    TTY->setStopBits(STOP_1); //StopBits
    TTY->setFlowControl(FLOW_OFF); //FlowControl

    TTY->setTimeout(0, 10);
    TTY->enableSending();
    TTY->enableReceiving();

    TTY->open();
    ToolBox::Wait(1000);
    qDebug("Try Open");
    if (TTY->isOpen())
    {
        qDebug("Opend");
        connect(TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_newDataReceived(const QByteArray &)));

        TTY->receiveData();
        b_isOpen = true;
    }

    return b_isOpen;
}

bool cConnection::send_Cmd(char CMD, int Address, char Data[150],unsigned int Length, bool Resend)
{
    if (b_isOpen)
    {
        QByteArray Temp;
        QString TX_Data;

        if (CMD != '#')
        {
            TX_Data = ToolBox::Encode64(Data, Length);

            TX_Data = QString("#") + (QString('a' + Address)) + QString(CMD) + TX_Data;
            TX_Data = ToolBox::add_CRC(TX_Data) + '\r';

            if (Resend)
            {
//                LastSend = TX_Data;
//                TickerEvent[0] = true;
            }
            Temp = QByteArray(TX_Data.toUtf8());
        }
        else
        {
            for (unsigned int a = 0; a < Length; a++)
            {
                Temp[a] = Data[a];
            }
        }

        TTY->sendData(Temp);

        emit(showTerminal(3, TX_Data));
    }
    return true;
}