#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
zck0911
- 粉丝: 2
- 资源: 13
最新资源
- 几何物体检测43-YOLO(v5至v9)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 基于cruise的燃料电池功率跟随仿真,按照丰田氢能源车型搭建,在wltc工况下跟随效果好,最高车速175,最大爬坡30,百公里9s均已实现 1.模型通过cruise simulink联合仿真,策略
- C#源码 上位机 联合Visionpro 通用框架开发源码,已应用于多个项目,整套设备程序,可以根据需求编出来,具体Vpp功能自己编 程序包含功能 1.自动设置界面窗体个数及分布 2.照方式以命令触
- 程序名称:悬架设计计算程序 开发平台:基于matlab平台 计算内容:悬架偏频刚度挠度;螺旋弹簧,多片簧,少片簧,稳定杆,减震器的匹配计算;悬架垂向纵向侧向力学、纵倾、侧倾校核等;独立悬架杠杆比,等效
- 华为OD+真题及解析+智能驾驶
- jQuery信息提示插件
- 基于stm32的通信系统,sim800c与服务器通信,无线通信监测,远程定位,服务器通信系统,gps,sim800c,心率,温度,stm32 由STM32F103ZET6单片机核心板电路、DS18B2
- 充电器检测9-YOLO(v5至v11)、COCO、Create充电器检测9L、Paligemma、TFRecord、VOC数据集合集.rar
- 华为OD+考试真题+实现过程
- 保险箱检测51-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈