#include "pch.h"
#include "ZZ_SeiralPort.h"

CZZ_SeiralPort_QT::CZZ_SeiralPort_QT()
{
	m_pSerialPort = new QSerialPort;
}

CZZ_SeiralPort_QT::~CZZ_SeiralPort_QT()
{
	if (m_pSerialPort != NULL)
	{
		delete m_pSerialPort;
	}
}

 void CZZ_SeiralPort_QT::InitPort(PortInfo portinfo)
{
	
	m_piSettings = portinfo;
	

}

 bool CZZ_SeiralPort_QT::SetPort()
 {
	 bool bRes = true;
// 	 int iRes = sio_ioctl(m_piSettings.iPortNumber, m_piSettings.indexBaudRate,
// 		 m_piSettings.indexParity | m_piSettings.indexBytesize | m_piSettings.indexStopBits);
// 	 if (iRes != SIO_OK)
// 	 {
// 		 bRes = false;
// 	 }
	 QString qstrPortName;
	 if (m_piSettings.qstrFullPortName != "")
	 {
		 qstrPortName = m_piSettings.qstrFullPortName;
		 qDebug() << m_piSettings.qstrFullPortName;
	 }
	 else 
	 {
		 qstrPortName = QString("COM%1").arg(m_piSettings.iPortNumber);
	 }
	 //QString qstrPortName = QString("COM%1").arg(m_piSettings.iPortNumber);
	 m_pSerialPort->setPortName(qstrPortName);
	 m_pSerialPort->setReadBufferSize(512);
	 bRes = m_pSerialPort->setBaudRate(m_piSettings.iBaudRate);
	 return bRes;
 }

bool CZZ_SeiralPort_QT::Open()
{
	bool bRes = true;
// 	int iRes = sio_open(m_piSettings.iPortNumber);
// 	if (iRes!=SIO_OK)
// 	{
// 		bRes = false;
// 	}
	bRes = m_pSerialPort->open(QIODevice::ReadWrite);
	if (!bRes)
	{
		qDebug() << "Err:open Failed.Exit Code:1";
		//std::cout << "Err.open Failed" << std::endl;
		return bRes;;
	}
	return bRes;
}

void CZZ_SeiralPort_QT::Close()
{
	m_pSerialPort->close();
}

std::string CZZ_SeiralPort_QT::ReadAll()
{
	m_pSerialPort->waitForReadyRead();
	qbaRecv.clear();
	qbaRecv = m_pSerialPort->readAll();
	std::string strRet(qbaRecv.constData(), qbaRecv.length());
	return strRet;

// 	std::string strRet;
// 	char cBuf[MAXBUFFER];
// 	int iRet = sio_read(m_piSettings.iPortNumber,cBuf, MAXBUFFER);
// 	if (iRet==0)
// 	{
// 		strRet = "";
// 		
// 	}
// 	else
// 	{
// 		char *pRecv = new char[iRet];
// 		memcpy(pRecv, cBuf, iRet);
// 		//std::string strRet(pRecv);
// 		//delete pRecv;
// 		//return strRet;
// 
// 		strRet.resize(iRet);
// 		for (int i=0;i<iRet;i++)
// 		{
// 			strRet[i] = pRecv[i];
// 		}
// 		delete pRecv;
// 	}
// 	return strRet;
}

std::string CZZ_SeiralPort_QT::ReadAll(int iWait)
{
	m_pSerialPort->waitForReadyRead(iWait);
	qbaRecv.clear();
	qbaRecv = m_pSerialPort->readAll();
	std::string strRet(qbaRecv.constData(), qbaRecv.length());
	return strRet;
}

int CZZ_SeiralPort_QT::Write(const std::string strSend)
{
	QByteArray qbaTest(strSend.c_str(),(int)strSend.length());
	qint64 qi64Write = m_pSerialPort->write(qbaTest);
	m_pSerialPort->waitForBytesWritten(5000);
	if (qi64Write != qbaTest.size())
	{
		qDebug() << "Err:write Failed.Exit Code:1" << qi64Write;
		return qi64Write;
	}

	return 0;
// 	char* pcSend = (char*)strSend.c_str();
// 	int iRet = sio_write(m_piSettings.iPortNumber, pcSend, (int)strSend.length());
// 
// 	//delete pcSend;
// 	if (iRet!= (int)strSend.length())
// 	{
// 		return iRet;
// 	}
// 	else
// 	{
// 		return 0;
// 	}
// 	//sio_write();
}
