#include "cbarcodedriver_xdl.h"
#include "cbarcode_xdl.h"
#include "cmserialport.h"
CBarCodeDriver_XDL::CBarCodeDriver_XDL(const QString &name, int32_t id)
   :CBarCodeDriver(name,id)
{
}

void CBarCodeDriver_XDL::GetBarCode(int iCHID)
{
    CBarCode_XDL* pBarCode=(CBarCode_XDL*)FindChannel(iCHID);

    QMutexLocker locker(&m_CardMutex);

    byte pCommand[3]={0x1b,0x31,0};
    char pResponse[32]={0};
    m_pSerialPort->send((const char*)pCommand,2);

    LogRecord(std::string("send:") + (const char*)pCommand);
    MSleep(200);
    int32_t irtn=m_pSerialPort->wait();
    m_pSerialPort->receive(pResponse,irtn);
    LogRecord(std::string("receive:") + pResponse);
    int iSize=strlen(pResponse);
    if(!iSize)
    {
        LogRecord("recive error:0");
        MQT_THROW_STR(QObject::trUtf8("BarCode Scan Communication Err"));
    }
    else if(iSize>1&&pResponse[iSize-1]!=0x0D)
    {
        LogRecord("recive error: format err");
        MQT_THROW_STR(QObject::trUtf8("BarCode Scan Data Format Err"));
    }
    pBarCode->m_strBarCode=pResponse;
    pBarCode->m_strBarCode.remove(0x06);
    pBarCode->m_strBarCode.remove(0x0D);
}

int32_t CBarCodeDriver_XDL::init_driver()
{
    m_pSerialPort->open();
    return CBarCodeDriver::init_driver();
}

int32_t CBarCodeDriver_XDL::exit_driver()
{
    m_pSerialPort->close();
    return CBarCodeDriver::exit_driver();
}
