#ifdef COMPILE_FOR_WINDOWS
#include "print.h"
#include <iostream>
#include <ctime>
#include <string>
#include <qmessagebox.h>
#include <qlibrary.h>
#include <qthread.h>
#include <qeventloop.h>
#include <QDebug>
using namespace std;
#include "qtcpserver.h"
print::print()
{
hPort=NULL; //句柄
}
bool print::start (int caozuo ,int type, QString com, QString bote,QString lpt , QString usb , QString ip , QString str)
{
_caozuo = caozuo;
_device.printtype = type;
_device.com = com.toStdString();
_device.BawdRate = bote.toInt();
_device.DataBits=8;
_device.StopBits=ONESTOPBIT;
_device.Parity=NOPARITY; //NONE
_device.ReceiveBuffer=256;
_device.lpt = lpt.toStdString();
_device.usb = usb.toStdString();
_device.ip = ip.toStdString();
_print_text = str;
//usb
QLibrary mylib("./lg.dll"); //声明所用到的dll文件
if (mylib.load()) //判断是否正确加载
{
qDebug()<<"DLL load is OK!";
typedef QString (*Fun)(QString ); //定义函数指针,以备调用
Fun open=(Fun)mylib.resolve("find_path"); //援引 add() 函数
if (open) //是否成功连接上 add() 函数
{
qDebug()<<"Link to Function is OK!";
QString usbstr = QString::fromStdString(_device.usb);
usbstr = usbstr.toLower().section("usb",1,1);
_device.usb = open(usbstr).toStdString();
}
else
qDebug()<<"Linke to Function is not OK!!!!";
}
else
qDebug()<<"DLL is not loaded!";
if( InitPort() ) //初始化打印机端口
{
return false;
}
return true;
}
print::~print()
{
WriteData("\x1b\x44\x00",3);
CloseHandle(hPort);//关闭端口
}
bool print::InitPort()
{
const char *sLinkName = _device.com.c_str();
switch ( _device.printtype ) {
case 0://串
sLinkName = _device.com.c_str();
break;
case 1://并
sLinkName = _device.lpt.c_str();
break;
case 2://usb
sLinkName= _device.usb.c_str();
break;
default:
sLinkName = _device.com.c_str();
break;
}
WCHAR wstr[MAX_PATH]={0};
int returnValue = MultiByteToWideChar(CP_ACP, 0, sLinkName, -1, wstr, sizeof(wstr));
qDebug()<<"sLinkName"<<sLinkName << QString::number(returnValue);
if(_device.printtype==1) //并口
{
hPort = CreateFile(wstr, GENERIC_READ | GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
//hPort =CreateFile(wstr, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
}
else
{
hPort =CreateFile(wstr, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);/* CreateFile(wstr, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL );*/
}
if (hPort == INVALID_HANDLE_VALUE)
{
// QString error = QString("打开端口、串口、usb失败,%1,%2").arg(_device.printtype).arg(sLinkName) ;
// QMessageBox msgBox;
// msgBox.setText( error );
// msgBox.exec();
return false;
}
else
{
//设置端口缓冲
SetupComm(hPort, 0, 0);
// 设定通讯端口超时参数
COMMTIMEOUTS tmouts;
tmouts.ReadIntervalTimeout = 100;
tmouts.ReadTotalTimeoutMultiplier = 100;
tmouts.ReadTotalTimeoutConstant = 100;
tmouts.WriteTotalTimeoutConstant = 100;
tmouts.WriteTotalTimeoutMultiplier = 100;
SetCommTimeouts(hPort, &tmouts);
//设定通讯端口通讯参数
DCB dcb;
BOOL bol = TRUE;
//dcb.DCBlength = sizeof(dcb);
bol=GetCommState(hPort, &dcb);
dcb.BaudRate = _device.BawdRate;
dcb.ByteSize = _device.DataBits;
dcb.StopBits = _device.StopBits;
dcb.Parity = _device.Parity;
bol = SetCommState(hPort, &dcb); //配置串口
qDebug() << "bol" << bol;
// 清除通讯端口缓存
bool bol2 = PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
qDebug() << "bol2" << bol2;
// 初始化重叠IO对象
OVERLAPPED m_OverlappedRead;
OVERLAPPED m_OverlappedWrite;
HANDLE m_hStopCommEvent;
HANDLE m_hDataReady;
memset(&m_OverlappedRead, 0, sizeof(OVERLAPPED));
m_OverlappedRead.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
memset(&m_OverlappedWrite, 0, sizeof(OVERLAPPED));
m_OverlappedWrite.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
// 初始化事件对豿
m_hStopCommEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
m_hDataReady = CreateEvent(NULL, FALSE, FALSE, NULL);
if(_device.printtype == 1)//并口
{
//初始化打印ESC @
//DWORD iBytesLength;
char chInitCode[] = "\x0D\x1B\x40";
bk_flag = true;
if( !Lpt_writedata(chInitCode, 3) )
{
qDebug()<<"并口打印机未开";
}
}
else if(_device.printtype == 2)
{
//初始化打印ESC @
DWORD iBytesLength;
char chInitCode[] = "\x0D\x1B\x40";
if( !WriteFile(hPort, chInitCode, (DWORD)3L, &iBytesLength, NULL) )
{
qDebug()<<"usb打印机未开";
}
}
else //串口
{
//初始化打印ESC @
DWORD iBytesLength;
char chInitCode[] = "\x0D\x1B\x40";
WriteFile(hPort, chInitCode, (DWORD)3L, &iBytesLength, NULL);
}
}
return 0;
}
bool print::NewRow()
{
std::string temp = "\x0A";
return WriteData(temp);
}
bool print::NewRow(int iRow)
{
bool Result = false;
for (int i = 0; i < iRow; i++)
{
Result = NewRow();
if (!Result) break;
}
return Result;
}
bool print::CutPaper()
{
char temp[] = { 0x1d, 0x56, 0x01 };
bool r = WriteData(temp, 3);
return r;
}
bool print::OpenCashbox(){
string s = "\x1B\x70\x30\x32\xC8";
return WriteData(s);
}
bool print::stopPrint()
{
string s = "\x1D\x99";
return WriteData(s);
}
bool print::WriteData(string meg)
{
bool ww = true;
int type = _device.printtype;
DWORD dwWrite;
switch(type)
{
case 0://串
ww = WriteFile(hPort, meg.c_str(), (DWORD)meg.length(), &dwWrite, NULL);
break;
case 2://usb
ww = WriteFile(hPort, meg.c_str(), (DWORD)meg.length(), &dwWrite, NULL);
break;
case 1://并口
ww = Lpt_writedata(meg);
//qDebug()<<"enter 11111111111111111";
//ww = WriteFile(hPort, meg.c_str(), (DWORD)meg.length(), &dwWrite, NULL);
break;
default://默认串口
ww = WriteFile(hPort, meg.c_str(), (DWORD)meg.length(), &dwWrite, NULL);
break;
}
return ww;
}
bool print::WriteData(string meg, int len)
{
bool ww = true;
int type = _device.printtype;
DWORD dwWrite;
switch (type) {
case 0://串
ww = WriteFile(hPort, meg.c_str(), (DWORD)len, &dwWrite, NULL);
break;
case 2://usb
ww = WriteFile(hPort, meg.c_str(), (DWORD)len, &dwWrite, NULL);
break;
case 1://并口
ww = Lpt_writedata(meg, len);
//ww = WriteFile(hPort, meg.c_str(), (DWORD)len, &dwWrite, NULL);
break;
default://默认串口
ww = WriteFile(hPort, meg.c_str(), (DWORD)len, &dwWrite, NULL);
break;
}
return