看别人的代码,确实是一个学习的过程。相信通过大量的源码的阅读和理解,以及项目经验的堆积,我会变得更加强大!
我要更加努力!!!才行!
(1).h文件
#ifndef __PRINT_H__
#define __RINT_H__
#include <windows.h>
#include <string>
#include <IOSTREAM>
#include <tchar.h>
#include <vector>
#include <qobject.h>
using namespace std;
//端口信息、并口信息
typedef struct DataInfo
{
int printtype; //打印类型,0串口,1:usb,2:并口
string com; //串口端口号
int BawdRate;//波特率
int DataBits; //数据位
char Parity; //校验位
int ReceiveBuffer; //缓冲区
int StopBits;//停止位
string lpt; //并口
string usb;//usb名称
string ip;
}PrintDevice;
//打印控制类
class print:public QObject
{
Q_OBJECT
public:
print();
~print();
//载入数据com等
bool start (int caozuo ,int type, QString com, QString bote,QString lpt , QString usb , QString ip , QString str);
public:
bool InitPort();//初始化串口
void InitializeDevicePar();
HANDLE hPort; //句柄
public:
//输出打印
bool NewRow();//换行打印
bool NewRow(int iRow);//换N行打印
bool CutPaper();//切纸
bool OpenCashbox();//开钱箱
bool WriteData(string meg); //写入数据
bool WriteData(string meg, int len); //同上
bool Lpt_writedata(string msg);
bool Lpt_writedata(string msg, int len);
private:
PrintDevice _device;
int _caozuo; //0:打印 1:切纸 2:开钱箱
QString _print_text;
bool bk_flag; //并口是否能用, true能,false否
};
#endif
(2).cpp
//一行打印42字符
#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; //打印类型,0串口,1:usb,2:并口
_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(); //usb名称
_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(); //_str()函数返回一个指向正规C字符串的指针
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};
//MultiByteToWideChar是一种windows API 函数,该函数映射一个字符串到一个宽字符(unicode)的字符串。
/* 函数原型:
* int MultiByteToWideChar(
UINT CodePage,
DWORD dwFlags,
LPCSTR lpMultiByteStr,
int cchMultiByte,
LPWSTR lpWideCharStr,
int cchWideChar
);
*/
MultiByteToWideChar(CP_ACP, 0, sLinkName, -1, wstr, sizeof(wstr));
qDebug()<<"sLinkName"<<sLinkName;
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, 1024, 1024);
// 设定通讯端口超时参数
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); //配置串口
// 清除通讯端口缓存
PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
// 初始化重叠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::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 ww;
}
bool print::Lpt_writedata(string msg)
{
bool ret = false;
if( !bk_flag)
{
return ret;
}
OVERLAPPED ov;
memset(&ov,0,sizeof(ov));
ov.hEvent = CreateEvent( 0,true,0,0);
DWORD dwBytesWritten = 0;
try
{
int iRet = WriteFile (hPort, msg.c_str(), (DWORD)msg.length(), &dwBytesWritten ,&ov);
//m_hFile文件句柄,m_buf缓冲区,m_len长度
DWORD dw = WaitForSingleObject(ov.hEvent ,1000);
int a = 10;
if(dw!=0)
{
bk_flag = false;
}
else
{
bk_flag = true;
ret = true;
}
}
catch(char *str)
{
ret = false;
}
CloseHandle(ov.hEvent);
return ret;
}
bool print::Lpt_writedata(string msg, int len)
{
bool ret = false;
if( !bk_flag)
{
return ret;
}
OVERLAPPED ov;
memset(&ov,0,sizeof(ov));
ov.hEvent = CreateEvent( 0,true,0,0);
DWORD dwBytesWritten = 0;
try
{
int iRet = WriteFile (hPort, msg.c_str(), (DWORD)len, &dwBytesWritten ,&ov);
//m_hFile文件句柄,m_buf缓冲区,m_len长度
DWORD dw = WaitForSingleObject(ov.hEvent ,1000);
int a = 10;
if(dw!=0)
{
bk_flag = false;
}
else
{
bk_flag = true;
ret = true;
}
}
catch(char *str)
{
ret = false;
}
CloseHandle(ov.hEvent);
return ret;
}