#include "taserialthread.h"
#include <QDebug>
TA_SerialThread::TA_SerialThread()
{
    portCnt = 0;
    portNewCnt=0;
    m_arrSerial = new QSerialPort[10];
    m_arrNewSerial=new QSerialPortInfo[10];
}

TA_SerialThread::~TA_SerialThread()
{

}

void TA_SerialThread::run()
{

}

void TA_SerialThread::SearchPort(void)
{
    portNewCnt=0;
    //查找可用的串口
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        if(portNewCnt > 10)
            break;

        m_arrNewSerial[portNewCnt]=info;
        portNewCnt++;
    }

    for (int i=0;i<portNewCnt;i++) {
        bool iHaveFlag=false;
        for(int j=0;j<portCnt;j++)
        {
            if(m_arrSerial[j].portName()==m_arrNewSerial[i].portName())
            {
                iHaveFlag=true;
                break;
            }
        }
        if(!iHaveFlag)
        {
            m_arrSerial[portCnt].setPort(m_arrNewSerial[i]);
            portCnt++;
        }

    }
    //查找可用的串口
//    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
//    {
//        if(portCnt > 10)
//            break;

//        m_arrSerial[portCnt].setPort(info);
//        if(m_arrSerial[portCnt].open(QIODevice::ReadWrite))
//            m_arrSerial[portCnt++].close();
//    }



}

void TA_SerialThread::InitPortName(uint8_t portIndex,const QString &portName)
{
    if(portIndex < portCnt)
        m_arrSerial[portIndex].setPortName(portName);//const QString &name
    else
       qDebug()<<"OpenPort port index is out of range!";
}

void TA_SerialThread::InitPortBaudRate(uint8_t portIndex,qint32 baudRate)
{
    if(portIndex < portCnt)
        m_arrSerial[portIndex].setBaudRate(baudRate);//BaudRate baudRate
    else
       qDebug()<<"OpenPort port index is out of range!";
}

void TA_SerialThread::InitPortDataBits(uint8_t portIndex,QSerialPort::DataBits dataBits)
{
    if(portIndex < portCnt)
        m_arrSerial[portIndex].setDataBits(dataBits);//DataBits dataBits
    else
       qDebug()<<"OpenPort port index is out of range!";
}

void TA_SerialThread::InitPortParity(uint8_t portIndex,QSerialPort::Parity parity)
{
    if(portIndex < portCnt)
        m_arrSerial[portIndex].setParity(parity);//Parity parity
    else
       qDebug()<<"OpenPort port index is out of range!";
}

void TA_SerialThread::InitPortStopBits(uint8_t portIndex,QSerialPort::StopBits stopBits)
{
    if(portIndex < portCnt)
    {
        m_arrSerial[portIndex].setStopBits(stopBits);//StopBits stopBits
        m_arrSerial[portIndex].setFlowControl(QSerialPort::NoFlowControl);
    }
    else
       qDebug()<<"OpenPort port index is out of range!";
}

bool TA_SerialThread::OpenPort(uint8_t portIndex)
{
    bool result=false;
    if(portIndex < portCnt)
        result=m_arrSerial[portIndex].open(QIODevice::ReadWrite);
    else
        qDebug()<<"OpenPort port index is out of range!";
    return result;
}

void TA_SerialThread::ClosePort(uint8_t portIndex)
{
    if(portIndex < portCnt)
    {
        //关闭串口
        m_arrSerial[portIndex].clear();
        m_arrSerial[portIndex].close();
        //m_arrSerial[portIndex].deleteLater();
    }
    else
        qDebug()<<"OpenPort port index is out of range!";
}

void TA_SerialThread::SendDataBuf(uint8_t portIndex,const QByteArray &str)
{
    if(portIndex < portCnt)
    {
        m_arrSerial[portIndex].write(str);
        qDebug()<<str;
    }
    else
        qDebug()<<"SendDataBuf port index is out of range!";
}

QByteArray TA_SerialThread::GetDataBuf(uint8_t portIndex)
{
    if(portIndex < portCnt)
        return m_arrSerial[portIndex].readAll();
    else
        qDebug()<<"GetDataBuf port index is out of range!";
}
