#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
没有合适的资源?快使用搜索试试~ 我知道了~
58小票机打印动态库和代码 通过调用ESCPOS命令,打印文字,条码
共3个文件
cpp:1个
h:1个
dll:1个
5星 · 超过95%的资源 需积分: 50 25 下载量 199 浏览量
2020-03-12
16:01:56
上传
评论
收藏 14KB RAR 举报
温馨提示
58小票机打印动态库和代码 通过调用ESCPOS命令,打印文字,条码,使用的时候执行文件目录放入动态库lg.dll,可以加qq1256622929共同探讨
资源推荐
资源详情
资源评论
收起资源包目录
58小票机打印动态库和代码.rar (3个子文件)
58小票机打印动态库和代码
print.h 2KB
lg.dll 21KB
print.cpp 14KB
共 3 条
- 1
资源评论
- glowlaw2023-07-26代码编写清晰,易于理解,对于不熟悉ESCPOS命令的开发者也很友好。
- 林祈墨2023-07-26这个文件解决了我之前在打印小票时遇到的问题,真是个大大的帮手。
- 雨后的印2023-07-26这个文件提供了打印小票的功能,使用简单,方便实用。
- 基鑫阁2023-07-26小票打印效果清晰,条码精确,可以满足商家的打印需求。
- ask_ai_app2023-07-26不仅可以打印文字和条码,还可以扩展更多功能,是一个多功能的小票打印解决方案。
zck0911
- 粉丝: 2
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功