#include "SerialTool/SerialTool.h"
#include <QSerialPort>

SerialTool::SerialTool()
{

}

bool SerialTool::SerialOpen(QSerialPort *mySerialPort, QString PortName, QString Baud, QString DataBits, QString Parity, QString stopBit)
{
    bool ret = true;
    if((PortName=="")||(PortName =="  avail"))
    {
        return false;
    }
//    if(mySerialPort->parent() == NULL)
//    {// the object is not exist;
//       return false;
//    }
    if(mySerialPort->isOpen())
    {
        return false;
    }
    mySerialPort->setPortName(PortName);
    if(mySerialPort->open(QIODevice::ReadWrite))
    {
        //设置波特率
        if (Baud == "115200")
            mySerialPort->setBaudRate(QSerialPort::Baud115200);
        else if (Baud == "9600")
            mySerialPort->setBaudRate(QSerialPort::Baud9600);
        else if (Baud == "1200")
            mySerialPort->setBaudRate(QSerialPort::Baud1200);
        else if (Baud == "2400")
            mySerialPort->setBaudRate(QSerialPort::Baud2400);
        else if (Baud == "4800")
            mySerialPort->setBaudRate(QSerialPort::Baud4800);
        else if (Baud == "19200")
            mySerialPort->setBaudRate(QSerialPort::Baud19200);
        else if (Baud == "38400")
            mySerialPort->setBaudRate(QSerialPort::Baud38400);
        else if (Baud == "57600")
            mySerialPort->setBaudRate(QSerialPort::Baud57600);
        else if (Baud == "600")
            mySerialPort->setBaudRate(600);
        else {
            ret = false;
        }
        //检验位

        if (Parity == QString("无校验"))
        {
            mySerialPort->setParity(QSerialPort::NoParity);
        }
        else if (Parity == QString("奇校验"))
            mySerialPort->setParity(QSerialPort::OddParity);
        else if (Parity == QString("偶校验"))
            mySerialPort->setParity(QSerialPort::EvenParity);
        else if (Parity == QString("空格"))
            mySerialPort->setParity(QSerialPort::SpaceParity);
        else if (Parity == QString("标志"))
            mySerialPort->setParity(QSerialPort::MarkParity);
        else {
            ret = false;
        }
        //数据位长度
        if (DataBits == "8")
            mySerialPort->setDataBits(QSerialPort::Data8);
        else if (DataBits == "7")
            mySerialPort->setDataBits(QSerialPort::Data7);
        else if (DataBits == "6")
            mySerialPort->setDataBits(QSerialPort::Data6);
        else if (DataBits == "5")
            mySerialPort->setDataBits(QSerialPort::Data5);
        else {
            ret = false;
        }
        //停止位数
        if (stopBit == "1")
            mySerialPort->setStopBits(QSerialPort::OneStop);
        else if (stopBit == "1.5")
            mySerialPort->setStopBits(QSerialPort::OneAndHalfStop);
        else if (stopBit == "2")
            mySerialPort->setStopBits(QSerialPort::TwoStop);
        //硬件流控制
         mySerialPort->setFlowControl(QSerialPort::NoFlowControl);
        /*-
        if (stopBit == "off")
            mySerialPort->setFlowControl(QSerialPort::NoFlowControl);
        else if (stopBit == "hardware")
            mySerialPort->setFlowControl(QSerialPort::HardwareControl);
        else if (stopBit == "xonxoff")
            mySerialPort->setFlowControl(QSerialPort::SoftwareControl);
        else {
            ret = false;
        }
        */
        //接收处理 需要外部申请
        // connect(mySerialPort, SIGNAL(readyRead()), this, SLOT(readMyCom()));//接收数据
    }
    else
    {
        ret = false;
    }
    return ret;
}

bool SerialTool::sendBytes(QSerialPort *mySerialPort, INT8U *sendDates, INT32S lenght)
{
    bool ret = true;

    if(mySerialPort->isOpen())
    {//
      //清接收错误标识
      //清接收字节计数器
      if(lenght != 0)
      {
          QByteArray temp;

          ToolFunc::charArrToByteArrayCopy(temp,sendDates,lenght);
              //清除接收错误标识
              mySerialPort->clearError();
              mySerialPort->write(temp);
              mySerialPort->waitForBytesWritten(50);//50ms内发送完数据
      }
      else {

      }
    }
    else {
        ret = false;
    }
    return ret;
}

bool SerialTool::sendBytes_9data(QSerialPort *SerialPort, INT8U *buff, INT8U *buff_9bit, INT32S length)
{
    bool ret = true;
    INT8U  parity = 0xff;//当前第9位状态
    SerialPort->clearError();
    if(SerialPort->isOpen() == false)
    {
        return false;
    }
    QByteArray temp;
    for(INT32S i =0;i<length;i++)
    {
        if(buff_9bit[i] == 1)
        {
            if(parity!=1)
            {
                parity = 1;
                SerialPort->setParity(QSerialPort::MarkParity);
            }
        }
        else {
            if(parity!=0)
            {
                parity = 0;
                SerialPort->setParity(QSerialPort::SpaceParity);
            }
        }
        //发送数据
        temp.clear();
        temp[0] = buff[i];
        SerialPort->write(temp);
        SerialPort->waitForBytesWritten(50);
    }
    return ret;
}

