CPosPrinter.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  1. #include "../pch/pch.h"
  2. #include "CPosPrinter.h"
  3. #include "../tool/CComHelper.h"
  4. #include "../helper/CBitmapHelper.h"
  5. #include <qrencode/qrencode.h>
  6. CPosPrinter::CPosPrinter(): m_socket(m_io)
  7. {
  8. }
  9. CPosPrinter::~CPosPrinter()
  10. {
  11. }
  12. /*
  13. *usbType是1表示只查找收银小票打印机,会排除已知的标签打印机
  14. *usbType为2表示所有usb打印机都查找,包含标签与非标签
  15. **/
  16. void CPosPrinter::InitUsb(int usbType)
  17. {
  18. //设置中文字符
  19. setlocale(LC_CTYPE, "chs");
  20. //取设备路径
  21. int nDevice = GetDevicePath((LPGUID)&USB_GUID, usbType);
  22. LOG_INFO("可用的USB打印机数量:" << nDevice);
  23. //添加usb端口
  24. int i = 0;
  25. while(i < nDevice)
  26. {
  27. std::string setting_biaoqian_printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
  28. std::wstring ws_setting_biaoqian_printer_usb = CLewaimaiString::UTF8ToUnicode(setting_biaoqian_printer_usb);
  29. if (CSetting::GetParam("setting_is_new_waimai_biaoqian_printer") == "1" && ws_setting_biaoqian_printer_usb == m_usb_devices[i])
  30. {
  31. //碰到了一个标签打印机
  32. i++;
  33. continue;
  34. }
  35. LOG_INFO("准备打开端口 Port = " << m_usb_devices[i].c_str());
  36. HANDLE hPort = CreateFile(m_usb_devices[i].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  37. if(hPort == INVALID_HANDLE_VALUE)
  38. {
  39. LOG_INFO("打开USB端口失败, Port = " << m_usb_devices[i].c_str());
  40. // 打开端口失败
  41. DWORD error = GetLastError();
  42. if(error == 2)
  43. {
  44. //没有指定的文件
  45. LOG_INFO("没有找对对应的usb端口");
  46. }
  47. else if(error == 5)
  48. {
  49. LOG_INFO("usb端口被占用!");
  50. }
  51. continue;
  52. }
  53. LOG_INFO("打开usb端口成功,准备进行打印机检测! hPort:" << hPort);
  54. if(PortTest(hPort) == true)
  55. {
  56. //端口测试连通,保存起来
  57. PrinterHandle newHandle;
  58. newHandle.hPort = hPort;
  59. newHandle.type = 1;
  60. m_hPorts.push_back(newHandle);
  61. }
  62. else
  63. {
  64. //端口打印机没有连接,那么就直接关闭掉并口,避免占用
  65. CloseHandle(hPort);
  66. }
  67. i++;
  68. }
  69. }
  70. bool CPosPrinter::InitOneUsb(wstring usb_path)
  71. {
  72. if (usb_path == L"")
  73. {
  74. return false;
  75. }
  76. HANDLE hPort = CreateFile(usb_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  77. if(hPort == INVALID_HANDLE_VALUE)
  78. {
  79. LOG_INFO("打开USB端口失败, Port = " << usb_path.c_str());
  80. // 打开端口失败
  81. DWORD error = GetLastError();
  82. if(error == 2)
  83. {
  84. //没有指定的文件
  85. LOG_INFO("没有找对对应的usb端口");
  86. }
  87. else if(error == 5)
  88. {
  89. LOG_INFO("usb端口被占用!");
  90. }
  91. return false;
  92. }
  93. LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
  94. if(PortTest(hPort) == true)
  95. {
  96. //端口测试连通,保存起来
  97. PrinterHandle newHandle;
  98. newHandle.hPort = hPort;
  99. newHandle.type = 1;
  100. m_hPorts.push_back(newHandle);
  101. }
  102. else
  103. {
  104. //端口打印机没有连接,那么就直接关闭掉并口,避免占用
  105. CloseHandle(hPort);
  106. return false;
  107. }
  108. return true;
  109. }
  110. bool CPosPrinter::InitBingkou()
  111. {
  112. std::wstring LptStr = L"lpt1";
  113. HANDLE hPort = CreateFile(LptStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  114. if(hPort == INVALID_HANDLE_VALUE)
  115. {
  116. LOG_INFO("打开并口失败, Port = " << LptStr.c_str());
  117. DWORD error = GetLastError();
  118. if(error == 2)
  119. {
  120. //没有指定的文件
  121. LOG_INFO("本机器没有并口!");
  122. }
  123. else if(error == 5)
  124. {
  125. LOG_INFO("并口被占用!");
  126. }
  127. return false;
  128. }
  129. else
  130. {
  131. //这个表示并口可以使用
  132. LOG_INFO("找到并口,准备进行打印机检测! hPort:" << hPort);
  133. if(PortTest(hPort) == true)
  134. {
  135. //并口测试连通,保存起来
  136. PrinterHandle newHandle;
  137. newHandle.hPort = hPort;
  138. newHandle.type = 2;
  139. m_hPorts.push_back(newHandle);
  140. }
  141. else
  142. {
  143. //并口打印机没有连接,那么就直接关闭掉并口,避免占用
  144. CloseHandle(hPort);
  145. return false;
  146. }
  147. }
  148. return true;
  149. }
  150. void CPosPrinter::InitCom()
  151. {
  152. CComHelper helper;
  153. std::vector<std::wstring> comVector = helper.getComPort();
  154. for(std::vector<std::wstring>::iterator it = comVector.begin(); it != comVector.end(); it++)
  155. {
  156. std::wstring com2Str = *it;
  157. HANDLE hPort = CreateFile(com2Str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  158. if(hPort == INVALID_HANDLE_VALUE)
  159. {
  160. DWORD error = GetLastError();
  161. if(error == 2)
  162. {
  163. //没有指定的文件
  164. LOG_INFO("没有找到对应的串口 " << com2Str.c_str());
  165. }
  166. else if(error == 5)
  167. {
  168. LOG_INFO("串口被占用 " << com2Str.c_str());
  169. }
  170. }
  171. else
  172. {
  173. LOG_INFO(com2Str.c_str() << " success, handle:" << hPort);
  174. //扎到了可用的串口,设置串口波特率
  175. DCB dcb;
  176. dcb.DCBlength = sizeof(dcb);
  177. GetCommState(hPort, &dcb);
  178. //佳博的串口打印机,默认是这个波特率
  179. dcb.BaudRate = 19200;
  180. if(!SetCommState(hPort, &dcb))
  181. {
  182. LOG_INFO("set baudRate failed!");
  183. CloseHandle(hPort);
  184. continue;
  185. }
  186. //设定通讯端口超时参数
  187. COMMTIMEOUTS tmouts;
  188. tmouts.ReadIntervalTimeout = 100;
  189. tmouts.ReadTotalTimeoutMultiplier = 100;
  190. tmouts.ReadTotalTimeoutConstant = 100;
  191. tmouts.WriteTotalTimeoutConstant = 100;
  192. tmouts.WriteTotalTimeoutMultiplier = 100;
  193. SetCommTimeouts(hPort, &tmouts);
  194. //设置端口缓冲
  195. SetupComm(hPort, 1024, 1024);
  196. //清除通讯端口缓存
  197. PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
  198. //保存端口
  199. PrinterHandle newHandle;
  200. newHandle.hPort = hPort;
  201. newHandle.type = 3;
  202. m_hPorts.push_back(newHandle);
  203. }
  204. }
  205. }
  206. /*
  207. *找到所有可用的小票打印机,这里只初始化USB,不包含并口、串口、网口
  208. *智能识别模式不再自动处理串口,串口的需要人工去选择类型
  209. **/
  210. void CPosPrinter::InitShouyin()
  211. {
  212. //开始添加usb
  213. InitUsb();
  214. }
  215. /*
  216. *获取CreateFile的USB端口号
  217. **/
  218. int CPosPrinter::GetDevicePath(LPGUID lpGuid, int usbType)
  219. {
  220. HDEVINFO hDevInfoSet;
  221. SP_DEVINFO_DATA spDevInfoData;
  222. SP_DEVICE_INTERFACE_DATA ifData;
  223. PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
  224. int nCount;
  225. int nTotle;
  226. BOOL bResult;
  227. //这2个字符串,用于根据usb的名字对比是否为打印机设备
  228. wstring strUSBPrint = TEXT("USB 打印支持");
  229. //xp上是英文
  230. wstring strUSBPrint_EN = L"USB Printing Support";
  231. // 取得一个该GUID相关的设备信息集句柄
  232. hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
  233. NULL, // 无关键字
  234. NULL, // 不指定父窗口句柄
  235. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
  236. // 失败...
  237. if(hDevInfoSet == INVALID_HANDLE_VALUE)
  238. {
  239. LOG_INFO("SetupDiGetClassDevs failed \r\n");
  240. return 0;
  241. }
  242. // 申请设备接口数据空间
  243. pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
  244. pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  245. nTotle = -1;
  246. nCount = 0;
  247. bResult = TRUE;
  248. // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
  249. while(bResult)
  250. {
  251. nTotle++;
  252. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  253. // 枚举符合该GUID的设备接口
  254. bResult = ::SetupDiEnumDeviceInfo(
  255. hDevInfoSet, // 设备信息集句柄
  256. (ULONG)nTotle, // 设备信息集里的设备序号
  257. &spDevInfoData); // 设备接口信息
  258. if(bResult)
  259. {
  260. DWORD DataT;
  261. TCHAR buf[MAX_PATH] = { 0 };
  262. DWORD nSize = 0;
  263. // get Friendly Name or Device Description
  264. if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  265. SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  266. {
  267. }
  268. else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  269. SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  270. {
  271. }
  272. else
  273. {
  274. lstrcpy(buf, _T("Unknown"));
  275. }
  276. wstring ws_buf = buf;
  277. //是否是要找的设备类型
  278. if(ws_buf != strUSBPrint && ws_buf != strUSBPrint_EN)
  279. {
  280. LOG_INFO("not good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  281. continue;
  282. }
  283. LOG_INFO("good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  284. ifData.cbSize = sizeof(ifData);
  285. // 枚舉符合該GUID的設備接口
  286. bResult = ::SetupDiEnumDeviceInterfaces(
  287. hDevInfoSet, // 設備信息集句柄
  288. NULL, // 不需額外的設備描述
  289. lpGuid, // GUID
  290. (ULONG)nTotle, // 設備信息集里的設備序號
  291. &ifData); // 設備接口信息
  292. if(bResult)
  293. {
  294. // 取得该设备接口的细节(设备路径)
  295. bResult = SetupDiGetInterfaceDeviceDetail(
  296. hDevInfoSet, // 设备信息集句柄
  297. &ifData, // 设备接口信息
  298. pDetail, // 设备接口细节(设备路径)
  299. INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
  300. NULL, // 不需计算输出缓冲区大小(直接用设定值)
  301. NULL); // 不需额外的设备描述
  302. if(bResult)
  303. {
  304. wstring DevicePath = pDetail->DevicePath;
  305. wstring vid = DevicePath.substr(DevicePath.find(_T("vid_"), 0) + 4, 4);
  306. wstring pid = DevicePath.substr(DevicePath.find(_T("pid_"), 0) + 4, 4);
  307. LOG_INFO("Vid:" << vid.c_str() << ", Pid:" << pid.c_str());
  308. if(usbType == 1 && GetPrinterType(vid, pid) == 2)
  309. {
  310. //标签打印机,暂时不处理打印
  311. LOG_INFO("标签打印机,暂时不打印!");
  312. continue;
  313. }
  314. // 复制设备路径到输出缓冲区
  315. m_usb_devices.push_back(DevicePath);
  316. // 调整计数值
  317. nCount++;
  318. LOG_INFO("Cnt = " << nCount << ",pDetail->DevicePath =" << pDetail->DevicePath);
  319. }
  320. }
  321. }
  322. else
  323. {
  324. //LOG_INFO("SetupDiEnumDeviceInfo FAILED!@");
  325. }
  326. }
  327. // 释放设备接口数据空间
  328. ::GlobalFree(pDetail);
  329. // 关闭设备信息集句柄
  330. ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
  331. return nCount;
  332. }
  333. /*
  334. *返回打印机的类型, 1:普通热敏打印机 2:标签打印机 3:发票打印机
  335. *这里因为不同厂家对vid pid定义不一样,有时候会出现佳博的标签打印机的vid和pid,在别的品牌上变成了热敏打印机,这里如果排除掉别的品牌的热敏打印机就无法打印了,所以这里不能轻易排除
  336. **/
  337. int CPosPrinter::GetPrinterType(wstring vid, wstring pid)
  338. {
  339. if(vid == L"6868" && pid == L"0500")
  340. {
  341. //佳博标签打印机
  342. return 2;
  343. }
  344. else if (vid == L"XXX" && pid == L"XXX")
  345. {
  346. //发票打印机
  347. return 3;
  348. }
  349. return 1;
  350. }
  351. /*
  352. *打印收银小票
  353. **/
  354. void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
  355. {
  356. LOG_INFO("_______________________________开始打印收银小票,订单编号:"<<order.m_order_num.c_str()<<"_________________________");
  357. //设置模式,后面输出数据不会错
  358. m_type = 1;
  359. std::string printer_leixing = CSetting::GetParam("setting_printer_leixing");
  360. if(printer_leixing == "auto")
  361. {
  362. InitShouyin();
  363. }
  364. else if(printer_leixing == "usb")
  365. {
  366. std::string printer_usb = CSetting::GetParam("setting_printer_usb");
  367. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  368. bool ret = InitOneUsb(ws_printer_usb);
  369. if (ret == false)
  370. {
  371. return;
  372. }
  373. }
  374. else if(printer_leixing == "bingkou")
  375. {
  376. bool ret = InitBingkou();
  377. if (ret == false)
  378. {
  379. return;
  380. }
  381. }
  382. else if(printer_leixing == "chuankou")
  383. {
  384. InitCom();
  385. }
  386. else if(printer_leixing == "wangkou")
  387. {
  388. m_type = 2;
  389. std::string wangkou_ip = CSetting::GetParam("setting_printer_wangkou_ip");
  390. //初始化连接
  391. try
  392. {
  393. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(wangkou_ip.c_str()), 9100);
  394. m_socket.connect(ep);
  395. }
  396. catch(std::exception& e)
  397. {
  398. std::string err = e.what();
  399. LOG_INFO("网口打印机连接失败,IP地址:" << wangkou_ip.c_str() << ",错误信息:" << err.c_str());
  400. MessageBoxW(NULL, (L"厨房网口打印机连接失败,IP地址:" + CLewaimaiString::UTF8ToUnicode(wangkou_ip)).c_str(), L"打印机连接失败", MB_OK);
  401. return;
  402. }
  403. }
  404. //读取当前收银打印机的设置
  405. std::string guige = CSetting::GetParam("setting_printer_guige");
  406. std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
  407. int n_lianshu = atoi(lianshu.c_str());
  408. //正式开始打印
  409. for(int i = 0; i < n_lianshu; i++)
  410. {
  411. POS_Reset();
  412. //打印店铺名字
  413. string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
  414. POS_TextOut(shop_name, true, true, 1);
  415. POS_FeedLine();
  416. POS_FeedLine();
  417. //打印小票自定义名称
  418. string printer_tip_name = CLewaimaiString::UTF8ToANSI(order.m_printer_tip_name);
  419. if (printer_tip_name.length() > 0)
  420. {
  421. POS_TextOut(printer_tip_name, false, true, 1);
  422. POS_FeedLine();
  423. POS_FeedLine();
  424. }
  425. //打印预约、即时配送的提醒
  426. /*
  427. bool setting_printer_peisongshijian_big = false;
  428. if (CSetting::GetParam("setting_printer_peisongshijian_big") == "1")
  429. {
  430. setting_printer_peisongshijian_big = true;
  431. }
  432. }*/
  433. if (order.m_is_selftake == "1")
  434. {
  435. string date = "【自取单】" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  436. POS_TextOut(date, false, true);
  437. POS_TextOut("取货", false, false);
  438. POS_FeedLine();
  439. }
  440. else
  441. {
  442. if (order.m_is_book == "1")
  443. {
  444. //预约单
  445. string date = "【预约单】" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  446. POS_TextOut(date, false, true);
  447. POS_TextOut("送达", false, false);
  448. POS_FeedLine();
  449. }
  450. else
  451. {
  452. //非预约单
  453. string date = "【即时单】立即送达";
  454. POS_TextOut(date, false, true, 1);
  455. POS_FeedLine();
  456. }
  457. }
  458. POS_FeedLine();
  459. //打印取货方式
  460. /*
  461. bool setting_printer_quhuo_big = false;
  462. if(CSetting::GetParam("setting_printer_quhuo_big") == "1")
  463. {
  464. setting_printer_quhuo_big = true;
  465. }
  466. if(order.m_is_selftake == "1")
  467. {
  468. POS_TextOut("到店自取订单", false, setting_printer_quhuo_big, 1);
  469. POS_FeedLine();
  470. POS_FeedLine();
  471. }
  472. else
  473. {
  474. POS_TextOut("外卖订单", false, setting_printer_quhuo_big, 1);
  475. POS_FeedLine();
  476. }
  477. POS_FeedLine();
  478. */
  479. //订单来源
  480. string from_type = "订单来源:" + CLewaimaiString::UTF8ToANSI(order.m_from_type);
  481. POS_TextOut(from_type);
  482. POS_FeedLine();
  483. //订单号
  484. bool setting_printer_dingdanhao_big = false;
  485. if(CSetting::GetParam("setting_printer_dingdanhao_big") == "1")
  486. {
  487. setting_printer_dingdanhao_big = true;
  488. }
  489. string order_num = "订 单 号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  490. POS_TextOut(order_num, false, setting_printer_dingdanhao_big);
  491. POS_FeedLine();
  492. //下单时间
  493. bool setting_printer_xiadanshijian_big = false;
  494. if(CSetting::GetParam("setting_printer_xiadanshijian_big") == "1")
  495. {
  496. setting_printer_xiadanshijian_big = true;
  497. }
  498. string order_date = "下单时间:" + CLewaimaiString::UTF8ToANSI(order.m_order_date);
  499. POS_TextOut(order_date, false, setting_printer_xiadanshijian_big);
  500. POS_FeedLine();
  501. //姓名
  502. bool setting_printer_name_big = false;
  503. if(CSetting::GetParam("setting_printer_name_big") == "1")
  504. {
  505. setting_printer_name_big = true;
  506. }
  507. if(order.m_is_selftake != "1")
  508. {
  509. string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
  510. //把下单次数也一起打印
  511. if (order.m_showordernum == "1")
  512. {
  513. name += "(第 " + CLewaimaiString::UTF8ToANSI(order.m_customer_order_total) + " 次下单)";
  514. }
  515. POS_TextOut(name, false, setting_printer_name_big);
  516. POS_FeedLine();
  517. }
  518. //电话
  519. bool setting_printer_phone_big = false;
  520. if(CSetting::GetParam("setting_printer_phone_big") == "1")
  521. {
  522. setting_printer_phone_big = true;
  523. }
  524. //看看电话是否要脱敏
  525. if (order.m_show_whole_phone == "0")
  526. {
  527. order.m_phone.replace(3, 4, 4, '*');
  528. }
  529. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  530. POS_TextOut(phone, false, setting_printer_phone_big);
  531. POS_FeedLine();
  532. //地址
  533. bool setting_printer_address_big = false;
  534. if(CSetting::GetParam("setting_printer_address_big") == "1")
  535. {
  536. setting_printer_address_big = true;
  537. }
  538. if(order.m_is_selftake != "1")
  539. {
  540. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  541. POS_TextOut(address, false, setting_printer_address_big);
  542. POS_FeedLine();
  543. }
  544. //预设选项
  545. if(order.m_order_field.size() > 1)
  546. {
  547. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  548. {
  549. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  550. POS_TextOut(order_field, false, false, 0);
  551. POS_FeedLine();
  552. }
  553. }
  554. //准备开始打印商品详情
  555. string lines;
  556. if(guige == "58")
  557. {
  558. lines = "--------------------------------";
  559. }
  560. else
  561. {
  562. lines = "------------------------------------------------";
  563. }
  564. POS_TextOut(lines);
  565. POS_FeedLine();
  566. //商品标题
  567. bool setting_printer_shangpin_big = false;
  568. if(CSetting::GetParam("setting_printer_shangpin_big") == "1")
  569. {
  570. setting_printer_shangpin_big = true;
  571. }
  572. if(guige == "58")
  573. {
  574. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  575. }
  576. else
  577. {
  578. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  579. }
  580. POS_FeedLine();
  581. if (order.m_manzeng_name.length() > 0)
  582. {
  583. CWaimaiOrderItem manzengItem;
  584. manzengItem.m_food_name = CLewaimaiString::ANSIToUTF8("【满赠】" + CLewaimaiString::UTF8ToANSI(order.m_manzeng_name));
  585. order.m_order_items.push_back(manzengItem);
  586. }
  587. //商品内容
  588. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  589. {
  590. std::string food_name = (*it).m_food_name;
  591. std::string food_price = (*it).m_item_price;
  592. std::string quantity = (*it).m_quantity;
  593. //计算总价
  594. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  595. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  596. if (food_total_price == "0.00")
  597. {
  598. //这里是特殊处理满赠的展示
  599. food_total_price = "";
  600. }
  601. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  602. //先输出商品名字
  603. std::vector<string> m_names;
  604. if(guige == "58")
  605. {
  606. m_names = HandleFoodname(food_name, 1);
  607. }
  608. else
  609. {
  610. m_names = HandleFoodname(food_name, 2);
  611. }
  612. std::string firstLine = m_names[0];
  613. int nGuige;
  614. if(guige == "58")
  615. {
  616. nGuige = 1;
  617. }
  618. else
  619. {
  620. nGuige = 2;
  621. }
  622. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  623. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  624. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  625. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  626. POS_TextOut(firstLineShow, false, setting_printer_shangpin_big);
  627. POS_FeedLine();
  628. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  629. {
  630. POS_TextOut((*it), false, setting_printer_shangpin_big);
  631. POS_FeedLine();
  632. }
  633. }
  634. //判断是否有备注
  635. if(order.m_memo.length() > 0)
  636. {
  637. bool setting_printer_memo_big = false;
  638. if(CSetting::GetParam("setting_printer_memo_big") == "1")
  639. {
  640. setting_printer_memo_big = true;
  641. }
  642. POS_FeedLine();
  643. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  644. POS_TextOut(address, false, setting_printer_memo_big);
  645. POS_FeedLine();
  646. }
  647. //结束商品详情打印
  648. POS_TextOut(lines);
  649. POS_FeedLine();
  650. //开始打印其他的费用
  651. bool is_other_money = false;
  652. bool setting_printer_price_big = false;
  653. if(CSetting::GetParam("setting_printer_price_big") == "1")
  654. {
  655. setting_printer_price_big = true;
  656. }
  657. if(order.m_delivery > 0)
  658. {
  659. string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
  660. POS_TextOut(delivery, false, setting_printer_price_big);
  661. POS_FeedLine();
  662. is_other_money = true;
  663. }
  664. if(order.m_is_dabao == "1")
  665. {
  666. string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
  667. POS_TextOut(dabao, false, setting_printer_price_big);
  668. POS_FeedLine();
  669. is_other_money = true;
  670. }
  671. //开始显示增值服务费
  672. if(order.m_addservie.size() > 0)
  673. {
  674. for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
  675. {
  676. string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
  677. POS_TextOut(addservice, false, setting_printer_price_big);
  678. POS_FeedLine();
  679. is_other_money = true;
  680. }
  681. is_other_money = true;
  682. }
  683. if(order.m_discount < 10)
  684. {
  685. string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
  686. POS_TextOut(discount, false, setting_printer_price_big);
  687. POS_FeedLine();
  688. is_other_money = true;
  689. }
  690. if(order.m_is_member_discount == "1")
  691. {
  692. string member_discount = "会员优惠:-" + order.m_member_discount;
  693. POS_TextOut(member_discount, false, setting_printer_price_big);
  694. POS_FeedLine();
  695. is_other_money = true;
  696. }
  697. if(order.m_is_firstcut == "1")
  698. {
  699. string firstcut = "首单优惠:-" + order.m_firstcut_value;
  700. POS_TextOut(firstcut, false, setting_printer_price_big);
  701. POS_FeedLine();
  702. is_other_money = true;
  703. }
  704. //满减
  705. string::size_type position = order.m_promotion.find(":");
  706. if(position != order.m_promotion.npos)
  707. {
  708. string promotion_value;
  709. promotion_value.assign(order.m_promotion, position + 1);
  710. string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
  711. POS_TextOut(promotion, false, setting_printer_price_big);
  712. POS_FeedLine();
  713. is_other_money = true;
  714. }
  715. if(order.m_coupon > 0)
  716. {
  717. string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
  718. POS_TextOut(coupon, false, setting_printer_price_big);
  719. POS_FeedLine();
  720. is_other_money = true;
  721. }
  722. if(is_other_money)
  723. {
  724. POS_TextOut(lines);
  725. POS_FeedLine();
  726. }
  727. //最后显示总价
  728. POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, setting_printer_price_big, 2);
  729. POS_FeedLine();
  730. //显示付款方式
  731. bool setting_printer_pay_big = false;
  732. if(CSetting::GetParam("setting_printer_pay_big") == "1")
  733. {
  734. setting_printer_pay_big = true;
  735. }
  736. POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, setting_printer_pay_big, 2);
  737. POS_FeedLine();
  738. POS_FeedLine();;
  739. POS_FeedLine();
  740. //准备开始打印二维码
  741. std::string order_no = order.m_order_no;
  742. order_no = CLewaimaiString::UTF8ToANSI(order_no);
  743. if (order.m_show_qiangdan_qrcode == "1")
  744. {
  745. POS_OutQRCode(order_no);
  746. POS_FeedLine();
  747. POS_FeedLine();
  748. POS_TextOut("请使用配送员app扫码绑定订单", false, false, 1);
  749. }
  750. //走纸几行再切
  751. POS_FeedLine();
  752. POS_FeedLine();
  753. POS_FeedLine();
  754. POS_FeedLine();
  755. POS_FeedLine();
  756. POS_CutPaper();
  757. }
  758. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  759. POS_Reset();
  760. //关闭设备
  761. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  762. {
  763. CloseHandle((*it).hPort);
  764. }
  765. if (m_type == 2)
  766. {
  767. m_socket.close();
  768. }
  769. LOG_INFO("_______________________________结束打印收银小票,订单编号:" << order.m_order_num.c_str() << "_________________________");
  770. LOG_INFO(" ");
  771. }
  772. void CPosPrinter::PrintWaimaiOrderBiaoqian(CWaimaiOrder& order)
  773. {
  774. //设置模式,后面输出数据不会错
  775. m_type = 1;
  776. std::string printer_leixing = CSetting::GetParam("setting_biaoqian_printer_leixing");
  777. if (printer_leixing == "usb")
  778. {
  779. std::string printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
  780. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  781. bool ret = InitOneUsb(ws_printer_usb);
  782. if (ret == false)
  783. {
  784. return;
  785. }
  786. }
  787. else if (printer_leixing == "wangkou")
  788. {
  789. m_type = 2;
  790. std::string wangkou_ip = CSetting::GetParam("setting_biaoqian_printer_wangkou_ip");
  791. //初始化连接
  792. try
  793. {
  794. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(wangkou_ip.c_str()), 9100);
  795. m_socket.connect(ep);
  796. }
  797. catch (std::exception& e)
  798. {
  799. std::string err = e.what();
  800. LOG_INFO("网口打印机连接失败,IP地址:" << wangkou_ip.c_str() << ",错误信息:" << err.c_str());
  801. MessageBoxW(NULL, (L"厨房网口打印机连接失败,IP地址:" + CLewaimaiString::UTF8ToUnicode(wangkou_ip)).c_str(), L"打印机连接失败", MB_OK);
  802. return;
  803. }
  804. }
  805. //读取标签纸的规格和方向
  806. int biaoqian_width, biaoqian_height;
  807. std::string printer_guige = CSetting::GetParam("setting_biaoqian_printer_guige");
  808. if (printer_guige == "40*30mm")
  809. {
  810. biaoqian_width = 40;
  811. biaoqian_height = 30;
  812. }
  813. else if (printer_guige == "35*25mm")
  814. {
  815. biaoqian_width = 35;
  816. biaoqian_height = 25;
  817. }
  818. else if (printer_guige == "30*25mm")
  819. {
  820. biaoqian_width = 30;
  821. biaoqian_height = 25;
  822. }
  823. else if (printer_guige == "30*20mm")
  824. {
  825. biaoqian_width = 30;
  826. biaoqian_height = 20;
  827. }
  828. else if (printer_guige == "60*50mm")
  829. {
  830. biaoqian_width = 60;
  831. biaoqian_height = 50;
  832. }
  833. else if (printer_guige == "50*40mm")
  834. {
  835. biaoqian_width = 50;
  836. biaoqian_height = 40;
  837. }
  838. else if (printer_guige == "40*60mm")
  839. {
  840. biaoqian_width = 40;
  841. biaoqian_height = 60;
  842. }
  843. else
  844. {
  845. biaoqian_width = 40;
  846. biaoqian_height = 30;
  847. }
  848. int nDIRECTION;
  849. std::string printer_fangxiang = CSetting::GetParam("setting_biaoqian_printer_fangxiang");
  850. if (printer_fangxiang == "1")
  851. {
  852. nDIRECTION = 1;
  853. }
  854. else
  855. {
  856. nDIRECTION = 0;
  857. }
  858. std::vector<CWaimaiOrderItem> cur_printer_use;
  859. std::string setting_biaoqian_printer_is_type = CSetting::GetParam("setting_biaoqian_printer_is_type");
  860. std::string setting_biaoqian_printer_type_ids = CSetting::GetParam("setting_biaoqian_printer_type_ids");
  861. if (setting_biaoqian_printer_is_type == "0")
  862. {
  863. for (std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  864. {
  865. //如果当前商品的分类,在标签打印机设置的分类里面,才加入
  866. cur_printer_use.push_back(*it);
  867. }
  868. }
  869. else
  870. {
  871. std::map<string, bool> ids_map;
  872. std::vector<string> ids = CLewaimaiString::Split(setting_biaoqian_printer_type_ids, ",");
  873. for (std::vector<string>::iterator it = ids.begin(); it != ids.end(); it++)
  874. {
  875. ids_map[(*it)] = true;
  876. }
  877. for (std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  878. {
  879. if (ids_map.find((*it).m_type_id) != ids_map.end())
  880. {
  881. //如果当前商品的分类,在标签打印机设置的分类里面,才加入
  882. cur_printer_use.push_back(*it);
  883. }
  884. }
  885. }
  886. //先计算商品的总数量
  887. int foodNum = 0;
  888. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  889. {
  890. std::string quantity = (*it).m_quantity;
  891. foodNum += atoi(quantity.c_str());
  892. }
  893. //商品内容
  894. int curFoodNum = 0;
  895. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  896. {
  897. std::string food_name = (*it).m_food_name;
  898. std::string food_price = (*it).m_item_price;
  899. std::string quantity = (*it).m_quantity;
  900. //每份商品打印一个标签
  901. int nQuantity = atoi(quantity.c_str());
  902. for(int i = 0; i < nQuantity; i++)
  903. {
  904. //初始化标签打印机
  905. BIAOQIAN_Reset(biaoqian_width, biaoqian_height, nDIRECTION);
  906. std::string textData = "";
  907. curFoodNum++;
  908. //打印小票自定义名称
  909. string printer_tip_name = CLewaimaiString::UTF8ToANSI(order.m_printer_tip_name);
  910. if (printer_tip_name.length() == 0)
  911. {
  912. printer_tip_name = "外卖";
  913. }
  914. std::string restaurant_number = order.m_restaurant_number;
  915. std::string order_num_info = printer_tip_name + " #" + CLewaimaiString::UTF8ToANSI(restaurant_number);
  916. //纸张宽度40mm的,是 320个点位(每mm8个),刚好可以放13个汉字,每个汉字宽度差不多是24,商品换行显示的时候,
  917. //每行最多12个汉字,也就是空24个,因此这里设置12,刚好左右各空一半
  918. textData += BIAOQIAN_TEXTGet(order_num_info, 12, 6, 1, 1);
  919. //打印份数
  920. std::string numInfo = to_string(curFoodNum) + "/" + to_string(foodNum);
  921. //计算份数的位置
  922. int nFenshuLeft = (biaoqian_width - 8) * 8;
  923. textData += BIAOQIAN_TEXTGet(numInfo, nFenshuLeft, 6, 1, 1);
  924. //每行最多显示12个汉字,这里要计算一下换行(要先转成ANSI格式)
  925. std::string handle_food_name = CLewaimaiString::UTF8ToANSI(food_name);
  926. std::vector<std::string> foodNameVector = HandleBiaoqianFoodname(handle_food_name, biaoqian_width);
  927. int nRow = 0;
  928. //考虑是否用大号字体
  929. bool is_big_name = false;
  930. if(foodNameVector.size() <= 2)
  931. {
  932. //不超过2行,可以用大的字体
  933. is_big_name = true;
  934. }
  935. //LOG_INFO("foodNameVector size:" << foodNameVector.size());
  936. for(std::vector<std::string>::iterator it = foodNameVector.begin(); it != foodNameVector.end(); it++)
  937. {
  938. //LOG_INFO("nRow:" << nRow << ", 准备打印商品名字:" << (*it).c_str());
  939. if(is_big_name)
  940. {
  941. textData += BIAOQIAN_TEXTGet(*it, 12, 42 + 60 * nRow, 1, 2);
  942. }
  943. else
  944. {
  945. textData += BIAOQIAN_TEXTGet(*it, 12, 42 + 30 * nRow, 1, 1);
  946. }
  947. nRow++;
  948. }
  949. int nPriceY;
  950. if (biaoqian_height == 30)
  951. {
  952. nPriceY = 170;
  953. }
  954. else if (biaoqian_height == 25)
  955. {
  956. nPriceY = 130;
  957. }
  958. else if (biaoqian_height == 20)
  959. {
  960. nPriceY = 90;
  961. }
  962. else if (biaoqian_height == 50)
  963. {
  964. nPriceY = 330;
  965. }
  966. else if (biaoqian_height == 40)
  967. {
  968. nPriceY = 250;
  969. }
  970. else if (biaoqian_height == 60)
  971. {
  972. nPriceY = 410;
  973. }
  974. //打印价格
  975. std::string priceInfo = CLewaimaiString::UTF8ToANSI(food_price) + "元";
  976. textData += BIAOQIAN_TEXTGet(priceInfo, 12, nPriceY, 1, 1);
  977. //打印订单号
  978. std::string order_num = "订单号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  979. textData += BIAOQIAN_TEXTGet(order_num, 12, nPriceY + 30, 1, 1);
  980. WriteData(textData);
  981. BIAOQIAN_PRINT();
  982. BIAOQIAN_CLS();
  983. }
  984. }
  985. }
  986. void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
  987. {
  988. LOG_INFO("_______________________________开始打印厨房小票,订单编号:" << order.m_order_num.c_str() << "_________________________");
  989. //设置模式,后面输出数据不会错
  990. m_type = 2;
  991. //读取厨房打印机信息
  992. std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
  993. LOG_INFO("厨房打印机总数量:" << total_printers.size());
  994. for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
  995. {
  996. ChufangPrinter printer = *it;
  997. std::string ip = printer.ip;
  998. //初始化连接
  999. try
  1000. {
  1001. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
  1002. m_socket.connect(ep);
  1003. }
  1004. catch(std::exception& e)
  1005. {
  1006. std::string err = e.what();
  1007. LOG_INFO("厨房网口打印机连接失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
  1008. MessageBoxW(NULL, (L"厨房网口打印机连接失败,IP地址:" + CLewaimaiString::UTF8ToUnicode(ip)).c_str(), L"打印机连接失败", MB_OK);
  1009. //连接失败了,处理下一个厨房打印机
  1010. continue;
  1011. }
  1012. LOG_INFO("厨房网口打印机连接成功,IP地址:" << ip.c_str());
  1013. std::string guige = printer.guige;
  1014. std::string fendan = printer.fendan;
  1015. std::string fenlei = printer.fenlei;
  1016. std::string fenlei_ids = printer.fenlei_ids;
  1017. std::vector<CWaimaiOrderItem> cur_printer_use;
  1018. if (fenlei == "0")
  1019. {
  1020. LOG_INFO("未开启分类打印");
  1021. cur_printer_use = order.m_order_items;
  1022. }
  1023. else
  1024. {
  1025. LOG_INFO("已开启分类打印");
  1026. //如果开启了分类打印,就要比对了
  1027. std::map<string, bool> ids_map;
  1028. std::vector<string> ids = CLewaimaiString::Split(fenlei_ids, ",");
  1029. for (std::vector<string>::iterator it = ids.begin(); it != ids.end(); it++)
  1030. {
  1031. ids_map[(*it)] = true;
  1032. }
  1033. for (std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  1034. {
  1035. if (ids_map.find((*it).m_type_id) != ids_map.end())
  1036. {
  1037. //如果当前商品的分类,在厨房打印机设置的分类里面,才加入
  1038. cur_printer_use.push_back(*it);
  1039. }
  1040. }
  1041. }
  1042. LOG_INFO("当前厨房打印机可以打印的商品数量:" << cur_printer_use.size());
  1043. if (cur_printer_use.size() == 0)
  1044. {
  1045. //如果这个厨房打印机没有商品可以打印(开启了商品分类打印可能出现),就直接跳过
  1046. m_socket.close();
  1047. continue;
  1048. }
  1049. if(fendan == "0")
  1050. {
  1051. POS_Reset();
  1052. POS_TextOut("派工单", true, true, 1);
  1053. POS_FeedLine();
  1054. POS_TextOut(CLewaimaiString::UTF8ToANSI(order.m_shop_name), false, false, 1);
  1055. POS_FeedLine();
  1056. POS_FeedLine();
  1057. string order_type = "#" + order.m_restaurant_number + " ";
  1058. if(order.m_is_selftake == "1")
  1059. {
  1060. POS_TextOut(order_type + "到店自取订单", false, true, 1);
  1061. POS_FeedLine();
  1062. POS_FeedLine();
  1063. }
  1064. else
  1065. {
  1066. POS_TextOut(order_type + "外卖订单", false, true, 1);
  1067. POS_FeedLine();
  1068. POS_FeedLine();
  1069. }
  1070. if (order.m_is_book == "1")
  1071. {
  1072. //预约单
  1073. string date = "【预约单】" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  1074. POS_TextOut(date, false, true);
  1075. POS_TextOut("送达", false, false);
  1076. POS_FeedLine();
  1077. }
  1078. //打印订单号
  1079. string order_num = "订单号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  1080. POS_TextOut(order_num, false, false);
  1081. POS_FeedLine();
  1082. //打印顾客信息
  1083. //姓名
  1084. if (order.m_is_selftake != "1")
  1085. {
  1086. string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
  1087. POS_TextOut(name, true, true);
  1088. POS_FeedLine();
  1089. }
  1090. //电话
  1091. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  1092. POS_TextOut(phone, true, true);
  1093. POS_FeedLine();
  1094. //地址
  1095. if (order.m_is_selftake != "1")
  1096. {
  1097. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  1098. POS_TextOut(address, false, true);
  1099. POS_FeedLine();
  1100. }
  1101. //预设选项
  1102. if(order.m_order_field.size() > 1)
  1103. {
  1104. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  1105. {
  1106. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  1107. POS_TextOut(order_field, false, true, 0);
  1108. POS_FeedLine();
  1109. }
  1110. }
  1111. //准备开始打印商品详情
  1112. string lines;
  1113. if(guige == "58")
  1114. {
  1115. lines = "--------------------------------";
  1116. }
  1117. else
  1118. {
  1119. lines = "------------------------------------------------";
  1120. }
  1121. POS_TextOut(lines);
  1122. POS_FeedLine();
  1123. //商品标题
  1124. if(guige == "58")
  1125. {
  1126. POS_TextOut("商品 单价 数量 金额", false, true);
  1127. }
  1128. else
  1129. {
  1130. POS_TextOut("商品 单价 数量 金额", false, true);
  1131. }
  1132. POS_FeedLine();
  1133. //商品内容
  1134. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  1135. {
  1136. std::string food_name = (*it).m_food_name;
  1137. std::string food_price = (*it).m_item_price;
  1138. std::string quantity = (*it).m_quantity;
  1139. //计算总价
  1140. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  1141. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  1142. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  1143. //先输出商品名字
  1144. std::vector<string> m_names;
  1145. if(guige == "58")
  1146. {
  1147. m_names = HandleFoodname(food_name, 1);
  1148. }
  1149. else
  1150. {
  1151. m_names = HandleFoodname(food_name, 2);
  1152. }
  1153. std::string firstLine = m_names[0];
  1154. int nGuige;
  1155. if(guige == "58")
  1156. {
  1157. nGuige = 1;
  1158. }
  1159. else
  1160. {
  1161. nGuige = 2;
  1162. }
  1163. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  1164. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  1165. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  1166. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  1167. POS_TextOut(firstLineShow, false, true, 0);
  1168. POS_FeedLine();
  1169. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  1170. {
  1171. POS_TextOut((*it), false, true, 0);
  1172. POS_FeedLine();
  1173. }
  1174. }
  1175. //判断是否有备注
  1176. if(order.m_memo.length() > 0)
  1177. {
  1178. POS_FeedLine();
  1179. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  1180. POS_TextOut(address, false, true, 0);
  1181. POS_FeedLine();
  1182. }
  1183. //结束商品详情打印
  1184. POS_TextOut(lines);
  1185. POS_FeedLine();
  1186. POS_FeedLine();
  1187. POS_FeedLine();
  1188. POS_FeedLine();
  1189. POS_FeedLine();
  1190. POS_FeedLine();
  1191. POS_CutPaper();
  1192. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  1193. POS_Reset();
  1194. }
  1195. else
  1196. {
  1197. POS_Reset();
  1198. //分单模式下,每个商品打印一张单
  1199. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  1200. {
  1201. POS_TextOut("派工单", true, true, 1);
  1202. POS_FeedLine();
  1203. POS_FeedLine();
  1204. string order_type = "#" + order.m_restaurant_number + " ";
  1205. if(order.m_is_selftake == "1")
  1206. {
  1207. POS_TextOut(order_type + "到店自取订单", false, true, 1);
  1208. POS_FeedLine();
  1209. POS_FeedLine();
  1210. }
  1211. else
  1212. {
  1213. POS_TextOut(order_type + "外卖订单", false, true, 1);
  1214. POS_FeedLine();
  1215. POS_FeedLine();
  1216. }
  1217. if (order.m_is_book == "1")
  1218. {
  1219. //预约单
  1220. string date = "【预约单】" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  1221. POS_TextOut(date, false, true);
  1222. POS_TextOut("送达", false, false);
  1223. POS_FeedLine();
  1224. }
  1225. //打印订单号
  1226. string order_num = "订 单 号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  1227. POS_TextOut(order_num, false, false);
  1228. POS_FeedLine();
  1229. //打印顾客信息
  1230. //姓名
  1231. if (order.m_is_selftake != "1")
  1232. {
  1233. string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
  1234. POS_TextOut(name, false, false);
  1235. POS_FeedLine();
  1236. }
  1237. //电话
  1238. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  1239. POS_TextOut(phone, false, false);
  1240. POS_FeedLine();
  1241. //地址
  1242. if (order.m_is_selftake != "1")
  1243. {
  1244. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  1245. POS_TextOut(address, false, false);
  1246. POS_FeedLine();
  1247. }
  1248. //预设选项
  1249. if(order.m_order_field.size() > 1)
  1250. {
  1251. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  1252. {
  1253. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  1254. POS_TextOut(order_field, false, true, 0);
  1255. POS_FeedLine();
  1256. }
  1257. }
  1258. //准备开始打印商品详情
  1259. string lines;
  1260. if(guige == "58")
  1261. {
  1262. lines = "--------------------------------";
  1263. }
  1264. else
  1265. {
  1266. lines = "------------------------------------------------";
  1267. }
  1268. POS_TextOut(lines);
  1269. POS_FeedLine();
  1270. //商品标题
  1271. if(guige == "58")
  1272. {
  1273. POS_TextOut("商品 单价 数量 金额", false, true);
  1274. }
  1275. else
  1276. {
  1277. POS_TextOut("商品 单价 数量 金额", false, true);
  1278. }
  1279. POS_FeedLine();
  1280. std::string food_name = (*it).m_food_name;
  1281. std::string food_price = (*it).m_item_price;
  1282. std::string quantity = (*it).m_quantity;
  1283. //计算总价
  1284. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  1285. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  1286. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  1287. //先输出商品名字
  1288. std::vector<string> m_names;
  1289. if(guige == "58")
  1290. {
  1291. m_names = HandleFoodname(food_name, 1);
  1292. }
  1293. else
  1294. {
  1295. m_names = HandleFoodname(food_name, 2);
  1296. }
  1297. std::string firstLine = m_names[0];
  1298. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  1299. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  1300. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  1301. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  1302. POS_TextOut(firstLineShow, false, true, 0);
  1303. POS_FeedLine();
  1304. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  1305. {
  1306. POS_TextOut((*it), false, true, 0);
  1307. POS_FeedLine();
  1308. }
  1309. //判断是否有备注
  1310. if(order.m_memo.length() > 0)
  1311. {
  1312. POS_FeedLine();
  1313. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  1314. POS_TextOut(address, false, true, 0);
  1315. POS_FeedLine();
  1316. }
  1317. //结束商品详情打印
  1318. POS_TextOut(lines);
  1319. POS_FeedLine();
  1320. POS_FeedLine();
  1321. POS_FeedLine();
  1322. POS_FeedLine();
  1323. POS_FeedLine();
  1324. POS_FeedLine();
  1325. POS_CutPaper();
  1326. }
  1327. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  1328. POS_Reset();
  1329. }
  1330. m_socket.close();
  1331. }
  1332. LOG_INFO("_______________________________结束打印厨房小票,订单编号:" << order.m_order_num.c_str() << "_________________________");
  1333. }
  1334. std::vector<std::wstring> CPosPrinter::getUsbDevices()
  1335. {
  1336. return m_usb_devices;
  1337. }
  1338. int CPosPrinter::WriteData(string msg)
  1339. {
  1340. return WriteBuf((unsigned char*)msg.c_str(), msg.length());
  1341. }
  1342. /*
  1343. *这个函数只负责把数据拼接到打印缓存里面,不负责实际的发送数据
  1344. **/
  1345. int CPosPrinter::WriteBuf(const unsigned char* buf, int len)
  1346. {
  1347. if(m_type == 1)
  1348. {
  1349. //USB、串口、并口,走这里
  1350. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  1351. {
  1352. HANDLE hPort = (*it).hPort;
  1353. //同步写数据
  1354. DWORD dwWrite;
  1355. BOOL ret = WriteFile(hPort, buf, len, &dwWrite, NULL);
  1356. if (!ret)
  1357. {
  1358. LOG_INFO("WriteFile Failed!, hPort:"<<hPort<<", type:"<<(*it).type);
  1359. }
  1360. }
  1361. return 1;
  1362. }
  1363. else
  1364. {
  1365. //网口走这里
  1366. try
  1367. {
  1368. m_socket.write_some(boost::asio::buffer(buf, len));
  1369. }
  1370. catch(const std::exception& e)
  1371. {
  1372. LOG_INFO("网口打印机发送消息失败,错误信息:" << e.what());
  1373. }
  1374. return 1;
  1375. }
  1376. }
  1377. /*
  1378. *测试打印机是否连接
  1379. **/
  1380. bool CPosPrinter::PortTest(HANDLE hPort)
  1381. {
  1382. //这里的话目前USB、串口、并口用的都是同步读写模式,有的打印机(比如一些发票打印机)CreateFile会成功,但是WriteFile会一直阻塞卡着,如果检测的话,
  1383. //在这里会一直卡住,因此这里的检测就不检测了,默认所有的都是连通的
  1384. //因此,对于智能识别模式,可能会出现无法打印(因为被某些usb打印设备阻塞),这种情况就只能手动选择USB模式,然后重新启动软件
  1385. return true;
  1386. //标签打印机的查询状态指令
  1387. char chInitCode[2] = { 0x1b, 0x40};
  1388. DWORD dwWrite;
  1389. BOOL rc = WriteFile(hPort, chInitCode, 2, &dwWrite, NULL);
  1390. if(rc)
  1391. {
  1392. LOG_INFO("porttest success, handle:" << hPort);
  1393. return true;
  1394. }
  1395. else
  1396. {
  1397. LOG_INFO("porttest fail, handle:" << hPort);
  1398. return true;
  1399. }
  1400. return false;
  1401. }
  1402. int CPosPrinter::POS_Reset(void)
  1403. {
  1404. unsigned char s[2] = {0x1B, 0x40};
  1405. WriteBuf(s, 2);
  1406. return 0;
  1407. }
  1408. int CPosPrinter::POS_FeedLine(void)
  1409. {
  1410. unsigned char s[1] = {0x0A};
  1411. WriteBuf(s, 1);
  1412. return 0;
  1413. }
  1414. int CPosPrinter::POS_Feed(void)
  1415. {
  1416. unsigned char s[3] = { 0x1B, 0x4A, 0x00 };
  1417. WriteBuf(s, 3);
  1418. return 0;
  1419. }
  1420. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  1421. {
  1422. string s;
  1423. s = "\x1D\x50\xB4\xB4";
  1424. WriteData(s);
  1425. s = "\x1B\x53";
  1426. WriteData(s);
  1427. return 0;
  1428. }
  1429. int CPosPrinter::POS_SET_MOVE_X()
  1430. {
  1431. unsigned char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
  1432. WriteBuf(s2, 6);
  1433. return 0;
  1434. }
  1435. int CPosPrinter::POS_MOVE_X()
  1436. {
  1437. unsigned char s[1] = { 0x09 };
  1438. WriteBuf(s, 1);
  1439. return 0;
  1440. }
  1441. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  1442. {
  1443. unsigned char cx = (unsigned char)(x);
  1444. unsigned char cy = (unsigned char)(y);
  1445. unsigned char s1[4] = { 0x1B, 0x24, cx, cy };
  1446. WriteBuf(s1, 4);
  1447. return 0;
  1448. }
  1449. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  1450. {
  1451. unsigned char cx = (unsigned char)(x);
  1452. unsigned char cy = (unsigned char)(y);
  1453. unsigned char s1[4] = { 0x1D, 0x57, cx, cy };
  1454. WriteBuf(s1, 4);
  1455. return 0;
  1456. }
  1457. /*
  1458. *align_type:0 左对齐 1 居中对齐 2右对齐
  1459. **/
  1460. int CPosPrinter::POS_TextOut(string abc, bool is_double_width, bool is_double_height, int align_type)
  1461. {
  1462. if(is_double_width && is_double_height)
  1463. {
  1464. unsigned char s1[3] = { 0x1B, 0x21, 0x30 };
  1465. WriteBuf(s1, 3);
  1466. unsigned char s2[3] = { 0x1C, 0x21, 0x0c };
  1467. WriteBuf(s2, 3);
  1468. }
  1469. else if(is_double_width && !is_double_height)
  1470. {
  1471. unsigned char s1[3] = { 0x1B, 0x21, 0x20 };
  1472. WriteBuf(s1, 3);
  1473. unsigned char s2[3] = { 0x1C, 0x21, 0x04 };
  1474. WriteBuf(s2, 3);
  1475. }
  1476. else if(!is_double_width && is_double_height)
  1477. {
  1478. unsigned char s1[3] = { 0x1B, 0x21, 0x10 };
  1479. WriteBuf(s1, 3);
  1480. unsigned char s2[3] = { 0x1C, 0x21, 0x08 };
  1481. WriteBuf(s2, 3);
  1482. }
  1483. else
  1484. {
  1485. unsigned char s1[3] = { 0x1B, 0x21, 0x00 };
  1486. WriteBuf(s1, 3);
  1487. unsigned char s2[3] = { 0x1C, 0x57, 0x00 };
  1488. WriteBuf(s2, 3);
  1489. }
  1490. if(align_type == 0)
  1491. {
  1492. unsigned char s1[3] = { 0x1B, 0x61, 0x00 };
  1493. WriteBuf(s1, 3);
  1494. }
  1495. else if(align_type == 1)
  1496. {
  1497. unsigned char s1[3] = { 0x1B, 0x61, 0x01 };
  1498. WriteBuf(s1, 3);
  1499. }
  1500. else if(align_type == 2)
  1501. {
  1502. unsigned char s1[3] = { 0x1B, 0x61, 0x02 };
  1503. WriteBuf(s1, 3);
  1504. }
  1505. else
  1506. {
  1507. }
  1508. WriteData(abc);
  1509. return 0;
  1510. }
  1511. /*
  1512. *打印机切纸,直接切不走纸
  1513. **/
  1514. int CPosPrinter::POS_CutPaper()
  1515. {
  1516. unsigned char s[4] = { 0x1D, 0x56, 0x01};
  1517. WriteBuf(s, 3);
  1518. return 0;
  1519. }
  1520. /*
  1521. *先转换成ANSI格式才能传进来
  1522. **/
  1523. void CPosPrinter::POS_OutQRCode(std::string dataString)
  1524. {
  1525. QRcode * qrCode = nullptr;
  1526. qrCode = QRcode_encodeString(dataString.c_str(), 0, QR_ECLEVEL_H, QR_MODE_8, 1);
  1527. if (qrCode == nullptr)
  1528. {
  1529. printf("error occur\n");
  1530. return ;
  1531. }
  1532. unsigned int unWidth = qrCode->width;
  1533. unsigned int unWidthAdjusted = unWidth * 8;
  1534. unsigned int unHeightAjusted = unWidthAdjusted;
  1535. unsigned char* realData = new unsigned char[unWidthAdjusted * unHeightAjusted];
  1536. memset(realData, 0, unWidthAdjusted * unHeightAjusted);
  1537. unsigned char* pSourceData = qrCode->data;
  1538. for (unsigned int i = 0; i < unHeightAjusted; i++)
  1539. {
  1540. int realHeight = i / 8;
  1541. for (unsigned int j = 0; j < unWidthAdjusted; j++)
  1542. {
  1543. int realWeight = j / 8;
  1544. if (pSourceData[unWidth * realHeight + realWeight] & 1)
  1545. {
  1546. //表示这个像素要打印
  1547. realData[unWidthAdjusted * i + j] = 1;
  1548. }
  1549. else
  1550. {
  1551. realData[unWidthAdjusted * i + j] = 0;
  1552. }
  1553. }
  1554. }
  1555. //设置行间距为0
  1556. unsigned char data[3] = { 0x1B, 0x33, 0x00 };
  1557. WriteBuf(data, 3);
  1558. //设置图像居中对齐
  1559. unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
  1560. WriteBuf(data2, 3);
  1561. //选择位图模式
  1562. unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
  1563. escBmp[3] = (unsigned char)(unWidthAdjusted % 256);
  1564. escBmp[4] = (unsigned char)(unWidthAdjusted / 256);
  1565. //换行符,每打印一行都要发送这个,不然有些尺寸下图片会乱掉
  1566. unsigned char change_line_tag = '\n';
  1567. //存储每行数据的结构
  1568. unsigned char* dataTmp = new unsigned char[unWidthAdjusted * 3];
  1569. //循环图片像素打印图片
  1570. for (unsigned int i = 0; i < (unHeightAjusted / 24 + 1); i++)
  1571. {
  1572. //设置模式为位图模式
  1573. WriteBuf(escBmp, 5);
  1574. //重新开始打印一行,置零
  1575. memset(dataTmp, 0, unWidthAdjusted * 3);
  1576. //循环宽
  1577. for (unsigned int j = 0; j < unWidthAdjusted; j++)
  1578. {
  1579. for (unsigned int k = 0; k < 24; k++)
  1580. {
  1581. //找到有像素的区域,也就是未超出位图高度的区域
  1582. if (((i * 24) + k) < unHeightAjusted)
  1583. {
  1584. if (realData[unWidthAdjusted * (i * 24 + k) + j] == 1)
  1585. {
  1586. //如果本来是黑色的像素,那么就把对应的数据位设置为1
  1587. dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
  1588. }
  1589. }
  1590. }
  1591. }
  1592. WriteBuf(dataTmp, unWidthAdjusted * 3);
  1593. //换行符,这里必须要发这个,不然图片会乱
  1594. WriteBuf(&change_line_tag, 1);
  1595. }
  1596. WriteBuf(&change_line_tag, 1);
  1597. //还原默认的行间距
  1598. unsigned char data3[2] = { 0x1B, 0x32 };
  1599. WriteBuf(data3, 2);
  1600. delete[] dataTmp;
  1601. QRcode_free(qrCode);
  1602. delete[] realData;
  1603. return ;
  1604. }
  1605. void CPosPrinter::POS_OutBmp(std::wstring ImagePath)
  1606. {
  1607. //初始化
  1608. POS_Reset();
  1609. //设置行间距为0
  1610. unsigned char data[3] = { 0x1B, 0x33, 0x00 };
  1611. WriteBuf(data, 3);
  1612. //设置图像居中对齐
  1613. unsigned char data2[3] = { 0x1B, 0x61, 0x01 };
  1614. WriteBuf(data2, 3);
  1615. CBitmapHelper helper;
  1616. helper.LoadImage(ImagePath.c_str());
  1617. Bitmap* bmp = helper.getBmp();
  1618. //先进行图片预处理
  1619. unsigned int nWidth = bmp->GetWidth();
  1620. unsigned int nHeight = bmp->GetHeight();
  1621. //压缩尺寸
  1622. if (nWidth > 380)
  1623. {
  1624. int newWidth = 380;
  1625. int newHeight = (int)(nHeight / (nWidth / 380.0));
  1626. helper.ScaleBitmap(newWidth, newHeight);;
  1627. nWidth = newWidth;
  1628. nHeight = newHeight;
  1629. }
  1630. //二值化
  1631. helper.Image2Values();
  1632. //获得最新的图像指针
  1633. bmp = helper.getBmp();
  1634. //选择位图模式
  1635. unsigned char escBmp[5] = { 0x1B, 0x2A, 0x21, 0x00, 0x00 };
  1636. escBmp[3] = (unsigned char)(nWidth % 256);
  1637. escBmp[4] = (unsigned char)(nWidth / 256);
  1638. Gdiplus::Color pixelColor;
  1639. //换行符,每打印一行都要发送这个,不然有些尺寸下图片会乱掉
  1640. unsigned char change_line_tag = '\n';
  1641. //存储每行数据的结构
  1642. unsigned char* dataTmp = new unsigned char[nWidth * 3];
  1643. //循环图片像素打印图片
  1644. for (unsigned int i = 0; i < (nHeight / 24 + 1); i++)
  1645. {
  1646. //设置模式为位图模式
  1647. WriteBuf(escBmp, 5);
  1648. //重新开始打印一行,置零
  1649. memset(dataTmp, 0, nWidth * 3);
  1650. //循环宽
  1651. for (unsigned int j = 0; j < nWidth; j++)
  1652. {
  1653. for (unsigned int k = 0; k < 24; k++)
  1654. {
  1655. //找到有像素的区域,也就是未超出位图高度的区域
  1656. if (((i * 24) + k) < nHeight)
  1657. {
  1658. bmp->GetPixel(j, (i * 24) + k, &pixelColor);
  1659. unsigned char r = pixelColor.GetR();
  1660. if (r == 0)
  1661. {
  1662. //如果本来是黑色的像素,那么就把对应的数据位设置为1
  1663. dataTmp[j * 3 + k / 8] += (unsigned char)(128 >> (k % 8));
  1664. }
  1665. }
  1666. }
  1667. }
  1668. WriteBuf(dataTmp, nWidth * 3);
  1669. //换行符,这里必须要发这个,不然图片会乱
  1670. WriteBuf(&change_line_tag, 1);
  1671. }
  1672. WriteBuf(&change_line_tag, 1);
  1673. //还原默认的行间距
  1674. unsigned char data3[2] = { 0x1B, 0x32 };
  1675. WriteBuf(data3, 2);
  1676. delete[] dataTmp;
  1677. }
  1678. void CPosPrinter::BIAOQIAN_Reset(int nWidth, int nHeight, int nDirection)
  1679. {
  1680. char endTag[3] = {0x0d, 0x0a, 0x00};
  1681. std::string size = "SIZE " + to_string(nWidth) + " mm," + to_string(nHeight) + " mm";
  1682. size += endTag;
  1683. std::string gap = "GAP 2 mm,0 mm";
  1684. gap += endTag;
  1685. std::string speed = "SPEED 4.0";
  1686. speed += endTag;
  1687. std::string density = "DENSITY 12";
  1688. density += endTag;
  1689. std::string DIRECTION = "DIRECTION " + to_string(nDirection);
  1690. DIRECTION += endTag;
  1691. std::string REFERENCE = "REFERENCE 0,0";
  1692. REFERENCE += endTag;
  1693. std::string PEER = "SET PEEL OFF";
  1694. PEER += endTag;
  1695. std::string TEAR = "SET TEAR ON";
  1696. TEAR += endTag;
  1697. std::string cls = "CLS";
  1698. cls += endTag;
  1699. std::string data = size + gap + speed + density + DIRECTION + REFERENCE + PEER + TEAR + cls;
  1700. WriteData(data);
  1701. }
  1702. /*
  1703. *前进一张纸
  1704. **/
  1705. void CPosPrinter::BIAOQIAN_FORMFEED()
  1706. {
  1707. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1708. std::string formfeed = "FORMFEED";
  1709. formfeed += endTag;
  1710. WriteData(formfeed);
  1711. }
  1712. void CPosPrinter::BIAOQIAN_TEXTOUT(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1713. {
  1714. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1715. string text = "TEXT ";
  1716. text += to_string(x) + "," + to_string(y) + ",";
  1717. text += "\"TSS24.BF2\",";
  1718. text += to_string(0) + ",";
  1719. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1720. text += "\"" + content + "\"";
  1721. text += endTag;
  1722. WriteData(text);
  1723. }
  1724. std::string CPosPrinter::BIAOQIAN_TEXTGet(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1725. {
  1726. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1727. string text = "TEXT ";
  1728. text += to_string(x) + "," + to_string(y) + ",";
  1729. text += "\"TSS24.BF2\",";
  1730. text += to_string(0) + ",";
  1731. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1732. text += "\"" + content + "\"";
  1733. text += endTag;
  1734. return text;
  1735. }
  1736. void CPosPrinter::BIAOQIAN_PRINT()
  1737. {
  1738. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1739. std::string print = "PRINT 1";
  1740. print += endTag;
  1741. WriteData(print);
  1742. }
  1743. void CPosPrinter::BIAOQIAN_CLS()
  1744. {
  1745. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1746. std::string cls = "CLS";
  1747. cls += endTag;
  1748. WriteData(cls);
  1749. }
  1750. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  1751. {
  1752. nHanzi = 0;
  1753. nZimu = 0;
  1754. const char* buffer = s.c_str();
  1755. while(*buffer != '\0')
  1756. {
  1757. if(!(*buffer >= 0 && *buffer <= 127))
  1758. {
  1759. //汉字
  1760. buffer++;
  1761. buffer++;
  1762. nHanzi++;
  1763. }
  1764. else
  1765. {
  1766. //字母
  1767. buffer++;
  1768. nZimu++;
  1769. }
  1770. }
  1771. }
  1772. /*
  1773. *规格 1:58mm 2:80mm
  1774. **/
  1775. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  1776. {
  1777. std::vector<std::string> newnameArray;
  1778. int nHanzi, nZimu;
  1779. CalWord(oldname, nHanzi, nZimu);
  1780. int nWidth = nHanzi * 2 + nZimu;
  1781. int maxWidth;
  1782. if(guige == 1)
  1783. {
  1784. maxWidth = 15;
  1785. }
  1786. else
  1787. {
  1788. maxWidth = 28;
  1789. }
  1790. if(nWidth <= maxWidth)
  1791. {
  1792. //对于nWidth补空格
  1793. for(int i = 0; i < maxWidth - nWidth; i++)
  1794. {
  1795. oldname += " ";
  1796. }
  1797. newnameArray.push_back(oldname);
  1798. return newnameArray;
  1799. }
  1800. //宽度大于15的情况,如果超过了,就要进行换行截取
  1801. const char* s = oldname.c_str();
  1802. int nTmp = 0;
  1803. int nTotal = 0;
  1804. while(*s != '\0')
  1805. {
  1806. if(!(*s >= 0 && *s <= 127))
  1807. {
  1808. //汉字的情况
  1809. s++;
  1810. nTmp++;
  1811. nTotal++;
  1812. s++;
  1813. nTmp++;
  1814. nTotal++;
  1815. }
  1816. else
  1817. {
  1818. //字母的情况
  1819. s++;
  1820. nTmp++;
  1821. nTotal++;
  1822. }
  1823. if(nTmp == maxWidth)
  1824. {
  1825. //这里开始要换行了
  1826. string newnameItem;
  1827. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1828. newnameArray.push_back(newnameItem);
  1829. nTmp = 0;
  1830. }
  1831. else if(nTmp == maxWidth - 1)
  1832. {
  1833. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1834. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1835. {
  1836. //下一个是中文,也要换行了,补齐一个空格
  1837. string newnameItem;
  1838. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1839. newnameItem += " ";
  1840. newnameArray.push_back(newnameItem);
  1841. nTmp = 0;
  1842. }
  1843. }
  1844. }
  1845. if(nTmp > 0)
  1846. {
  1847. //处理分隔后的最后一行
  1848. string newnameItem;
  1849. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1850. for(int i = 0; i < maxWidth - nTmp; i++)
  1851. {
  1852. newnameItem += " ";
  1853. }
  1854. newnameArray.push_back(newnameItem);
  1855. }
  1856. return newnameArray;
  1857. }
  1858. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  1859. {
  1860. double price = atof(oldprice.c_str());
  1861. if(price < 10.00)
  1862. {
  1863. //单位数
  1864. if(guige == 1)
  1865. {
  1866. return " " + oldprice;
  1867. }
  1868. else
  1869. {
  1870. return " " + oldprice;
  1871. }
  1872. }
  1873. else if(price > 9.99 && price < 100.00)
  1874. {
  1875. //双位数
  1876. if(guige == 1)
  1877. {
  1878. return " " + oldprice;
  1879. }
  1880. else
  1881. {
  1882. return " " + oldprice;
  1883. }
  1884. }
  1885. else if(price > 99.99 && price < 1000.00)
  1886. {
  1887. //三位数
  1888. if(guige == 1)
  1889. {
  1890. return " " + oldprice;
  1891. }
  1892. else
  1893. {
  1894. return " " + oldprice;
  1895. }
  1896. }
  1897. else
  1898. {
  1899. //四位数
  1900. if(guige == 1)
  1901. {
  1902. return " " + oldprice;
  1903. }
  1904. else
  1905. {
  1906. return " " + oldprice;
  1907. }
  1908. }
  1909. }
  1910. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  1911. {
  1912. int n = atoi(oldquantity.c_str());
  1913. if(n < 10)
  1914. {
  1915. if(guige == 1)
  1916. {
  1917. return " " + oldquantity;
  1918. }
  1919. else
  1920. {
  1921. return " " + oldquantity;
  1922. }
  1923. }
  1924. else if(n >= 10 && n <= 99)
  1925. {
  1926. if(guige == 1)
  1927. {
  1928. return " " + oldquantity;
  1929. }
  1930. else
  1931. {
  1932. return " " + oldquantity;
  1933. }
  1934. }
  1935. else
  1936. {
  1937. if(guige == 1)
  1938. {
  1939. return "" + oldquantity;
  1940. }
  1941. else
  1942. {
  1943. return " " + oldquantity;
  1944. }
  1945. }
  1946. }
  1947. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  1948. {
  1949. double price = atof(oldprice.c_str());
  1950. if(price < 10.00)
  1951. {
  1952. //单位数
  1953. if(guige == 1)
  1954. {
  1955. return " " + oldprice;
  1956. }
  1957. else
  1958. {
  1959. return " " + oldprice;
  1960. }
  1961. }
  1962. else if(price > 9.99 && price < 100.00)
  1963. {
  1964. //双位数
  1965. if(guige == 1)
  1966. {
  1967. return " " + oldprice;
  1968. }
  1969. else
  1970. {
  1971. return " " + oldprice;
  1972. }
  1973. }
  1974. else if(price > 99.99 && price < 1000.00)
  1975. {
  1976. //三位数
  1977. if(guige == 1)
  1978. {
  1979. return " " + oldprice;
  1980. }
  1981. else
  1982. {
  1983. return " " + oldprice;
  1984. }
  1985. }
  1986. else
  1987. {
  1988. //四位数
  1989. if(guige == 1)
  1990. {
  1991. return " " + oldprice;
  1992. }
  1993. else
  1994. {
  1995. return " " + oldprice;
  1996. }
  1997. }
  1998. }
  1999. std::vector<std::string> CPosPrinter::HandleBiaoqianFoodname(std::string oldname, int nBiaoqianWidth)
  2000. {
  2001. std::vector<std::string> newnameArray;
  2002. int nHanzi, nZimu;
  2003. CalWord(oldname, nHanzi, nZimu);
  2004. //文字内容的实际宽度,1个汉字算2个宽度,1个英文或者数字、标点算1个(40mm宽度最多打印12个汉字 )
  2005. int nWidth = nHanzi * 2 + nZimu;
  2006. //40 *30mmm的标签,宽度最大24
  2007. int maxWidth;
  2008. if (nBiaoqianWidth == 40)
  2009. {
  2010. maxWidth = 24;
  2011. }
  2012. else if (nBiaoqianWidth == 35)
  2013. {
  2014. maxWidth = 21;
  2015. }
  2016. else if (nBiaoqianWidth == 30)
  2017. {
  2018. maxWidth = 18;
  2019. }
  2020. else
  2021. {
  2022. maxWidth = 24;
  2023. }
  2024. if(nWidth <= maxWidth)
  2025. {
  2026. //对于nWidth补空格
  2027. for(int i = 0; i < maxWidth - nWidth; i++)
  2028. {
  2029. oldname += " ";
  2030. }
  2031. newnameArray.push_back(oldname);
  2032. return newnameArray;
  2033. }
  2034. //宽度大于15的情况,如果超过了,就要进行换行截取
  2035. const char* s = oldname.c_str();
  2036. int nTmp = 0;
  2037. int nTotal = 0;
  2038. while(*s != '\0')
  2039. {
  2040. if(!(*s >= 0 && *s <= 127))
  2041. {
  2042. //汉字的情况
  2043. s++;
  2044. nTmp++;
  2045. nTotal++;
  2046. s++;
  2047. nTmp++;
  2048. nTotal++;
  2049. }
  2050. else
  2051. {
  2052. //字母的情况
  2053. s++;
  2054. nTmp++;
  2055. nTotal++;
  2056. }
  2057. if(nTmp == maxWidth)
  2058. {
  2059. //这里开始要换行了
  2060. string newnameItem;
  2061. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  2062. newnameArray.push_back(newnameItem);
  2063. nTmp = 0;
  2064. }
  2065. else if(nTmp == maxWidth - 1)
  2066. {
  2067. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  2068. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  2069. {
  2070. //下一个是中文,也要换行了,补齐一个空格
  2071. string newnameItem;
  2072. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  2073. newnameItem += " ";
  2074. newnameArray.push_back(newnameItem);
  2075. nTmp = 0;
  2076. }
  2077. }
  2078. }
  2079. if(nTmp > 0)
  2080. {
  2081. //处理分隔后的最后一行
  2082. string newnameItem;
  2083. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  2084. for(int i = 0; i < maxWidth - nTmp; i++)
  2085. {
  2086. newnameItem += " ";
  2087. }
  2088. newnameArray.push_back(newnameItem);
  2089. }
  2090. return newnameArray;
  2091. }