MFC(C++)实现斑马标签打印机打印

项目场景

过年刚上来,领导分配了工厂项目需求过来。MFC框架,语言C++,通过串口读出设备的MAC地址,然后通过调用斑马打印机打印出对应的二维码标签,经过一些了解比原想的简单,很快就实现了,下面分享下过程。

问题描述

本人也是个老程序员了,C++多年未接触,但这问题不大。首先安装打印机驱动,打印测试,确保直接能够打印出来,打印功能正常。按照以往的经验需要到下载对应的SDK,然后调用相关的API,奈何度娘了一圈,信息很少,大都是C#的,会用到一些库。继续筛选信息,看到一篇用C++实现斑马打印机打印的文章,其中并没有说用到SDK和库的信息,是调用win32 API实现驱动,那就试试吧。抄过来简单改改,尝试发现能够正常驱动打印机,并且标签有跳动,只是标签纸没有东西打印出来,好现象。继续研究,发现打印内容设置是通过ZPL语言设置,网上找一个打印二维码的指令,把MAC地址塞进去,即可。搞定,万事大吉!

MFC(C++)实现斑马标签打印机打印_第1张图片


代码实现

代码实现如下

int Zebra::printrjtlabel(const char* mac)
{
    LPTSTR szPrinterName;
    LPBYTE lpData;
    DWORD dwCount;
    HANDLE     hPrinter;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob;
    DWORD      dwBytesWritten;

    /*
    ^XA 开始
    ^XZ 结束
    ^LL 设置标签长度(我用的打印机是TSC TE344,这好像没有效果)
    ^PW 设置标签宽度
    ^FS 换行
    ^FS 注释
    ^LH0,0 纸张的边距,左边距和上边距
    ^FO0,0 设置打印内容坐标
    */

    //lpData = (LPBYTE)("^XA ^LH5,0,^FO20,20 ^BQ,2,3 ^FDLA,0123456789ABCD ^XZ");
    //dwCount = strlen("^XA ^LH5,0,^FO20,20 ^BQ,2,3 ^FDLA,0123456789ABCD ^XZ");

    std::string printcontents = "^XA ^LH5,0,^FO20,20 ^BQ,2,3 ^FDLA,";
    printcontents = printcontents + std::string(mac);
    printcontents = printcontents + " ^XZ";

    lpData = (LPBYTE)printcontents.c_str();
    dwCount = strlen(printcontents.c_str());

    // Need a handle to the printer. 
    szPrinterName = _T("ZDesigner ZT230-200dpi ZPL");//打印机名称

    if (!OpenPrinter(szPrinterName, &hPrinter, NULL))
    {

        return -1;
    }

    // Fill in the structure with info about this "document."  
    DocInfo.pDocName = TEXT("My Document");
    DocInfo.pOutputFile = NULL;
    DocInfo.pDatatype = TEXT("RAW");
    // Inform the spooler the document is beginning.  
    if ((dwJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo)) == 0)
    {

        ClosePrinter(hPrinter);
        return -2;
    }
    // Start a page.  
    if (!StartPagePrinter(hPrinter))
    {

        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return -3;
    }
    // Send the data to the printer.  
    if (!WritePrinter(hPrinter, lpData, dwCount, &dwBytesWritten))
    {

        EndPagePrinter(hPrinter);
        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return -4;
    }
    // End the page.  
    if (!EndPagePrinter(hPrinter))
    {

        EndDocPrinter(hPrinter);
        ClosePrinter(hPrinter);
        return -5;
    }
    // Inform the spooler that the document is ending.  
    if (!EndDocPrinter(hPrinter))
    {

        ClosePrinter(hPrinter);
        return -6;
    }
    // Tidy up the printer handle.  
    ClosePrinter(hPrinter);
    // Check to see if correct number of bytes were written.  
    if (dwBytesWritten != dwCount)
    {

        return -7;
    }
    return 0;
}

代码调用如下

int result = Zebra::printrjtlabel(macFromCsv);
		
//sprintf(logbuf, "打印返回 result = %d", result);
//SaveLog(hWnd, index, WP_PRINT_LOG_STR, logbuf, strlen(logbuf));

if (result < 0) {
  AfxMessageBox(_T("打印机打开失败!"));
  //goto ERR_HANDLE;
}

总结

许多事情就是这样,看似比登天还难的事情,有时候轻而易举就能做到。我们要坦然接受突如其来的变化,珍惜不期而遇的惊喜。加油,各位老铁!

你可能感兴趣的:(#,C/C++,mfc,c++,斑马打印机)