| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262 |
- #include "../pch/pch.h"
- #include "CPosPrinter.h"
- #include "../tool/CComHelper.h"
- #include "../helper/CBitmapHelper.h"
- #include <qrencode/qrencode.h>
- CPosPrinter::CPosPrinter(): m_socket(m_io)
- {
- }
- CPosPrinter::~CPosPrinter()
- {
- }
- /*
- *usbType是1表示只查找收银小票打印机,会排除已知的标签打印机
- *usbType为2表示所有usb打印机都查找,包含标签与非标签
- **/
- void CPosPrinter::InitUsb(int usbType)
- {
- //设置中文字符
- setlocale(LC_CTYPE, "chs");
- //取设备路径
- int nDevice = GetDevicePath((LPGUID)&USB_GUID, usbType);
- LOG_INFO("可用的USB打印机数量:" << nDevice);
- //添加usb端口
- int i = 0;
- while(i < nDevice)
- {
- std::string setting_biaoqian_printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
- std::wstring ws_setting_biaoqian_printer_usb = CLewaimaiString::UTF8ToUnicode(setting_biaoqian_printer_usb);
- if (CSetting::GetParam("setting_is_new_waimai_biaoqian_printer") == "1" && ws_setting_biaoqian_printer_usb == m_usb_devices[i])
- {
- //碰到了一个标签打印机
- i++;
- continue;
- }
- LOG_INFO("准备打开端口 Port = " << m_usb_devices[i].c_str());
- HANDLE hPort = CreateFile(m_usb_devices[i].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if(hPort == INVALID_HANDLE_VALUE)
- {
- LOG_INFO("打开USB端口失败, Port = " << m_usb_devices[i].c_str());
- // 打开端口失败
- DWORD error = GetLastError();
- if(error == 2)
- {
- //没有指定的文件
- LOG_INFO("没有找对对应的usb端口");
- }
- else if(error == 5)
- {
- LOG_INFO("usb端口被占用!");
- }
- continue;
- }
- LOG_INFO("打开usb端口成功,准备进行打印机检测! hPort:" << hPort);
- if(PortTest(hPort) == true)
- {
- //端口测试连通,保存起来
- PrinterHandle newHandle;
- newHandle.hPort = hPort;
- newHandle.type = 1;
- m_hPorts.push_back(newHandle);
- }
- else
- {
- //端口打印机没有连接,那么就直接关闭掉并口,避免占用
- CloseHandle(hPort);
- }
- i++;
- }
- }
- bool CPosPrinter::InitOneUsb(wstring usb_path)
- {
- if (usb_path == L"")
- {
- return false;
- }
- HANDLE hPort = CreateFile(usb_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if(hPort == INVALID_HANDLE_VALUE)
- {
- LOG_INFO("打开USB端口失败, Port = " << usb_path.c_str());
- // 打开端口失败
- DWORD error = GetLastError();
- if(error == 2)
- {
- //没有指定的文件
- LOG_INFO("没有找对对应的usb端口");
- }
- else if(error == 5)
- {
- LOG_INFO("usb端口被占用!");
- }
- return false;
- }
- LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
- if(PortTest(hPort) == true)
- {
- //端口测试连通,保存起来
- PrinterHandle newHandle;
- newHandle.hPort = hPort;
- newHandle.type = 1;
- m_hPorts.push_back(newHandle);
- }
- else
- {
- //端口打印机没有连接,那么就直接关闭掉并口,避免占用
- CloseHandle(hPort);
- return false;
- }
- return true;
- }
- bool CPosPrinter::InitBingkou()
- {
- std::wstring LptStr = L"lpt1";
- HANDLE hPort = CreateFile(LptStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if(hPort == INVALID_HANDLE_VALUE)
- {
- LOG_INFO("打开并口失败, Port = " << LptStr.c_str());
- DWORD error = GetLastError();
- if(error == 2)
- {
- //没有指定的文件
- LOG_INFO("本机器没有并口!");
- }
- else if(error == 5)
- {
- LOG_INFO("并口被占用!");
- }
- return false;
- }
- else
- {
- //这个表示并口可以使用
- LOG_INFO("找到并口,准备进行打印机检测! hPort:" << hPort);
- if(PortTest(hPort) == true)
- {
- //并口测试连通,保存起来
- PrinterHandle newHandle;
- newHandle.hPort = hPort;
- newHandle.type = 2;
- m_hPorts.push_back(newHandle);
- }
- else
- {
- //并口打印机没有连接,那么就直接关闭掉并口,避免占用
- CloseHandle(hPort);
- return false;
- }
- }
- return true;
- }
- void CPosPrinter::InitCom()
- {
- CComHelper helper;
- std::vector<std::wstring> comVector = helper.getComPort();
- for(std::vector<std::wstring>::iterator it = comVector.begin(); it != comVector.end(); it++)
- {
- std::wstring com2Str = *it;
- HANDLE hPort = CreateFile(com2Str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if(hPort == INVALID_HANDLE_VALUE)
- {
- DWORD error = GetLastError();
- if(error == 2)
- {
- //没有指定的文件
- LOG_INFO("没有找到对应的串口 " << com2Str.c_str());
- }
- else if(error == 5)
- {
- LOG_INFO("串口被占用 " << com2Str.c_str());
- }
- }
- else
- {
- LOG_INFO(com2Str.c_str() << " success, handle:" << hPort);
- //扎到了可用的串口,设置串口波特率
- DCB dcb;
- dcb.DCBlength = sizeof(dcb);
- GetCommState(hPort, &dcb);
- //佳博的串口打印机,默认是这个波特率
- dcb.BaudRate = 19200;
- if(!SetCommState(hPort, &dcb))
- {
- LOG_INFO("set baudRate failed!");
- CloseHandle(hPort);
- continue;
- }
- //设定通讯端口超时参数
- COMMTIMEOUTS tmouts;
- tmouts.ReadIntervalTimeout = 100;
- tmouts.ReadTotalTimeoutMultiplier = 100;
- tmouts.ReadTotalTimeoutConstant = 100;
- tmouts.WriteTotalTimeoutConstant = 100;
- tmouts.WriteTotalTimeoutMultiplier = 100;
- SetCommTimeouts(hPort, &tmouts);
- //设置端口缓冲
- SetupComm(hPort, 1024, 1024);
- //清除通讯端口缓存
- PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
- //保存端口
- PrinterHandle newHandle;
- newHandle.hPort = hPort;
- newHandle.type = 3;
- m_hPorts.push_back(newHandle);
- }
- }
- }
- /*
- *找到所有可用的小票打印机,这里只初始化USB,不包含并口、串口、网口
- *智能识别模式不再自动处理串口,串口的需要人工去选择类型
- **/
- void CPosPrinter::InitShouyin()
- {
- //开始添加usb
- InitUsb();
- }
- /*
- *获取CreateFile的USB端口号
- **/
- int CPosPrinter::GetDevicePath(LPGUID lpGuid, int usbType)
- {
- HDEVINFO hDevInfoSet;
- SP_DEVINFO_DATA spDevInfoData;
- SP_DEVICE_INTERFACE_DATA ifData;
- PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
- int nCount;
- int nTotle;
- BOOL bResult;
- //这2个字符串,用于根据usb的名字对比是否为打印机设备
- wstring strUSBPrint = TEXT("USB 打印支持");
- //xp上是英文
- wstring strUSBPrint_EN = L"USB Printing Support";
- // 取得一个该GUID相关的设备信息集句柄
- hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
- NULL, // 无关键字
- NULL, // 不指定父窗口句柄
- DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
- // 失败...
- if(hDevInfoSet == INVALID_HANDLE_VALUE)
- {
- LOG_INFO("SetupDiGetClassDevs failed \r\n");
- return 0;
- }
- // 申请设备接口数据空间
- pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
- pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
- nTotle = -1;
- nCount = 0;
- bResult = TRUE;
- // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
- while(bResult)
- {
- nTotle++;
- spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
- // 枚举符合该GUID的设备接口
- bResult = ::SetupDiEnumDeviceInfo(
- hDevInfoSet, // 设备信息集句柄
- (ULONG)nTotle, // 设备信息集里的设备序号
- &spDevInfoData); // 设备接口信息
- if(bResult)
- {
- DWORD DataT;
- TCHAR buf[MAX_PATH] = { 0 };
- DWORD nSize = 0;
- // get Friendly Name or Device Description
- if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
- SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
- {
- }
- else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
- SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
- {
- }
- else
- {
- lstrcpy(buf, _T("Unknown"));
- }
- wstring ws_buf = buf;
- //是否是要找的设备类型
- if(ws_buf != strUSBPrint && ws_buf != strUSBPrint_EN)
- {
- LOG_INFO("not good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
- continue;
- }
- LOG_INFO("good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
- ifData.cbSize = sizeof(ifData);
- // 枚舉符合該GUID的設備接口
- bResult = ::SetupDiEnumDeviceInterfaces(
- hDevInfoSet, // 設備信息集句柄
- NULL, // 不需額外的設備描述
- lpGuid, // GUID
- (ULONG)nTotle, // 設備信息集里的設備序號
- &ifData); // 設備接口信息
- if(bResult)
- {
- // 取得该设备接口的细节(设备路径)
- bResult = SetupDiGetInterfaceDeviceDetail(
- hDevInfoSet, // 设备信息集句柄
- &ifData, // 设备接口信息
- pDetail, // 设备接口细节(设备路径)
- INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
- NULL, // 不需计算输出缓冲区大小(直接用设定值)
- NULL); // 不需额外的设备描述
- if(bResult)
- {
- wstring DevicePath = pDetail->DevicePath;
- wstring vid = DevicePath.substr(DevicePath.find(_T("vid_"), 0) + 4, 4);
- wstring pid = DevicePath.substr(DevicePath.find(_T("pid_"), 0) + 4, 4);
- LOG_INFO("Vid:" << vid.c_str() << ", Pid:" << pid.c_str());
- if(usbType == 1 && GetPrinterType(vid, pid) == 2)
- {
- //标签打印机,暂时不处理打印
- LOG_INFO("标签打印机,暂时不打印!");
- continue;
- }
- // 复制设备路径到输出缓冲区
- m_usb_devices.push_back(DevicePath);
- // 调整计数值
- nCount++;
- LOG_INFO("Cnt = " << nCount << ",pDetail->DevicePath =" << pDetail->DevicePath);
- }
- }
- }
- else
- {
- //LOG_INFO("SetupDiEnumDeviceInfo FAILED!@");
- }
- }
- // 释放设备接口数据空间
- ::GlobalFree(pDetail);
- // 关闭设备信息集句柄
- ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
- return nCount;
- }
- /*
- *返回打印机的类型, 1:普通热敏打印机 2:标签打印机 3:发票打印机
- *这里因为不同厂家对vid pid定义不一样,有时候会出现佳博的标签打印机的vid和pid,在别的品牌上变成了热敏打印机,这里如果排除掉别的品牌的热敏打印机就无法打印了,所以这里不能轻易排除
- **/
- int CPosPrinter::GetPrinterType(wstring vid, wstring pid)
- {
- if(vid == L"6868" && pid == L"0500")
- {
- //佳博标签打印机
- return 2;
- }
- else if (vid == L"XXX" && pid == L"XXX")
- {
- //发票打印机
- return 3;
- }
- return 1;
- }
- /*
- *打印收银小票
- **/
- void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
- {
- LOG_INFO("_______________________________开始打印收银小票,订单编号:"<<order.m_order_num.c_str()<<"_________________________");
- //设置模式,后面输出数据不会错
- m_type = 1;
- std::string printer_leixing = CSetting::GetParam("setting_printer_leixing");
- if(printer_leixing == "auto")
- {
- InitShouyin();
- }
- else if(printer_leixing == "usb")
- {
- std::string printer_usb = CSetting::GetParam("setting_printer_usb");
- std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
- bool ret = InitOneUsb(ws_printer_usb);
- if (ret == false)
- {
- return;
- }
- }
- else if(printer_leixing == "bingkou")
- {
- bool ret = InitBingkou();
- if (ret == false)
- {
- return;
- }
- }
- else if(printer_leixing == "chuankou")
- {
- InitCom();
- }
- else if(printer_leixing == "wangkou")
- {
- m_type = 2;
- std::string wangkou_ip = CSetting::GetParam("setting_printer_wangkou_ip");
- //初始化连接
- try
- {
- boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(wangkou_ip.c_str()), 9100);
- m_socket.connect(ep);
- }
- catch(std::exception& e)
- {
- std::string err = e.what();
- LOG_INFO("网口打印机连接失败,IP地址:" << wangkou_ip.c_str() << ",错误信息:" << err.c_str());
- MessageBoxW(NULL, (L"厨房网口打印机连接失败,IP地址:" + CLewaimaiString::UTF8ToUnicode(wangkou_ip)).c_str(), L"打印机连接失败", MB_OK);
- }
- }
- //读取当前收银打印机的设置
- std::string guige = CSetting::GetParam("setting_printer_guige");
- std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
- int n_lianshu = atoi(lianshu.c_str());
- //正式开始打印
- for(int i = 0; i < n_lianshu; i++)
- {
- POS_Reset();
- //打印店铺名字
- string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
- POS_TextOut(shop_name, true, true, 1);
- POS_FeedLine();
- POS_FeedLine();
- //打印小票自定义名称
- string printer_tip_name = CLewaimaiString::UTF8ToANSI(order.m_printer_tip_name);
- if (printer_tip_name.length() > 0)
- {
- POS_TextOut(printer_tip_name, false, true, 1);
- POS_FeedLine();
- POS_FeedLine();
- }
- //打印预约、即时配送的提醒
- /*
- bool setting_printer_peisongshijian_big = false;
- if (CSetting::GetParam("setting_printer_peisongshijian_big") == "1")
- {
- setting_printer_peisongshijian_big = true;
- }
- }*/
- if (order.m_is_selftake == "1")
- {
- string date = "【自取单】" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
- POS_TextOut(date, false, true);
- POS_TextOut("取货", false, false);
- POS_FeedLine();
- }
- else
- {
- if (order.m_is_book == "1")
- {
- //预约单
- string date = "【预约单】" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
- POS_TextOut(date, false, true);
- POS_TextOut("送达", false, false);
- POS_FeedLine();
- }
- else
- {
- //非预约单
- string date = "【即时单】立即送达";
- POS_TextOut(date, false, true, 1);
- POS_FeedLine();
- }
- }
- POS_FeedLine();
- //打印取货方式
- /*
- bool setting_printer_quhuo_big = false;
- if(CSetting::GetParam("setting_printer_quhuo_big") == "1")
- {
- setting_printer_quhuo_big = true;
- }
- if(order.m_is_selftake == "1")
- {
- POS_TextOut("到店自取订单", false, setting_printer_quhuo_big, 1);
- POS_FeedLine();
- POS_FeedLine();
- }
- else
- {
- POS_TextOut("外卖订单", false, setting_printer_quhuo_big, 1);
- POS_FeedLine();
- }
- POS_FeedLine();
- */
- //订单来源
- string from_type = "订单来源:" + CLewaimaiString::UTF8ToANSI(order.m_from_type);
- POS_TextOut(from_type);
- POS_FeedLine();
- //订单号
- bool setting_printer_dingdanhao_big = false;
- if(CSetting::GetParam("setting_printer_dingdanhao_big") == "1")
- {
- setting_printer_dingdanhao_big = true;
- }
- string order_num = "订 单 号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
- POS_TextOut(order_num, false, setting_printer_dingdanhao_big);
- POS_FeedLine();
- //下单时间
- bool setting_printer_xiadanshijian_big = false;
- if(CSetting::GetParam("setting_printer_xiadanshijian_big") == "1")
- {
- setting_printer_xiadanshijian_big = true;
- }
- string order_date = "下单时间:" + CLewaimaiString::UTF8ToANSI(order.m_order_date);
- POS_TextOut(order_date, false, setting_printer_xiadanshijian_big);
- POS_FeedLine();
- //姓名
- bool setting_printer_name_big = false;
- if(CSetting::GetParam("setting_printer_name_big") == "1")
- {
- setting_printer_name_big = true;
- }
- if(order.m_is_selftake != "1")
- {
- string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
-
- //把下单次数也一起打印
- if (order.m_showordernum == "1")
- {
- name += "(第 " + CLewaimaiString::UTF8ToANSI(order.m_customer_order_total) + " 次下单)";
- }
- POS_TextOut(name, false, setting_printer_name_big);
- POS_FeedLine();
- }
- //电话
- bool setting_printer_phone_big = false;
- if(CSetting::GetParam("setting_printer_phone_big") == "1")
- {
- setting_printer_phone_big = true;
- }
- string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
- POS_TextOut(phone, false, setting_printer_phone_big);
- POS_FeedLine();
- //地址
- bool setting_printer_address_big = false;
- if(CSetting::GetParam("setting_printer_address_big") == "1")
- {
- setting_printer_address_big = true;
- }
- if(order.m_is_selftake != "1")
- {
- string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
- POS_TextOut(address, false, setting_printer_address_big);
- POS_FeedLine();
- }
- //预设选项
- if(order.m_order_field.size() > 1)
- {
- for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
- {
- string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
- POS_TextOut(order_field, false, false, 0);
- POS_FeedLine();
- }
- }
- //准备开始打印商品详情
- string lines;
- if(guige == "58")
- {
- lines = "--------------------------------";
- }
- else
- {
- lines = "------------------------------------------------";
- }
- POS_TextOut(lines);
- POS_FeedLine();
- //商品标题
- bool setting_printer_shangpin_big = false;
- if(CSetting::GetParam("setting_printer_shangpin_big") == "1")
- {
- setting_printer_shangpin_big = true;
- }
- if(guige == "58")
- {
- POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
- }
- else
- {
- POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
- }
- POS_FeedLine();
- if (order.m_manzeng_name.length() > 0)
- {
- CWaimaiOrderItem manzengItem;
- manzengItem.m_food_name = CLewaimaiString::ANSIToUTF8("【满赠】" + CLewaimaiString::UTF8ToANSI(order.m_manzeng_name));
- order.m_order_items.push_back(manzengItem);
- }
- //商品内容
- for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
- {
- std::string food_name = (*it).m_food_name;
- std::string food_price = (*it).m_item_price;
- std::string quantity = (*it).m_quantity;
- //计算总价
- double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
- std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
- if (food_total_price == "0.00")
- {
- //这里是特殊处理满赠的展示
- food_total_price = "";
- }
- food_name = CLewaimaiString::UTF8ToANSI(food_name);
- //先输出商品名字
- std::vector<string> m_names;
- if(guige == "58")
- {
- m_names = HandleFoodname(food_name, 1);
- }
- else
- {
- m_names = HandleFoodname(food_name, 2);
- }
- std::string firstLine = m_names[0];
- int nGuige;
- if(guige == "58")
- {
- nGuige = 1;
- }
- else
- {
- nGuige = 2;
- }
- std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
- std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
- std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
- std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
- POS_TextOut(firstLineShow, false, setting_printer_shangpin_big);
- POS_FeedLine();
- for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
- {
- POS_TextOut((*it), false, setting_printer_shangpin_big);
- POS_FeedLine();
- }
- }
- //判断是否有备注
- if(order.m_memo.length() > 0)
- {
- bool setting_printer_memo_big = false;
- if(CSetting::GetParam("setting_printer_memo_big") == "1")
- {
- setting_printer_memo_big = true;
- }
- POS_FeedLine();
- string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
- POS_TextOut(address, false, setting_printer_memo_big);
- POS_FeedLine();
- }
- //结束商品详情打印
- POS_TextOut(lines);
- POS_FeedLine();
- //开始打印其他的费用
- bool is_other_money = false;
- bool setting_printer_price_big = false;
- if(CSetting::GetParam("setting_printer_price_big") == "1")
- {
- setting_printer_price_big = true;
- }
- if(order.m_delivery > 0)
- {
- string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
- POS_TextOut(delivery, false, setting_printer_price_big);
- POS_FeedLine();
- is_other_money = true;
- }
- if(order.m_is_dabao == "1")
- {
- string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
- POS_TextOut(dabao, false, setting_printer_price_big);
- POS_FeedLine();
- is_other_money = true;
- }
- //开始显示增值服务费
- if(order.m_addservie.size() > 0)
- {
- for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
- {
- string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
- POS_TextOut(addservice, false, setting_printer_price_big);
- POS_FeedLine();
- is_other_money = true;
- }
- is_other_money = true;
- }
- if(order.m_discount < 10)
- {
- string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
- POS_TextOut(discount, false, setting_printer_price_big);
- POS_FeedLine();
- is_other_money = true;
- }
- if(order.m_is_member_discount == "1")
- {
- string member_discount = "会员优惠:-" + order.m_member_discount;
- POS_TextOut(member_discount, false, setting_printer_price_big);
- POS_FeedLine();
- is_other_money = true;
- }
- if(order.m_is_firstcut == "1")
- {
- string firstcut = "首单优惠:-" + order.m_firstcut_value;
- POS_TextOut(firstcut, false, setting_printer_price_big);
- POS_FeedLine();
- is_other_money = true;
- }
- //满减
- string::size_type position = order.m_promotion.find(":");
- if(position != order.m_promotion.npos)
- {
- string promotion_value;
- promotion_value.assign(order.m_promotion, position + 1);
- string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
- POS_TextOut(promotion, false, setting_printer_price_big);
- POS_FeedLine();
- is_other_money = true;
- }
- if(order.m_coupon > 0)
- {
- string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
- POS_TextOut(coupon, false, setting_printer_price_big);
- POS_FeedLine();
- is_other_money = true;
- }
- if(is_other_money)
- {
- POS_TextOut(lines);
- POS_FeedLine();
- }
- //最后显示总价
- POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, setting_printer_price_big, 2);
- POS_FeedLine();
- //显示付款方式
- bool setting_printer_pay_big = false;
- if(CSetting::GetParam("setting_printer_pay_big") == "1")
- {
- setting_printer_pay_big = true;
- }
- POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, setting_printer_pay_big, 2);
- POS_FeedLine();
- POS_FeedLine();;
- POS_FeedLine();
- //准备开始打印二维码
- std::string order_no = order.m_order_no;
- order_no = CLewaimaiString::UTF8ToANSI(order_no);
- if (order.m_show_qiangdan_qrcode == "1")
- {
- POS_OutQRCode(order_no);
- POS_FeedLine();
- POS_FeedLine();
- POS_TextOut("请使用配送员app扫码绑定订单", false, false, 1);
- }
- //走纸几行再切
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_CutPaper();
- }
- //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
- POS_Reset();
- //关闭设备
- for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
- {
- CloseHandle((*it).hPort);
- }
- if (m_type == 2)
- {
- m_socket.close();
- }
- LOG_INFO("_______________________________结束打印收银小票,订单编号:" << order.m_order_num.c_str() << "_________________________");
- LOG_INFO(" ");
- }
- void CPosPrinter::PrintWaimaiOrderBiaoqian(CWaimaiOrder& order)
- {
- m_type = 1;
- std::string printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
- std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
- //连接usb端口
- InitOneUsb(ws_printer_usb);
- //初始化标签打印机
- BIAOQIAN_Reset();
- std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
- //先计算商品的总数量
- int foodNum = 0;
- for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
- {
- std::string quantity = (*it).m_quantity;
- foodNum += atoi(quantity.c_str());
- }
- //商品内容
- int curFoodNum = 0;
- for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
- {
- std::string food_name = (*it).m_food_name;
- std::string food_price = (*it).m_item_price;
- std::string quantity = (*it).m_quantity;
- //每份商品打印一个标签
- int nQuantity = atoi(quantity.c_str());
- for(int i = 0; i < nQuantity; i++)
- {
- std::string textData = "";
- curFoodNum++;
- std::string restaurant_number = order.m_restaurant_number;
- std::string order_num_info = "外卖 #" + CLewaimaiString::UTF8ToANSI(restaurant_number);
- textData += BIAOQIAN_TEXTGet(order_num_info, 16, 24, 1, 1);
- //打印份数
- std::string numInfo = to_string(curFoodNum) + "/" + to_string(foodNum);
- textData += BIAOQIAN_TEXTGet(numInfo, 160, 24, 1, 1);
- //每行最多显示12个汉字,这里要计算一下换行(要先转成ANSI格式)
- std::string handle_food_name = CLewaimaiString::UTF8ToANSI(food_name);
- std::vector<std::string> foodNameVector = HandleBiaoqianFoodname(handle_food_name);
- int nRow = 0;
- //考虑是否用大号字体
- bool is_big_name = false;
- if(foodNameVector.size() <= 2)
- {
- //不超过2行,可以用大的字体
- is_big_name = true;
- }
- LOG_INFO("foodNameVector size:" << foodNameVector.size());
- for(std::vector<std::string>::iterator it = foodNameVector.begin(); it != foodNameVector.end(); it++)
- {
- LOG_INFO("nRow:" << nRow << ", 准备打印商品名字:" << (*it).c_str());
- if(is_big_name)
- {
- textData += BIAOQIAN_TEXTGet(*it, 16, 64 + 64 * nRow, 1, 2);
- }
- else
- {
- textData += BIAOQIAN_TEXTGet(*it, 16, 64 + 32 * nRow, 1, 1);
- }
- nRow++;
- }
- //打印价格
- std::string priceInfo = CLewaimaiString::UTF8ToANSI(food_price) + "元";
- textData += BIAOQIAN_TEXTGet(priceInfo, 16, 188, 1, 1);
- //打印订单号
- std::string order_num = "订单号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
- textData += BIAOQIAN_TEXTGet(order_num, 16, 218, 1, 1);
- WriteData(textData);
- BIAOQIAN_PRINT();
- BIAOQIAN_CLS();
- }
- }
- }
- void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
- {
- //设置模式,后面输出数据不会错
- m_type = 2;
- //读取厨房打印机信息
- std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
- for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
- {
- ChufangPrinter printer = *it;
- std::string ip = printer.ip;
- //初始化连接
- try
- {
- boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
- m_socket.connect(ep);
- }
- catch(std::exception& e)
- {
- std::string err = e.what();
- LOG_INFO("厨房网口打印机连接失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
- MessageBoxW(NULL, (L"厨房网口打印机连接失败,IP地址:" + CLewaimaiString::UTF8ToUnicode(ip)).c_str(), L"打印机连接失败", MB_OK);
- //连接失败了,处理下一个厨房打印机
- continue;
- }
- std::string guige = printer.guige;
- std::string fendan = printer.fendan;
- std::string fenlei = printer.fenlei;
- std::string fenlei_ids = printer.fenlei_ids;
- std::vector<CWaimaiOrderItem> cur_printer_use;
- if (fenlei == "0")
- {
- cur_printer_use = order.m_order_items;
- }
- else
- {
- //如果开启了分类打印,就要比对了
- std::map<string, bool> ids_map;
- std::vector<string> ids = CLewaimaiString::Split(fenlei_ids, ",");
- for (std::vector<string>::iterator it = ids.begin(); it != ids.end(); it++)
- {
- ids_map[(*it)] = true;
- }
- for (std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
- {
- if (ids_map.find((*it).m_type_id) != ids_map.end())
- {
- //如果当前商品的分类,在厨房打印机设置的分类里面,才加入
- cur_printer_use.push_back(*it);
- }
- }
- }
- if (cur_printer_use.size() == 0)
- {
- //如果这个厨房打印机没有商品可以打印(开启了商品分类打印可能出现),就直接跳过
- m_socket.close();
- continue;
- }
- if(fendan == "0")
- {
- POS_Reset();
- POS_TextOut("派工单", true, true, 1);
- POS_FeedLine();
- POS_FeedLine();
- string order_type = "#" + order.m_restaurant_number + " ";
- if(order.m_is_selftake == "1")
- {
- POS_TextOut(order_type + "到店自取订单", false, true, 1);
- POS_FeedLine();
- POS_FeedLine();
- }
- else
- {
- POS_TextOut(order_type + "外卖订单", false, true, 1);
- POS_FeedLine();
- POS_FeedLine();
- }
- //预设选项
- if(order.m_order_field.size() > 1)
- {
- for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
- {
- string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
- POS_TextOut(order_field, false, true, 0);
- POS_FeedLine();
- }
- }
- //准备开始打印商品详情
- string lines;
- if(guige == "58")
- {
- lines = "--------------------------------";
- }
- else
- {
- lines = "------------------------------------------------";
- }
- POS_TextOut(lines);
- POS_FeedLine();
- //商品标题
- if(guige == "58")
- {
- POS_TextOut("商品 单价 数量 金额", false, true);
- }
- else
- {
- POS_TextOut("商品 单价 数量 金额", false, true);
- }
- POS_FeedLine();
- //商品内容
- for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
- {
- std::string food_name = (*it).m_food_name;
- std::string food_price = (*it).m_item_price;
- std::string quantity = (*it).m_quantity;
- //计算总价
- double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
- std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
- food_name = CLewaimaiString::UTF8ToANSI(food_name);
- //先输出商品名字
- std::vector<string> m_names;
- if(guige == "58")
- {
- m_names = HandleFoodname(food_name, 1);
- }
- else
- {
- m_names = HandleFoodname(food_name, 2);
- }
- std::string firstLine = m_names[0];
- int nGuige;
- if(guige == "58")
- {
- nGuige = 1;
- }
- else
- {
- nGuige = 2;
- }
- std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
- std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
- std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
- std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
- POS_TextOut(firstLineShow, false, true, 0);
- POS_FeedLine();
- for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
- {
- POS_TextOut((*it), false, true, 0);
- POS_FeedLine();
- }
- }
- //判断是否有备注
- if(order.m_memo.length() > 0)
- {
- POS_FeedLine();
- string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
- POS_TextOut(address, false, true, 0);
- POS_FeedLine();
- }
- //结束商品详情打印
- POS_TextOut(lines);
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_CutPaper();
- //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
- POS_Reset();
- }
- else
- {
- POS_Reset();
- //分单模式下,每个商品打印一张单
- for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
- {
- POS_TextOut("派工单", true, true, 1);
- POS_FeedLine();
- POS_FeedLine();
- string order_type = "#" + order.m_restaurant_number + " ";
- if(order.m_is_selftake == "1")
- {
- POS_TextOut(order_type + "到店自取订单", false, true, 1);
- POS_FeedLine();
- POS_FeedLine();
- }
- else
- {
- POS_TextOut(order_type + "外卖订单", false, true, 1);
- POS_FeedLine();
- POS_FeedLine();
- }
- //预设选项
- if(order.m_order_field.size() > 1)
- {
- for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
- {
- string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
- POS_TextOut(order_field, false, true, 0);
- POS_FeedLine();
- }
- }
- //准备开始打印商品详情
- string lines;
- if(guige == "58")
- {
- lines = "--------------------------------";
- }
- else
- {
- lines = "------------------------------------------------";
- }
- POS_TextOut(lines);
- POS_FeedLine();
- //商品标题
- if(guige == "58")
- {
- POS_TextOut("商品 单价 数量 金额", false, true);
- }
- else
- {
- POS_TextOut("商品 单价 数量 金额", false, true);
- }
- POS_FeedLine();
- std::string food_name = (*it).m_food_name;
- std::string food_price = (*it).m_item_price;
- std::string quantity = (*it).m_quantity;
- //计算总价
- double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
- std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
- food_name = CLewaimaiString::UTF8ToANSI(food_name);
- //先输出商品名字
- std::vector<string> m_names;
- if(guige == "58")
- {
- m_names = HandleFoodname(food_name, 1);
- }
- else
- {
- m_names = HandleFoodname(food_name, 2);
- }
- std::string firstLine = m_names[0];
- std::string priceShow = HandleFoodItemPrice(food_price, 2);
- std::string quantityShow = HandleFoodQuantity(quantity, 2);
- std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
- std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
- POS_TextOut(firstLineShow, false, true, 0);
- POS_FeedLine();
- for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
- {
- POS_TextOut((*it), false, true, 0);
- POS_FeedLine();
- }
- //判断是否有备注
- if(order.m_memo.length() > 0)
- {
- POS_FeedLine();
- string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
- POS_TextOut(address, false, true, 0);
- POS_FeedLine();
- }
- //结束商品详情打印
- POS_TextOut(lines);
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_FeedLine();
- POS_CutPaper();
- }
- //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
- POS_Reset();
- }
- m_socket.close();
- }
- }
- std::vector<std::wstring> CPosPrinter::getUsbDevices()
- {
- return m_usb_devices;
- }
- int CPosPrinter::WriteData(string msg)
- {
- return WriteBuf((unsigned char*)msg.c_str(), msg.length());
- }
- /*
- *这个函数只负责把数据拼接到打印缓存里面,不负责实际的发送数据
- **/
- int CPosPrinter::WriteBuf(const unsigned char* buf, int len)
- {
- if(m_type == 1)
- {
- //USB、串口、并口,走这里
- for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
- {
- HANDLE hPort = (*it).hPort;
- //同步写数据
- DWORD dwWrite;
- BOOL ret = WriteFile(hPort, buf, len, &dwWrite, NULL);
- if (!ret)
- {
- LOG_INFO("WriteFile Failed!, hPort:"<<hPort<<", type:"<<(*it).type);
- }
- }
- return 1;
- }
- else
- {
- //网口走这里
- try
- {
- m_socket.write_some(boost::asio::buffer(buf, len));
- }
- catch(const std::exception& e)
- {
- LOG_INFO("网口打印机发送消息失败,错误信息:" << e.what());
- }
- return 1;
- }
- }
- /*
- *测试打印机是否连接
- **/
- bool CPosPrinter::PortTest(HANDLE hPort)
- {
- //这里的话目前USB、串口、并口用的都是同步读写模式,有的打印机(比如一些发票打印机)CreateFile会成功,但是WriteFile会一直阻塞卡着,如果检测的话,
- //在这里会一直卡住,因此这里的检测就不检测了,默认所有的都是连通的
- //因此,对于智能识别模式,可能会出现无法打印(因为被某些usb打印设备阻塞),这种情况就只能手动选择USB模式,然后重新启动软件
- return true;
- //标签打印机的查询状态指令
- char chInitCode[2] = { 0x1b, 0x40};
- DWORD dwWrite;
- BOOL rc = WriteFile(hPort, chInitCode, 2, &dwWrite, NULL);
- if(rc)
- {
- LOG_INFO("porttest success, handle:" << hPort);
- return true;
- }
- else
- {
- LOG_INFO("porttest fail, handle:" << hPort);
- return true;
- }
- return false;
- }
- int CPosPrinter::POS_Reset(void)
- {
- unsigned char s[2] = {0x1B, 0x40};
- WriteBuf(s, 2);
- return 0;
- }
- int CPosPrinter::POS_FeedLine(void)
- {
- unsigned char s[1] = {0x0A};
- WriteBuf(s, 1);
- return 0;
- }
- int CPosPrinter::POS_Feed(void)
- {
- unsigned char s[3] = { 0x1B, 0x4A, 0x00 };
- WriteBuf(s, 3);
- return 0;
- }
- int CPosPrinter::POS_SetMotionUnit(int x, int y)
- {
- string s;
- s = "\x1D\x50\xB4\xB4";
- WriteData(s);
- s = "\x1B\x53";
- WriteData(s);
- return 0;
- }
- int CPosPrinter::POS_SET_MOVE_X()
- {
- unsigned char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
- WriteBuf(s2, 6);
- return 0;
- }
- int CPosPrinter::POS_MOVE_X()
- {
- unsigned char s[1] = { 0x09 };
- WriteBuf(s, 1);
- return 0;
- }
- int CPosPrinter::POS_SET_ABS_X(int x, int y)
- {
- unsigned char cx = (unsigned char)(x);
- unsigned char cy = (unsigned char)(y);
- unsigned char s1[4] = { 0x1B, 0x24, cx, cy };
- WriteBuf(s1, 4);
- return 0;
- }
- int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
- {
- unsigned char cx = (unsigned char)(x);
- unsigned char cy = (unsigned char)(y);
- unsigned char s1[4] = { 0x1D, 0x57, cx, cy };
- WriteBuf(s1, 4);
- return 0;
- }
- /*
- *align_type:0 左对齐 1 居中对齐 2右对齐
- **/
- int CPosPrinter::POS_TextOut(string abc, bool is_double_width, bool is_double_height, int align_type)
- {
- if(is_double_width && is_double_height)
- {
- unsigned char s1[3] = { 0x1B, 0x21, 0x30 };
- WriteBuf(s1, 3);
- unsigned char s2[3] = { 0x1C, 0x21, 0x0c };
- WriteBuf(s2, 3);
- }
- else if(is_double_width && !is_double_height)
- {
- unsigned char s1[3] = { 0x1B, 0x21, 0x20 };
- WriteBuf(s1, 3);
- unsigned char s2[3] = { 0x1C, 0x21, 0x04 };
- WriteBuf(s2, 3);
- }
- else if(!is_double_width && is_double_height)
- {
- unsigned char s1[3] = { 0x1B, 0x21, 0x10 };
- WriteBuf(s1, 3);
- unsigned char s2[3] = { 0x1C, 0x21, 0x08 };
- WriteBuf(s2, 3);
- }
- else
- {
- unsigned char s1[3] = { 0x1B, 0x21, 0x00 };
- WriteBuf(s1, 3);
- unsigned char s2[3] = { 0x1C, 0x57, 0x00 };
- WriteBuf(s2, 3);
- }
- if(align_type == 0)
- {
- unsigned char s1[3] = { 0x1B, 0x61, 0x00 };
- WriteBuf(s1, 3);
- }
- else if(align_type == 1)
- {
- unsigned char s1[3] = { 0x1B, 0x61, 0x01 };
- WriteBuf(s1, 3);
- }
- else if(align_type == 2)
- {
- unsigned char s1[3] = { 0x1B, 0x61, 0x02 };
- WriteBuf(s1, 3);
- }
- else
- {
- }
- WriteData(abc);
- return 0;
- }
- /*
- *打印机切纸,直接切不走纸
- **/
- int CPosPrinter::POS_CutPaper()
- {
- unsigned char s[4] = { 0x1D, 0x56, 0x01};
- WriteBuf(s, 3);
- return 0;
- }
- /*
- *先转换成ANSI格式才能传进来
- **/
- void CPosPrinter::POS_OutQRCode(std::string dataString)
- {
- QRcode * qrCode = nullptr;
- qrCode = QRcode_encodeString(dataString.c_str(), 0, QR_ECLEVEL_H, QR_MODE_8, 1);
- if (qrCode == nullptr)
- {
- printf("error occur\n");
- return ;
- }
- unsigned int unWidth = qrCode->width;
- unsigned int unWidthAdjusted = unWidth * 8;
- unsigned int unHeightAjusted = unWidthAdjusted;
- unsigned char* realData = new unsigned char[unWidthAdjusted * unHeightAjusted];
- memset(realData, 0, unWidthAdjusted * unHeightAjusted);
- unsigned char* pSourceData = qrCode->data;
- for (unsigned int i = 0; i < unHeightAjusted; i++)
- {
- int realHeight = i / 8;
- for (unsigned int j = 0; j < unWidthAdjusted; j++)
- {
- int realWeight = j / 8;
- if (pSourceData[unWidth * realHeight + realWeight] & 1)
- {
- //表示这个像素要打印
- realData[unWidthAdjusted * i + j] = 1;
- }
- else
- {
- realData[unWidthAdjusted * i + j] = 0;
- }
- }
- }
- //设置行间距为0
- unsigned char data[3] = { 0x1B, 0x33, 0x00 };
- WriteBuf(data, 3);
- //设置图像居中对齐
- unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
- WriteBuf(data2, 3);
- //选择位图模式
- unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
- escBmp[3] = (unsigned char)(unWidthAdjusted % 256);
- escBmp[4] = (unsigned char)(unWidthAdjusted / 256);
- //换行符,每打印一行都要发送这个,不然有些尺寸下图片会乱掉
- unsigned char change_line_tag = '\n';
- //存储每行数据的结构
- unsigned char* dataTmp = new unsigned char[unWidthAdjusted * 3];
- //循环图片像素打印图片
- for (unsigned int i = 0; i < (unHeightAjusted / 24 + 1); i++)
- {
- //设置模式为位图模式
- WriteBuf(escBmp, 5);
- //重新开始打印一行,置零
- memset(dataTmp, 0, unWidthAdjusted * 3);
- //循环宽
- for (unsigned int j = 0; j < unWidthAdjusted; j++)
- {
- for (unsigned int k = 0; k < 24; k++)
- {
- //找到有像素的区域,也就是未超出位图高度的区域
- if (((i * 24) + k) < unHeightAjusted)
- {
- if (realData[unWidthAdjusted * (i * 24 + k) + j] == 1)
- {
- //如果本来是黑色的像素,那么就把对应的数据位设置为1
- dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
- }
- }
- }
- }
- WriteBuf(dataTmp, unWidthAdjusted * 3);
- //换行符,这里必须要发这个,不然图片会乱
- WriteBuf(&change_line_tag, 1);
- }
- WriteBuf(&change_line_tag, 1);
- //还原默认的行间距
- unsigned char data3[2] = { 0x1B, 0x32 };
- WriteBuf(data3, 2);
- delete[] dataTmp;
- QRcode_free(qrCode);
- delete[] realData;
- return ;
- }
- void CPosPrinter::POS_OutBmp(std::wstring ImagePath)
- {
- //初始化
- POS_Reset();
- //设置行间距为0
- unsigned char data[3] = { 0x1B, 0x33, 0x00 };
- WriteBuf(data, 3);
- //设置图像居中对齐
- unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
- WriteBuf(data2, 3);
- CBitmapHelper helper;
- helper.LoadImage(ImagePath.c_str());
- Bitmap* bmp = helper.getBmp();
- //先进行图片预处理
- unsigned int nWidth = bmp->GetWidth();
- unsigned int nHeight = bmp->GetHeight();
- //压缩尺寸
- if (nWidth > 380)
- {
- int newWidth = 380;
- int newHeight = (int)(nHeight / (nWidth / 380.0));
- helper.ScaleBitmap(newWidth, newHeight);;
- nWidth = newWidth;
- nHeight = newHeight;
- }
- //二值化
- helper.Image2Values();
- //获得最新的图像指针
- bmp = helper.getBmp();
- //选择位图模式
- unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
- escBmp[3] = (unsigned char)(nWidth % 256);
- escBmp[4] = (unsigned char)(nWidth / 256);
- Gdiplus::Color pixelColor;
- //换行符,每打印一行都要发送这个,不然有些尺寸下图片会乱掉
- unsigned char change_line_tag = '\n';
- //存储每行数据的结构
- unsigned char* dataTmp = new unsigned char[nWidth * 3];
- //循环图片像素打印图片
- for (unsigned int i = 0; i < (nHeight / 24 + 1); i++)
- {
- //设置模式为位图模式
- WriteBuf(escBmp, 5);
- //重新开始打印一行,置零
- memset(dataTmp, 0, nWidth * 3);
- //循环宽
- for (unsigned int j = 0; j < nWidth; j++)
- {
- for (unsigned int k = 0; k < 24; k++)
- {
- //找到有像素的区域,也就是未超出位图高度的区域
- if (((i * 24) + k) < nHeight)
- {
- bmp->GetPixel(j, (i * 24) + k, &pixelColor);
- unsigned char r = pixelColor.GetR();
- if (r == 0)
- {
- //如果本来是黑色的像素,那么就把对应的数据位设置为1
- dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
- }
- }
- }
- }
- WriteBuf(dataTmp, nWidth * 3);
- //换行符,这里必须要发这个,不然图片会乱
- WriteBuf(&change_line_tag, 1);
- }
- WriteBuf(&change_line_tag, 1);
- //还原默认的行间距
- unsigned char data3[2] = { 0x1B, 0x32 };
- WriteBuf(data3, 2);
- delete[] dataTmp;
- }
- void CPosPrinter::BIAOQIAN_Reset()
- {
- char endTag[3] = {0x0d, 0x0a, 0x00};
- std::string size = "SIZE 40 mm, 30 mm";
- size += endTag;
- std::string gap = "GAP 2 mm,0 mm";
- gap += endTag;
- std::string speed = "SPEED 4.0";
- speed += endTag;
- std::string density = "DENSITY 12";
- density += endTag;
- std::string REFERENCE = "REFERENCE 0,0";
- REFERENCE += endTag;
- std::string PEER = "SET PEEL OFF";
- PEER += endTag;
- std::string TEAR = "SET TEAR ON";
- TEAR += endTag;
- std::string cls = "CLS";
- cls += endTag;
- std::string data = size + gap + speed + density + REFERENCE + PEER + TEAR + cls;
- WriteData(data);
- }
- /*
- *前进一张纸
- **/
- void CPosPrinter::BIAOQIAN_FORMFEED()
- {
- char endTag[3] = { 0x0d, 0x0a, 0x00 };
- std::string formfeed = "FORMFEED";
- formfeed += endTag;
- WriteData(formfeed);
- }
- void CPosPrinter::BIAOQIAN_TEXTOUT(std::string content, int x, int y, int x_multiplication, int y_multiplication)
- {
- char endTag[3] = { 0x0d, 0x0a, 0x00 };
- string text = "TEXT ";
- text += to_string(x) + "," + to_string(y) + ",";
- text += "\"TSS24.BF2\",";
- text += to_string(0) + ",";
- text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
- text += "\"" + content + "\"";
- text += endTag;
- WriteData(text);
- }
- std::string CPosPrinter::BIAOQIAN_TEXTGet(std::string content, int x, int y, int x_multiplication, int y_multiplication)
- {
- char endTag[3] = { 0x0d, 0x0a, 0x00 };
- string text = "TEXT ";
- text += to_string(x) + "," + to_string(y) + ",";
- text += "\"TSS24.BF2\",";
- text += to_string(0) + ",";
- text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
- text += "\"" + content + "\"";
- text += endTag;
- return text;
- }
- void CPosPrinter::BIAOQIAN_PRINT()
- {
- char endTag[3] = { 0x0d, 0x0a, 0x00 };
- std::string print = "PRINT 1";
- print += endTag;
- WriteData(print);
- }
- void CPosPrinter::BIAOQIAN_CLS()
- {
- char endTag[3] = { 0x0d, 0x0a, 0x00 };
- std::string cls = "CLS";
- cls += endTag;
- WriteData(cls);
- }
- void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
- {
- nHanzi = 0;
- nZimu = 0;
- const char* buffer = s.c_str();
- while(*buffer != '\0')
- {
- if(!(*buffer >= 0 && *buffer <= 127))
- {
- //汉字
- buffer++;
- buffer++;
- nHanzi++;
- }
- else
- {
- //字母
- buffer++;
- nZimu++;
- }
- }
- }
- /*
- *规格 1:58mm 2:80mm
- **/
- std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
- {
- std::vector<std::string> newnameArray;
- int nHanzi, nZimu;
- CalWord(oldname, nHanzi, nZimu);
- int nWidth = nHanzi * 2 + nZimu;
- int maxWidth;
- if(guige == 1)
- {
- maxWidth = 15;
- }
- else
- {
- maxWidth = 28;
- }
- if(nWidth <= maxWidth)
- {
- //对于nWidth补空格
- for(int i = 0; i < maxWidth - nWidth; i++)
- {
- oldname += " ";
- }
- newnameArray.push_back(oldname);
- return newnameArray;
- }
- //宽度大于15的情况,如果超过了,就要进行换行截取
- const char* s = oldname.c_str();
- int nTmp = 0;
- int nTotal = 0;
- while(*s != '\0')
- {
- if(!(*s >= 0 && *s <= 127))
- {
- //汉字的情况
- s++;
- nTmp++;
- nTotal++;
- s++;
- nTmp++;
- nTotal++;
- }
- else
- {
- //字母的情况
- s++;
- nTmp++;
- nTotal++;
- }
- if(nTmp == maxWidth)
- {
- //这里开始要换行了
- string newnameItem;
- newnameItem.assign(oldname, nTotal - nTmp, nTmp);
- newnameArray.push_back(newnameItem);
- nTmp = 0;
- }
- else if(nTmp == maxWidth - 1)
- {
- //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
- if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
- {
- //下一个是中文,也要换行了,补齐一个空格
- string newnameItem;
- newnameItem.assign(oldname, nTotal - nTmp, nTmp);
- newnameItem += " ";
- newnameArray.push_back(newnameItem);
- nTmp = 0;
- }
- }
- }
- if(nTmp > 0)
- {
- //处理分隔后的最后一行
- string newnameItem;
- newnameItem.assign(oldname, nTotal - nTmp, nTmp);
- for(int i = 0; i < maxWidth - nTmp; i++)
- {
- newnameItem += " ";
- }
- newnameArray.push_back(newnameItem);
- }
- return newnameArray;
- }
- std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
- {
- double price = atof(oldprice.c_str());
- if(price < 10.00)
- {
- //单位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else if(price > 9.99 && price < 100.00)
- {
- //双位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else if(price > 99.99 && price < 1000.00)
- {
- //三位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else
- {
- //四位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- }
- std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
- {
- int n = atoi(oldquantity.c_str());
- if(n < 10)
- {
- if(guige == 1)
- {
- return " " + oldquantity;
- }
- else
- {
- return " " + oldquantity;
- }
- }
- else if(n >= 10 && n <= 99)
- {
- if(guige == 1)
- {
- return " " + oldquantity;
- }
- else
- {
- return " " + oldquantity;
- }
- }
- else
- {
- if(guige == 1)
- {
- return "" + oldquantity;
- }
- else
- {
- return " " + oldquantity;
- }
- }
- }
- std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
- {
- double price = atof(oldprice.c_str());
- if(price < 10.00)
- {
- //单位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else if(price > 9.99 && price < 100.00)
- {
- //双位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else if(price > 99.99 && price < 1000.00)
- {
- //三位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else
- {
- //四位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- }
- std::vector<std::string> CPosPrinter::HandleBiaoqianFoodname(std::string oldname)
- {
- std::vector<std::string> newnameArray;
- int nHanzi, nZimu;
- CalWord(oldname, nHanzi, nZimu);
- int nWidth = nHanzi * 2 + nZimu;
- //40 *30mmm的标签,宽度最大24
- int maxWidth = 24;
- if(nWidth <= maxWidth)
- {
- //对于nWidth补空格
- for(int i = 0; i < maxWidth - nWidth; i++)
- {
- oldname += " ";
- }
- newnameArray.push_back(oldname);
- return newnameArray;
- }
- //宽度大于15的情况,如果超过了,就要进行换行截取
- const char* s = oldname.c_str();
- int nTmp = 0;
- int nTotal = 0;
- while(*s != '\0')
- {
- if(!(*s >= 0 && *s <= 127))
- {
- //汉字的情况
- s++;
- nTmp++;
- nTotal++;
- s++;
- nTmp++;
- nTotal++;
- }
- else
- {
- //字母的情况
- s++;
- nTmp++;
- nTotal++;
- }
- if(nTmp == maxWidth)
- {
- //这里开始要换行了
- string newnameItem;
- newnameItem.assign(oldname, nTotal - nTmp, nTmp);
- newnameArray.push_back(newnameItem);
- nTmp = 0;
- }
- else if(nTmp == maxWidth - 1)
- {
- //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
- if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
- {
- //下一个是中文,也要换行了,补齐一个空格
- string newnameItem;
- newnameItem.assign(oldname, nTotal - nTmp, nTmp);
- newnameItem += " ";
- newnameArray.push_back(newnameItem);
- nTmp = 0;
- }
- }
- }
- if(nTmp > 0)
- {
- //处理分隔后的最后一行
- string newnameItem;
- newnameItem.assign(oldname, nTotal - nTmp, nTmp);
- for(int i = 0; i < maxWidth - nTmp; i++)
- {
- newnameItem += " ";
- }
- newnameArray.push_back(newnameItem);
- }
- return newnameArray;
- }
|