// ReceiptBase.cpp : Defines the entry point for the DLL application. // #include "ReceiptBase.h" //m_iUsedLeng修改成用纸长度,原为行数。 ReceiptBase::ReceiptBase() { m_iLetSpace = 0; m_iUsedLeng = 0; m_iMinPaper = 400; //400*0.125=50mm m_iLineHeight = 30; m_iNVHeight = 0; m_iLineLeng = 48; m_iLastPos = 0; m_iLinesPrted = 0; m_iMMPerSec = 80; } ReceiptBase::~ReceiptBase() { m_cSerial.Close(); } void ReceiptBase::SetLinePara(int iLineLeng, int iLeftSpace, int iSpeed) { m_iLetSpace = iLeftSpace; m_iLineLeng = iLineLeng; m_iMMPerSec = iSpeed; } //凭条打印机 判断端口打开 bool ReceiptBase::Isopen() { return m_cSerial.IsOpen(); } int ReceiptBase::Open(const int iVID, const int iPID, const int iFD) { if(m_cSerial.IsOpen()) return RECEIPT_SUCCESS; //sew此处需要修改 if (m_cSerial.Open("{28d78fad-5a12-11D1-ae5b-0000f803a8c2}", iVID, iPID) == false) return -1; Init(); return RECEIPT_SUCCESS; } // 关闭端口 int ReceiptBase::Close() { if(m_cSerial.IsOpen() == false) return RECEIPT_SUCCESS; m_cSerial.Close(); return RECEIPT_SUCCESS; } //通用发送数据接口 int ReceiptBase::SendData(const unsigned char *sSend,int iDatLen,int iTimeout) { int iCount = 0; int ia; for (ia=0; ia 50 && iCount > iDatLen / 10) iCount = 50; static INT64 iPrintTime = 0; if (iCount) { m_iLinesPrted += iCount; if (m_iLinesPrted > 20) //50行处理一次 { int iTimeWant = m_iLinesPrted * m_iLineHeight * 1000 / (8 * m_iMMPerSec); INT64 iCurTime = ::GetSystemTime(); int iUse = (int)(iCurTime - iPrintTime); if (iUse < 0 || iUse > iTimeWant) iUse = iTimeWant; int iSleep = iTimeWant - iUse; if (iSleep > 0) Sleep(iSleep); iPrintTime = iCurTime; m_iLinesPrted = 0; } } return m_cSerial.Send(sSend, iDatLen, iTimeout); } //通用接收数据接口 int ReceiptBase::ReadData(unsigned char *sRead,int iWant,int iTimeout) { return m_cSerial.Receive((unsigned char *)sRead, iWant, iTimeout); } //打印机初始化 int ReceiptBase::Init() { if(m_cSerial.IsOpen() == false) return RECEIPT_ERR_NO_OPEN; unsigned char szSendBuf[20]; szSendBuf[0] = 0x1b; szSendBuf[1] = 0x40; int nRet = SendData(szSendBuf, 2, 3000); if (nRet < 2) return RECEIPT_ERR_RCV_ERR; SetRowDistance(0); SetLeftDistance(0); SetFont(false, false, false, false, false); SetAlign(0); SetScale(1,1); SetInverse(false); SetRotate(false); SetBlackFlag(false); return RECEIPT_SUCCESS; } //通用发送数据接口 int ReceiptBase::SendHexStr(const char *sData, int iDataLen) { if (iDataLen == 0) iDataLen = strlen(sData); if (iDataLen > 1000) return RECEIPT_ERR_IN_ERR; unsigned char sSend[512]; int iCount = ::HexStrToANSI(sData, sSend, iDataLen); if (iCount <= 0) return RECEIPT_ERR_IN_ERR; int iRet = SendData(sSend, iCount); return iRet; } //数据输出,打印 int ReceiptBase::PrintData(const char * sData) { unsigned char sSend[128]; if(sData == NULL || strlen((char*)sData) < 1) { return RECEIPT_ERR_IN_ERR; } int iLeng = strlen((char*)sData); int iPos = 0, iLinePos = 0; int iChCount = 0; unsigned char chTemp; bool bLineMax, bLineEnd, bDataEnd; int iLengMax = m_iLineLeng; for (iPos = 0 ; iPos < iLeng; ) { bLineMax = FALSE; bLineEnd = FALSE; bDataEnd = FALSE; iChCount = 0; //半个汉字计算 memset(sSend, ' ', m_iLetSpace); if (m_iLastPos) iLinePos = m_iLastPos; else iLinePos = m_iLetSpace; while (iLinePos < iLengMax) { chTemp = (unsigned char)sData[iPos++]; if (chTemp == '\r' || chTemp == '\n') { //一行结束 bLineEnd = TRUE; break; } //数据不包含换行 sSend[iLinePos++] = chTemp; //计算半个汉字 if (chTemp >= 128) iChCount++; else iChCount = 0; if (iPos >= iLeng) { //总数据结束 bDataEnd = TRUE; break; } } if (iLinePos >= iLengMax) bLineMax = TRUE; //处理半个汉字,最后是半个汉字,前移一字节 if ((iChCount % 2) == 1 && (bLineMax || bDataEnd)) { iLinePos--; iPos--; } if (bLineMax || bLineEnd) { sSend[iLinePos] = '\n'; iLinePos++; m_iUsedLeng += m_iLineHeight; } sSend[iLinePos] = 0; int iStart = 0; if (m_iLastPos) { iStart = m_iLastPos; m_iLastPos = 0; } if (SendData(sSend + iStart, iLinePos - iStart, 1000) < iLinePos - iStart) return RECEIPT_ERR_RCV_ERR; //数据的最后一个字符是汉字的半个字符 if ((iChCount % 2) == 1 && iPos == (iLeng - 1)) { m_iLastPos = iLinePos; return iPos; } //windows格式\r\n换行处理 if (chTemp == '\r' && sData[iPos] == '\n') iPos++; } if (bLineMax || bLineEnd) m_iLastPos = 0; else m_iLastPos = iLinePos; return iPos; } //走纸n行 int ReceiptBase::FeedLines(int iLines) { for (int ia = 0; ia < iLines; ia++) { int iRet = SendData((unsigned char*)"\r\n", 2, 500); Sleep(40); m_iUsedLeng += m_iLineHeight; m_iLastPos = 0; if (iRet < 1) return RECEIPT_ERR_RCV_ERR; } return RECEIPT_SUCCESS; } //走纸n行 int ReceiptBase::PrintChar(char ch, int iCount) { for (int ia = 0; ia < iCount; ia++) { int iRet = SendData((unsigned char*)&ch, 1, 500); if (ch == '\n'){ SendData((unsigned char*)"\r", 1, 500); Sleep(40); m_iUsedLeng += m_iLineHeight; m_iLastPos = 0; } if (iRet < 1) return RECEIPT_ERR_RCV_ERR; } return RECEIPT_SUCCESS; } //走纸n行 int ReceiptBase::PrintLine(const char* sLine) { int iRet = PrintData(sLine); if (iRet < 0) return iRet; return FeedLines(1); } //横向移动 int ReceiptBase::MoveX(int iX) { if(iX < 0 || iX > 255) { return RECEIPT_ERR_IN_ERR; } unsigned char sCmd[4]={0x1b, 0x5c, (unsigned char)iX, 0}; int nRet = SendData(sCmd,4); if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } //走纸1点阵 int ReceiptBase::FeedPaper(short iMode, short iDistance) { unsigned char cpSend[10]={0}; int nRet = 0; if ( iMode == 0 ) //往前走 { m_iUsedLeng += iDistance; cpSend[0] = 0x1B; cpSend[1] = 0x4A; while (iDistance > 0 ) //line 大于 0 才执行后走纸命令 { if (iDistance > 80) { cpSend[2] = 80; iDistance -= 80; } else { cpSend[2] = (char)iDistance; iDistance = 0; } nRet = SendData(cpSend,3,500); } if (nRet < 0) return nRet; SendData((unsigned char*)"\r", 1, 500); return RECEIPT_SUCCESS; } return RECEIPT_ERR_RCV_ERR; } //0:全切 1:半切 int ReceiptBase::Cut(int iMode) { SetFont(0, 0, 0, 0, 0); SendData((unsigned char*)"\r", 1, 500); //为保证最后一行数据输出 if (m_iUsedLeng < m_iMinPaper -160 ) FeedPaper(0, (m_iMinPaper - m_iUsedLeng - 160) >> 3); //机械结构,切刀位置在2cm位置,必须走纸2cm后切纸,否则最后数据被切掉 FeedPaper(0, 160); unsigned char cpSend[20]; int nRet = 0; cpSend[0]=0x1B; cpSend[1]=0x4E; cpSend[2]=0x03; cpSend[3]=iMode; SendData(cpSend,4,300); cpSend[0]=0x1D; cpSend[1]=0x56; cpSend[2]=0x42; cpSend[3]=0x00; Sleep(300); nRet = SendData(cpSend,4,300); m_iUsedLeng=0; m_cSerial.Flush(); Sleep(300); if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } //nAlign 0 - 左对齐 ,1 - 居中, 2 - 右对齐 int ReceiptBase::SetAlign(short nAlign) { if(nAlign > 2 || nAlign <0) { return RECEIPT_ERR_IN_ERR; } unsigned char szSendBuf[20]; int nRet = 0; memset(szSendBuf,'\0',20); szSendBuf[0] = 0x1b; szSendBuf[1] = 0x61; szSendBuf[2] = (char)nAlign;//0 - 左对齐 ,1 - 居中, 2 - 右对齐 nRet = SendData(szSendBuf,3); if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } //DH 倍高 DW倍宽 DD黑体 UL下划线 int ReceiptBase::SetFont(bool bDH, bool bDW, bool bDD, bool bUL, bool bCharB) { unsigned char sCmd[4], chChi = 0, chAsc = 0; //英文模式 sCmd[0]=0x1B; sCmd[1]=0x21; if (bDH) { chAsc += 16; chChi += 8; m_iLineHeight = 54; } else m_iLineHeight = 30; if (bUL) { chAsc += 0x80; chChi += 0x80; } if (bDW) { chAsc += 32; chChi += 4; } if (bDD) { chAsc += 8; } if (bCharB) { chAsc += 1; } sCmd[2] = chAsc; SendData(sCmd,3,500); //汉字倍宽倍高模式 sCmd[0]=0x1C; sCmd[1]=0x21; sCmd[2] = chChi; SendData(sCmd,3,500); // sCmd[0]=0x1b; // sCmd[1]=0x2d; // sCmd[2]=bUL ? 1 : 0; // SendData(sCmd,3,500); // sCmd[0]=0x1c; // sCmd[1]=0x57; // sCmd[2]=(bDW | bDH) ? 1 : 0; // SendData(sCmd,3,500); return RECEIPT_SUCCESS; } //设置打印放大倍数(1-6) int ReceiptBase::SetScale(int iX, int iY) { iX--; iY--; if (iX < 0 || iX > 5) iX = 0; if (iY < 0 || iY > 5) iY = 0; unsigned char sCmd[4]; sCmd[0]=0x1D; sCmd[1]=0x21; sCmd[2] = (iX << 4) + iY; int nRet = SendData(sCmd,3,300); m_cSerial.Flush(); if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } //设置黑白反显打印模式 int ReceiptBase::SetInverse(bool b) { unsigned char sCmd[4]; sCmd[0]=0x1D; sCmd[1]=0x42; sCmd[2] = b; int nRet = SendData(sCmd,3,300); m_cSerial.Flush(); if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } //选择/取消顺时针旋转90度 int ReceiptBase::SetRotate(bool b) { unsigned char sCmd[4]; sCmd[0]=0x1B; sCmd[1]=0x56; sCmd[2] = b; int nRet = SendData(sCmd,3,300); m_cSerial.Flush(); if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } //是否黑标纸 int ReceiptBase::SetBlackFlag(bool b) { unsigned char sCmd[4]; sCmd[0]=0x1B; sCmd[1]=0x63; sCmd[2]=0x30; sCmd[3]=b?153:0; int nRet = SendData(sCmd,4,300); m_cSerial.Flush(); if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } //设置黑标位置 int ReceiptBase::SetBlackPos(int iLen) { int iRet = SetBlackFlag(true); if (iRet!=RECEIPT_SUCCESS) return iRet; unsigned char sCmd[6]; sCmd[0]=0x1B; sCmd[1]=0x63; sCmd[2]=0x31; if (iLen < 19) iLen = 19; if (iLen > 65534) iLen = 65534; sCmd[3] = iLen%256; sCmd[4] = iLen/256; int nRet = SendData(sCmd,5,300); m_cSerial.Flush(); if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } //获取固件版本 int ReceiptBase::GetFirmware(char* s) { unsigned char sCmd[128]; sCmd[0]=0x1D; sCmd[1]=0x49; sCmd[2]=3; int nRet = SendData(sCmd,3,300); if (nRet < 0) return nRet; //Sleep(10); int iLen = ReadData(sCmd, 127, 300); if(iLen < 1) return RECEIPT_ERR_RCV_ERR; sCmd[iLen] = 0; strcpy(s, (char*)sCmd); return RECEIPT_SUCCESS; } //打印高速20,中速10,低速 00 int ReceiptBase::SetPrintSpeed(short nSpeed) { return RECEIPT_SUCCESS; } int ReceiptBase::SetRowDistance(int nDis) { unsigned char cpSend[10]={0}; int nRet = 0; if(nDis == 0) { cpSend[0]=0x1B; cpSend[1]=0x32; nRet = SendData(cpSend,2,300); m_iLineHeight = 30; } else { cpSend[0]=0x1B; cpSend[1]=0x33; cpSend[2]=(char)nDis&0xFF; nRet = SendData(cpSend,3,300); m_iLineHeight = nDis; } if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } //设置左边空白 int ReceiptBase::SetLeftDistance(int iLeft) { unsigned char cmd[]={0x1D, 0x4C, 0x00, 0x00}; cmd[2] = iLeft%256; cmd[3] = iLeft/256; int nRet = SendData(cmd,4,300); if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } //设置字符间隔,单位为点(0.125mm) int ReceiptBase::SetCharSpace(char iLeft, char iRight) { char iWide = (iLeft + iRight) >> 1; unsigned char cmd[]={0x1B, 0x20, iWide}; int nRet = SendData(cmd,3,300); if (nRet < 0) return nRet; unsigned char cmd2[]={0x1C, 0x53, (char)iLeft, (char)iRight}; SendData(cmd2,4,300); return RECEIPT_SUCCESS; } //设置打印区域 int ReceiptBase::SetPrintArea(int iLeng) { unsigned char cmd[]={0x1D, 0x57, 0x00, 0x00}; cmd[2] = iLeng%256; cmd[3] = iLeng/256; int nRet = SendData(cmd,4,300); if (nRet < 0) return nRet; return RECEIPT_SUCCESS; } // ctype 1 EAN13 =2CODE128-A =3 CODE128-B =4 CODE128-C // iBytePos 0不打印HRI字符 1条码上方打印 2条码下方打印 3条码上下方打印 int ReceiptBase::PrinterCode(const char* cdata, UINT ctype, UINT iBytePos,UINT BarCodeWidth,UINT BarCodeHeight, bool bCharB) { if(iBytePos > 3) return RECEIPT_ERR_IN_ERR; if(strlen(cdata) > 80) return RECEIPT_ERR_IN_ERR; //字符打印位置 0不打印HRI字符 1条码上方打印 2条码下方打印 3条码上下方打印 char szSendBuf[100]; szSendBuf[0]=0x1D; szSendBuf[1]=0x48; szSendBuf[2]=iBytePos; int nRet = SendData((unsigned char*)szSendBuf, 3,3000); if(nRet < RECEIPT_SUCCESS) return nRet; if(BarCodeWidth<2) BarCodeWidth = 2; if(BarCodeWidth>6) BarCodeWidth = 6; //设置条形码宽度 2 - 6 szSendBuf[0]=0x1D; szSendBuf[1]=0x77; szSendBuf[2]=BarCodeWidth; nRet = SendData((unsigned char*)szSendBuf, 3,3000); if(nRet < RECEIPT_SUCCESS) return nRet; memset(szSendBuf, 0x00, sizeof(szSendBuf)); //条码高度,点 szSendBuf[0]=0x1D; szSendBuf[1]=0x68; szSendBuf[2]=BarCodeHeight; m_iUsedLeng += BarCodeHeight; nRet = SendData((unsigned char*)szSendBuf, 3,3000); if(nRet < RECEIPT_SUCCESS) return nRet; memset(szSendBuf, 0x00, sizeof(szSendBuf)); //条码下面字符的字体 szSendBuf[0]=0x1D; szSendBuf[1]=0x66; szSendBuf[2]=bCharB; nRet = SendData((unsigned char*)szSendBuf, 3,3000); if(nRet < RECEIPT_SUCCESS) return nRet; memset(szSendBuf, 0x00, sizeof(szSendBuf)); unsigned char cmdbuf[256] = {0x1d, 0x6b, }; int iDatPos = 0; if (ctype > 0 && ctype < 9) cmdbuf[2] = ctype + 'A'; else return RECEIPT_ERR_IN_ERR; int nInLen = strlen(cdata); cmdbuf[3] = nInLen; iDatPos = 4; memcpy(cmdbuf+iDatPos,cdata,nInLen); FeedLines(1); nRet = SendData(cmdbuf, iDatPos + nInLen + 1, 3000); if(nRet < RECEIPT_SUCCESS) return nRet; return RECEIPT_SUCCESS; } /////////////////////////////////////////////////////////// int ReceiptBase::Bit24ToBit1(const unsigned char* sInData, int iWidth, int iHigh, unsigned char** psOutData) { //存储模式为4字节对齐 int iInXBytes = ((iWidth * 3) + 3) & 0xfffc; int iX, iY, iPos, iRGB; int iOutXytes = ((iWidth + 31) >> 3) & 0xfffc; int iSizeOut = iOutXytes * iHigh; unsigned char* sOutData = new unsigned char[iSizeOut]; memset(sOutData, 0, iSizeOut); for(iY = 0; iY < iHigh; iY++) { for (iX = 0; iX < iWidth; iX++) { //在真彩图位置和数据 iPos = (iInXBytes * iY) + iX * 3; iRGB = sInData[iPos++] + sInData[iPos++] + sInData[iPos]; //在单色图位置 if (iRGB > 100) { iPos = (iOutXytes * iY) + (iX >> 3); sOutData[iPos] |= (1 << (7-(iX % 8))); } } } *psOutData = sOutData; return iSizeOut; } int ReceiptBase::Bit1ToPrint(unsigned char* sInData, int iWidth, int iHigh, BOOL bRotate, unsigned char** psOutData) { //旋转后的位图 int nBytesOfWidth = ((iWidth+31) >> 3) & 0xfffc; //bmp为标准4字节对齐 int nBytesOfHeight = (iHigh+7) >> 3; //打印像素竖向排列 int nBytesOutWidth = (iWidth+7) >> 3; //bmp为标准4字节对齐,输出为1字节对齐 int iBufSize= nBytesOutWidth * nBytesOfHeight * 8; unsigned char *pOutData = new unsigned char[iBufSize]; *psOutData = pOutData; memset(pOutData,0x00,iBufSize); //反色,BMP的 1 为白色的底色,与打印机定义相反 int iTemp = 0; for(iTemp = 0; iTemp < nBytesOfWidth * iHigh; iTemp++) { sInData[iTemp] = 0xff - sInData[iTemp]; } //BMP为横向排列,先从左到右再从下到上, if (bRotate) { //打印机下载数据为纵向排列,先从上到下再从左到右 for (int iX = 0; iX < iWidth; iX++) { for (int iY = 0; iY < iHigh; iY++) { //计算在bmp中的位置 int iYPos = iHigh - 1 - iY;//Y向倒转后的位置 int iXPos = iX >> 3; //字节位置,/8 //位图中字节位置 iTemp = (nBytesOfWidth * iYPos) + iXPos; //字节中的位置 iXPos = 7 - (iX % 8); //测试结果,高位在左 bool bIsBlack = (sInData[iTemp] >> iXPos) & 1; //打印机单个字节内的顺序为从下到上 //打印机字节位置 iTemp = iX * nBytesOfHeight + (iY >> 3); //字节中的位置,需要Y向倒置 iYPos = (7 - (iY % 8)); pOutData[iTemp] |= (bIsBlack << iYPos); } } } else { int iRight = iWidth % 8; unsigned char chRight = 0; while (iRight) { chRight |= (1 << (8 - iRight)); iRight--; } //打印机下载数据为从上到下 for (int iY = 0; iY < iHigh; iY++) { //计算在bmp中的位置 int iOldStart = nBytesOfWidth * (iHigh - 1 - iY); int iNewStart = nBytesOutWidth * iY; memcpy(pOutData + iNewStart, sInData + iOldStart, nBytesOutWidth); //此处可能会出现右侧黑边,需要处理最后一个字节像素 if (chRight) *(pOutData + iNewStart + nBytesOutWidth - 1) &= chRight; } } return iBufSize; } //位图打印 int ReceiptBase::GetBmpData(const char *sData, bool bBASE64, unsigned char** psOut, int* iPrintW, int* iPrintH, BOOL bRotate) { if (sData==NULL) return RECEIPT_ERR_IN_ERR; unsigned char* sBmpDat=NULL; int iBmpLen=0; if (bBASE64) { int iLen = strlen(sData); if (iLen < 256) return RECEIPT_ERR_IN_ERR; sBmpDat = new unsigned char[iLen+1]; iBmpLen = ::Base64Decode(sData, iLen, sBmpDat, iLen); if (iBmpLen < 256) { delete sBmpDat; return RECEIPT_ERR_IN_ERR; } } else { char sName[128]; if (sData==NULL || sData[0]==0) strcpy(sName,"logo.bmp"); else strcpy(sName,sData); FILE* file = fopen(sName, "rb"); if (file == NULL) return RECEIPT_ERR_IN_ERR; fseek(file, 0, SEEK_END); iBmpLen=ftell(file); fseek(file,0, SEEK_SET); if (iBmpLen<192) return RECEIPT_ERR_IN_ERR; sBmpDat=new unsigned char[iBmpLen+1]; fread(sBmpDat, 1, iBmpLen,file); fclose(file); } /////////////////////////////// BITMAPFILEHEADER bfh; BITMAPINFOHEADER bih; int iReadPos = 0; memcpy(&bfh, sBmpDat, sizeof(bfh)); iReadPos = sizeof(bfh); memcpy(&bih, sBmpDat + iReadPos, sizeof(bih)); iReadPos = bfh.bfOffBits; int iSizeAll = (bih.biWidth * bih.biHeight * bih.biBitCount) >> 3; if (iBmpLen < iSizeAll + iReadPos) return RECEIPT_ERR_IN_ERR; //获得实际图像尺寸 *iPrintW = bih.biWidth; *iPrintH = bih.biHeight; unsigned char* sTemp = NULL; if (bih.biBitCount == 24) { Bit24ToBit1(sBmpDat + iReadPos, bih.biWidth, bih.biHeight, &sTemp); iBmpLen = Bit1ToPrint(sTemp, bih.biWidth, bih.biHeight, bRotate, psOut); delete sBmpDat; return iBmpLen; } if (bih.biBitCount == 1) { iBmpLen = Bit1ToPrint(sBmpDat+iReadPos, bih.biWidth, bih.biHeight, bRotate, psOut); delete sBmpDat; return iBmpLen; } ////////////////////////////// return RECEIPT_ERR_IN_ERR; } //功能:将单色 bmp 图像数据下载到硬件 int ReceiptBase::DownloadNVBmp(const char *sData, bool bBASE64) { unsigned char* sBmpDat=NULL; int iWidth, iHeight; int iRet = GetBmpData(sData, bBASE64, &sBmpDat, &iWidth, &iHeight, TRUE); if (iRet < 0) return iRet; int iWidthByte = (iWidth+7) >> 3; //bmp4字节对齐,此处不需要,1字节对齐即可 int iHeightByte = (iHeight+7) >> 3; m_iNVHeight = iHeightByte * 8; //下载位图 unsigned char cmd1[8]={0x1C, 0x71, 1, 0, 0, 0, 0}; cmd1[3] = iWidthByte; cmd1[5] = iHeightByte; SendData(cmd1, 7); // Sleep(10); int iMax = iWidthByte * iHeightByte * 8; for (int ia=0; ia> 3; //宽度字节数 int iWidthDown = iWidthByte < 80 ? iWidthByte : 80; int iOutHeight = ((4096 / iWidthDown) / 24) * 24; //高度需要是24倍数 unsigned char sSend[4*1024]; sSend[0] = 0x1D; //光栅位图打印 sSend[1] = 0x76; sSend[2] = 0x30; sSend[3] = 0; sSend[4] = iWidthDown; //宽度方向字节数 sSend[5] = 0; sSend[7] = 0; int iSendPos; for (int iHPos = 0; iHPos < iHeight; iHPos += iOutHeight) { int iBmpHeight = (iHeight - iHPos < iOutHeight) ? iHeight - iHPos : iOutHeight; sSend[6] = iBmpHeight; //高度方向点数 iSendPos = 8; for ( int ia = 0; ia < iBmpHeight; ia++)//宽度循环 { memcpy(sSend + iSendPos, sBmpDat + iWidthByte * (iHPos + ia), iWidthDown); iSendPos += iWidthDown; } SendData(sSend, iSendPos); Sleep(100); } delete sBmpDat; m_iUsedLeng += iHeight; return RECEIPT_SUCCESS; } //原有,转换成字符串 short ReceiptBase::GetBinBuf(short val,char *buf) { char sBufs[][8]= { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; if (val >= 0 && val <= 15) { memcpy(buf, sBufs[val], 4); return 0; } return 1; } //原有,变量转化成字串 short ReceiptBase::ByteToBytes(short value,char *buf) { char low_byte,high_byte,low_buf[5],high_buf[5]; short ret1,ret2; memset(low_buf,'\0',5); memset(high_buf,'\0',5); if(value<0 || value>255) return 2; //非法值 low_byte=value%16; high_byte=value/16; ret1=GetBinBuf(low_byte,low_buf); ret2=GetBinBuf(high_byte,high_buf); if(ret1!=0 || ret2!=0) return 1; //计算错误 memcpy(buf,high_buf,4); memcpy(buf+4,low_buf,4); return 0; } int ReceiptBase::GetStateData(const char* sCmd1, const char* sCmd2, const char* sCmd3, const char* sCmd4) { if(sCmd2[1] == '1' || sCmd3[4]=='1') //打印机故障 return RECEIPT_ERR_DEV_ERR; if(sCmd2[5] == '1') //打印机头抬起 return RECEIPT_WARN_HEAD_UP; if(sCmd4[7] == '1') //卡纸 return RECEIPT_WARN_JAMMED; if(sCmd4[2] == '1') //缺纸 return RECEIPT_WARN_NO_MEDIA; if(sCmd4[4] == '1') //纸将尽 return RECEIPT_WARN_PAP_LOW; return RECEIPT_SUCCESS; } int ReceiptBase::GetState() { char sState[4][16]; int iRet = ReadStateData(NULL, sState[1], sState[2], sState[3]); if (iRet != RECEIPT_SUCCESS) return iRet; return GetStateData(NULL, sState[1], sState[2], sState[3]); } //发送状态查询命令 int ReceiptBase::ReadStateData(short nNum,char * buf) { unsigned char szSendBuf[20]; int ret,back; char chRcvbuf[10] = {0}; short nRcvlen; int nRet = 0; memset(szSendBuf,'\0',20); szSendBuf[0]=0x10; szSendBuf[1]=0x04; szSendBuf[2]=(char)nNum; nRet = SendData(szSendBuf,3,300); if(nRet <=0 ) { return RECEIPT_ERR_RCV_ERR; } //Sleep(10); nRcvlen = ReadData((unsigned char*)chRcvbuf, 1, 3000); if(nRcvlen < 1) { return RECEIPT_ERR_RCV_ERR; } back=chRcvbuf[0]; ret=ByteToBytes(back,buf); if(ret!=0) { return RECEIPT_ERR_IN_ERR; } buf[8] = 0; return RECEIPT_SUCCESS; } //取状态 int ReceiptBase::ReadStateData(char * buf1, char* buf2, char * buf3, char * buf4) { int nRet = 0; if(buf1) { nRet = ReadStateData(1,buf1); if(nRet != RECEIPT_SUCCESS) return nRet; } ///////////////// if(buf2) { nRet = ReadStateData(2,buf2); if(nRet != RECEIPT_SUCCESS) return nRet; } ///////////////// if(buf3) { nRet = ReadStateData(3,buf3); if(nRet != RECEIPT_SUCCESS) return nRet; } ///////////////// if(buf4) { nRet = ReadStateData(4,buf4); return nRet; } return RECEIPT_SUCCESS; }