Subversion Repositories Projects

Rev

Blame | 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 "Input_TTY.h"

void Input_TTY::Init()
{
    Timer = new QTimer();
    connect(Timer, SIGNAL(timeout()), this, SLOT(slot_Timer()));

    for (int z = 0; z < MAX_Confirm; z++)
    {
        Confirm[z].ID = false;
    }
}

bool Input_TTY::Open(set_Input s_Input)
{
    o_TTY = new ManageSerialPort;

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

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

    o_TTY->setPort(s_Input.Main); //Port
    o_TTY->open();

    if (o_TTY->isOpen())
    {
        connect(o_TTY, SIGNAL(newDataReceived(const QByteArray &)), this, SLOT(slot_Receive(const QByteArray &)));

        o_TTY->receiveData();
//        qDebug(QString("Open Input-TTY " + s_Input.Main).toLatin1().data());

        return true;
    }
    return false;
}

bool Input_TTY::IsOpen()
{
    return o_TTY->isOpen();
}

bool Input_TTY::Close()
{
    o_TTY->close();

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

    Timer->stop();

    return o_TTY->isOpen();
}

void Input_TTY::send_Data(QString t_Data, int ID)
{
    if ((ID > 0) && (ID < MAX_Confirm))
    {
        Timer->start(2500);
        Confirm[ID].ID = true;
        Confirm[ID].Data = t_Data;
    }

    if (o_TTY->isOpen())
    {
        if (t_Data[t_Data.length() - 1] != '\r')
        {
            t_Data = t_Data + "\r";
        }

        QByteArray Temp;

        Temp = QByteArray(t_Data.toAscii());

        o_TTY->sendData(Temp);

//        qDebug(t_Data.toLatin1().data());
    }
}

void Input_TTY::send_RawData(char *t_Data)
{
    t_Data = t_Data;
}

void Input_TTY::slot_Receive(const QByteArray &dataReceived)
{
    const char *RX;
    RX = dataReceived.data();
    int a = 0;

    while (RX[a] != '\0')
    {
        if (RX[a] == '\r')
        {
            emit(sig_NewData(s_RX));
            s_RX = QString("");
        }
        else
        {
            s_RX = s_RX + QString(RX[a]);
        }
        a++;
    }
}

void Input_TTY::stop_Resend(int ID)
{
    if ((ID > 0) && (ID < MAX_Confirm))
    {
        Confirm[ID].ID = false;
        Confirm[ID].Data = "";
    }
}

void Input_TTY::slot_Timer()
{
    bool Active = false;

    for (int x = 0; x < MAX_Confirm; x++)
    {
        if (Confirm[x].ID)
        {
            Active = true;
            send_Data(Confirm[x].Data);
        }
    }

    if (Active == false)
    {
        Timer->stop();
    }
}

eMode Input_TTY::Mode()
{
    return TTY;
}