CPosPrinter.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  1. #include "../pch/pch.h"
  2. #include "CPosPrinter.h"
  3. #include <winioctl.h>
  4. #include <setupapi.h>
  5. #include "../tool/CComHelper.h"
  6. using boost::asio::ip::tcp;
  7. CPosPrinter::CPosPrinter(): m_socket(m_io)
  8. {
  9. }
  10. CPosPrinter::~CPosPrinter()
  11. {
  12. }
  13. void CPosPrinter::InitUsb(int usbType)
  14. {
  15. //设置中文字符
  16. setlocale(LC_CTYPE, "chs");
  17. //取设备路径
  18. int nDevice = GetDevicePath((LPGUID)&USB_GUID, usbType);
  19. LOG_INFO("可用的USB打印机数量:" << nDevice);
  20. //添加usb端口
  21. int i = 0;
  22. while (i < nDevice)
  23. {
  24. LOG_INFO("准备打开端口 Port = " << m_usb_devices[i].c_str());
  25. HANDLE hPort = CreateFile(m_usb_devices[i].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  26. if (hPort == INVALID_HANDLE_VALUE)
  27. {
  28. // 打开端口失败
  29. DWORD error = GetLastError();
  30. if (error == 2)
  31. {
  32. //没有指定的文件
  33. LOG_INFO("没有找对对应的usb端口");
  34. }
  35. else if (error == 5)
  36. {
  37. LOG_INFO("usb端口被占用!");
  38. }
  39. continue;
  40. }
  41. LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
  42. if (PortTest(hPort) == true)
  43. {
  44. //端口测试连通,保存起来
  45. PrinterHandle newHandle;
  46. newHandle.hPort = hPort;
  47. newHandle.type = 1;
  48. m_hPorts.push_back(newHandle);
  49. }
  50. else
  51. {
  52. //端口打印机没有连接,那么就直接关闭掉并口,避免占用
  53. CloseHandle(hPort);
  54. }
  55. i++;
  56. }
  57. }
  58. void CPosPrinter::InitOneUsb(wstring usb_path)
  59. {
  60. HANDLE hPort = CreateFile(usb_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  61. if (hPort == INVALID_HANDLE_VALUE)
  62. {
  63. // 打开端口失败
  64. DWORD error = GetLastError();
  65. if (error == 2)
  66. {
  67. //没有指定的文件
  68. LOG_INFO("没有找对对应的usb端口");
  69. }
  70. else if (error == 5)
  71. {
  72. LOG_INFO("usb端口被占用!");
  73. }
  74. }
  75. LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
  76. if (PortTest(hPort) == true)
  77. {
  78. //端口测试连通,保存起来
  79. PrinterHandle newHandle;
  80. newHandle.hPort = hPort;
  81. newHandle.type = 1;
  82. m_hPorts.push_back(newHandle);
  83. }
  84. else
  85. {
  86. //端口打印机没有连接,那么就直接关闭掉并口,避免占用
  87. CloseHandle(hPort);
  88. }
  89. }
  90. void CPosPrinter::InitBingkou()
  91. {
  92. std::wstring LptStr = L"lpt1";
  93. HANDLE hPort = CreateFile(LptStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  94. if (hPort == INVALID_HANDLE_VALUE)
  95. {
  96. DWORD error = GetLastError();
  97. if (error == 2)
  98. {
  99. //没有指定的文件
  100. LOG_INFO("本机器没有并口!");
  101. }
  102. else if (error == 5)
  103. {
  104. LOG_INFO("并口被占用!");
  105. }
  106. }
  107. else
  108. {
  109. //这个表示并口可以使用
  110. LOG_INFO("找到并口,准备进行打印机检测! hPort:" << hPort);
  111. if (PortTest(hPort) == true)
  112. {
  113. //并口测试连通,保存起来
  114. PrinterHandle newHandle;
  115. newHandle.hPort = hPort;
  116. newHandle.type = 2;
  117. m_hPorts.push_back(newHandle);
  118. }
  119. else
  120. {
  121. //并口打印机没有连接,那么就直接关闭掉并口,避免占用
  122. CloseHandle(hPort);
  123. }
  124. }
  125. }
  126. void CPosPrinter::InitCom()
  127. {
  128. CComHelper helper;
  129. std::vector<std::wstring> comVector = helper.getComPort();
  130. for (std::vector<std::wstring>::iterator it = comVector.begin(); it != comVector.end(); it++)
  131. {
  132. std::wstring com2Str = *it;
  133. HANDLE hPort = CreateFile(com2Str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  134. if (hPort == INVALID_HANDLE_VALUE)
  135. {
  136. DWORD error = GetLastError();
  137. if (error == 2)
  138. {
  139. //没有指定的文件
  140. LOG_INFO("没有找到对应的串口 " << com2Str.c_str());
  141. }
  142. else if (error == 5)
  143. {
  144. LOG_INFO("串口被占用 " << com2Str.c_str());
  145. }
  146. }
  147. else
  148. {
  149. LOG_INFO(com2Str.c_str() << " success, handle:" << hPort);
  150. //扎到了可用的串口,设置串口波特率
  151. DCB dcb;
  152. dcb.DCBlength = sizeof(dcb);
  153. GetCommState(hPort, &dcb);
  154. //佳博的串口打印机,默认是这个波特率
  155. dcb.BaudRate = 19200;
  156. if (!SetCommState(hPort, &dcb))
  157. {
  158. LOG_INFO("set baudRate failed!");
  159. CloseHandle(hPort);
  160. continue;
  161. }
  162. //设定通讯端口超时参数
  163. COMMTIMEOUTS tmouts;
  164. tmouts.ReadIntervalTimeout = 100;
  165. tmouts.ReadTotalTimeoutMultiplier = 100;
  166. tmouts.ReadTotalTimeoutConstant = 100;
  167. tmouts.WriteTotalTimeoutConstant = 100;
  168. tmouts.WriteTotalTimeoutMultiplier = 100;
  169. SetCommTimeouts(hPort, &tmouts);
  170. //设置端口缓冲
  171. SetupComm(hPort, 1024, 1024);
  172. //清除通讯端口缓存
  173. PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
  174. //保存端口
  175. PrinterHandle newHandle;
  176. newHandle.hPort = hPort;
  177. newHandle.type = 3;
  178. m_hPorts.push_back(newHandle);
  179. }
  180. }
  181. }
  182. /*
  183. *找到所有可用的小票打印机,包括USB、并口、串口3个类型,注意这里不包含网口
  184. **/
  185. void CPosPrinter::InitShouyin()
  186. {
  187. //开始添加usb
  188. InitUsb();
  189. //开始添加并口的端口
  190. InitBingkou();
  191. //开始添加串口
  192. InitCom();
  193. }
  194. /*
  195. *获取CreateFile的USB端口号
  196. **/
  197. int CPosPrinter::GetDevicePath(LPGUID lpGuid, int usbType)
  198. {
  199. HDEVINFO hDevInfoSet;
  200. SP_DEVINFO_DATA spDevInfoData;
  201. SP_DEVICE_INTERFACE_DATA ifData;
  202. PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
  203. int nCount;
  204. int nTotle;
  205. BOOL bResult;
  206. //这2个字符串,用于根据usb的名字对比是否为打印机设备
  207. wstring strUSBPrint = TEXT("USB 打印支持");
  208. //xp上是英文
  209. wstring strUSBPrint_EN = L"USB Printing Support";
  210. // 取得一个该GUID相关的设备信息集句柄
  211. hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
  212. NULL, // 无关键字
  213. NULL, // 不指定父窗口句柄
  214. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
  215. // 失败...
  216. if(hDevInfoSet == INVALID_HANDLE_VALUE)
  217. {
  218. LOG_INFO("SetupDiGetClassDevs failed \r\n");
  219. return 0;
  220. }
  221. // 申请设备接口数据空间
  222. pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
  223. pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  224. nTotle = -1;
  225. nCount = 0;
  226. bResult = TRUE;
  227. // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
  228. while(bResult)
  229. {
  230. nTotle++;
  231. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  232. // 枚举符合该GUID的设备接口
  233. bResult = ::SetupDiEnumDeviceInfo(
  234. hDevInfoSet, // 设备信息集句柄
  235. (ULONG)nTotle, // 设备信息集里的设备序号
  236. &spDevInfoData); // 设备接口信息
  237. if(bResult)
  238. {
  239. DWORD DataT;
  240. TCHAR buf[MAX_PATH] = { 0 };
  241. DWORD nSize = 0;
  242. // get Friendly Name or Device Description
  243. if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  244. SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  245. {
  246. }
  247. else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  248. SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  249. {
  250. }
  251. else
  252. {
  253. lstrcpy(buf, _T("Unknown"));
  254. }
  255. wstring ws_buf = buf;
  256. //是否是要找的设备类型
  257. if(ws_buf != strUSBPrint && ws_buf != strUSBPrint_EN)
  258. {
  259. LOG_INFO("not good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  260. continue;
  261. }
  262. LOG_INFO("good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  263. ifData.cbSize = sizeof(ifData);
  264. // 枚舉符合該GUID的設備接口
  265. bResult = ::SetupDiEnumDeviceInterfaces(
  266. hDevInfoSet, // 設備信息集句柄
  267. NULL, // 不需額外的設備描述
  268. lpGuid, // GUID
  269. (ULONG)nTotle, // 設備信息集里的設備序號
  270. &ifData); // 設備接口信息
  271. if(bResult)
  272. {
  273. // 取得该设备接口的细节(设备路径)
  274. bResult = SetupDiGetInterfaceDeviceDetail(
  275. hDevInfoSet, // 设备信息集句柄
  276. &ifData, // 设备接口信息
  277. pDetail, // 设备接口细节(设备路径)
  278. INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
  279. NULL, // 不需计算输出缓冲区大小(直接用设定值)
  280. NULL); // 不需额外的设备描述
  281. if(bResult)
  282. {
  283. wstring DevicePath = pDetail->DevicePath;
  284. wstring vid = DevicePath.substr(DevicePath.find(_T("vid_"), 0) + 4, 4);
  285. wstring pid = DevicePath.substr(DevicePath.find(_T("pid_"), 0) + 4, 4);
  286. LOG_INFO("Vid:" << vid.c_str() << ", Pid:" << pid.c_str());
  287. if (usbType == 1 && GetPrinterType(vid, pid) == 2)
  288. {
  289. //标签打印机,暂时不处理打印
  290. LOG_INFO("标签打印机,暂时不打印!");
  291. continue;
  292. }
  293. // 复制设备路径到输出缓冲区
  294. m_usb_devices.push_back(DevicePath);
  295. // 调整计数值
  296. nCount++;
  297. LOG_INFO("Cnt = " << nCount << ",pDetail->DevicePath =" << pDetail->DevicePath);
  298. }
  299. }
  300. }
  301. else
  302. {
  303. //LOG_INFO("SetupDiEnumDeviceInfo FAILED!@");
  304. }
  305. }
  306. // 释放设备接口数据空间
  307. ::GlobalFree(pDetail);
  308. // 关闭设备信息集句柄
  309. ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
  310. return nCount;
  311. }
  312. /*
  313. *返回打印机的类型, 1:普通热敏打印机 2:标签打印机
  314. **/
  315. int CPosPrinter::GetPrinterType(wstring vid, wstring pid)
  316. {
  317. if (vid == L"6868" && pid == L"0500")
  318. {
  319. //佳博标签打印机
  320. return 2;
  321. }
  322. return 1;
  323. }
  324. /*
  325. *打印收银小票
  326. **/
  327. void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
  328. {
  329. //设置模式,后面输出数据不会错
  330. m_type = 1;
  331. std::string printer_leixing = CSetting::GetParam("setting_printer_leixing");
  332. if (printer_leixing == "auto")
  333. {
  334. InitShouyin();
  335. }
  336. else if (printer_leixing == "usb")
  337. {
  338. std::string printer_usb = CSetting::GetParam("setting_printer_usb");
  339. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  340. InitOneUsb(ws_printer_usb);
  341. }
  342. else if (printer_leixing == "bingkou")
  343. {
  344. InitBingkou();
  345. }
  346. else if (printer_leixing == "chuankou")
  347. {
  348. InitCom();
  349. }
  350. else if (printer_leixing == "wangkou")
  351. {
  352. m_type = 2;
  353. std::string wangkou_ip = CSetting::GetParam("setting_printer_wangkou_ip");
  354. //初始化连接
  355. try
  356. {
  357. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(wangkou_ip.c_str()), 9100);
  358. m_socket.connect(ep);
  359. }
  360. catch (std::exception& e)
  361. {
  362. std::string err = e.what();
  363. LOG_INFO("打印机失败,IP地址:" << wangkou_ip.c_str() << ",错误信息:" << err.c_str());
  364. }
  365. }
  366. //读取当前收银打印机的设置
  367. std::string guige = CSetting::GetParam("setting_printer_guige");
  368. std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
  369. int n_lianshu = atoi(lianshu.c_str());
  370. //正式开始打印
  371. for(int i = 0; i < n_lianshu; i++)
  372. {
  373. POS_Reset();
  374. string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
  375. POS_TextOut(shop_name, true, true, 1);
  376. POS_FeedLine();
  377. POS_FeedLine();
  378. bool setting_printer_quhuo_big = false;
  379. if(CSetting::GetParam("setting_printer_quhuo_big") == "1")
  380. {
  381. setting_printer_quhuo_big = true;
  382. }
  383. if(order.m_is_selftake == "1")
  384. {
  385. POS_TextOut("到店自取订单", false, setting_printer_quhuo_big, 1);
  386. POS_FeedLine();
  387. POS_FeedLine();
  388. }
  389. else
  390. {
  391. POS_TextOut("外卖订单", false, setting_printer_quhuo_big, 1);
  392. POS_FeedLine();
  393. }
  394. POS_FeedLine();
  395. //订单来源
  396. string from_type = "订单来源:" + CLewaimaiString::UTF8ToANSI(order.m_from_type);
  397. POS_TextOut(from_type);
  398. POS_FeedLine();
  399. //订单号
  400. bool setting_printer_dingdanhao_big = false;
  401. if(CSetting::GetParam("setting_printer_dingdanhao_big") == "1")
  402. {
  403. setting_printer_dingdanhao_big = true;
  404. }
  405. string order_num = "订 单 号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  406. POS_TextOut(order_num, false, setting_printer_dingdanhao_big);
  407. POS_FeedLine();
  408. //下单时间
  409. bool setting_printer_xiadanshijian_big = false;
  410. if(CSetting::GetParam("setting_printer_xiadanshijian_big") == "1")
  411. {
  412. setting_printer_xiadanshijian_big = true;
  413. }
  414. string order_date = "下单时间:" + CLewaimaiString::UTF8ToANSI(order.m_order_date);
  415. POS_TextOut(order_date, false, setting_printer_xiadanshijian_big);
  416. POS_FeedLine();
  417. //配送时间
  418. bool setting_printer_peisongshijian_big = false;
  419. if(CSetting::GetParam("setting_printer_peisongshijian_big") == "1")
  420. {
  421. setting_printer_peisongshijian_big = true;
  422. }
  423. if(order.m_is_selftake == "1")
  424. {
  425. string date = "自取时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  426. POS_TextOut(date, false, setting_printer_peisongshijian_big);
  427. POS_FeedLine();
  428. }
  429. else
  430. {
  431. string date = "配送时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  432. POS_TextOut(date, false, setting_printer_peisongshijian_big);
  433. POS_FeedLine();
  434. }
  435. //姓名
  436. bool setting_printer_name_big = false;
  437. if(CSetting::GetParam("setting_printer_name_big") == "1")
  438. {
  439. setting_printer_name_big = true;
  440. }
  441. if(order.m_is_selftake != "1")
  442. {
  443. string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
  444. POS_TextOut(name, false, setting_printer_name_big);
  445. POS_FeedLine();
  446. }
  447. //电话
  448. bool setting_printer_phone_big = false;
  449. if(CSetting::GetParam("setting_printer_phone_big") == "1")
  450. {
  451. setting_printer_phone_big = true;
  452. }
  453. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  454. POS_TextOut(phone, false, setting_printer_phone_big);
  455. POS_FeedLine();
  456. //地址
  457. bool setting_printer_address_big = false;
  458. if(CSetting::GetParam("setting_printer_address_big") == "1")
  459. {
  460. setting_printer_address_big = true;
  461. }
  462. if(order.m_is_selftake != "1")
  463. {
  464. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  465. POS_TextOut(address, false, setting_printer_address_big);
  466. POS_FeedLine();
  467. }
  468. //预设选项
  469. if(order.m_order_field.size() > 1)
  470. {
  471. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  472. {
  473. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  474. POS_TextOut(order_field, false, false, 0);
  475. POS_FeedLine();
  476. }
  477. }
  478. //准备开始打印商品详情
  479. string lines;
  480. if(guige == "58")
  481. {
  482. lines = "--------------------------------";
  483. }
  484. else
  485. {
  486. lines = "------------------------------------------------";
  487. }
  488. POS_TextOut(lines);
  489. POS_FeedLine();
  490. //商品标题
  491. bool setting_printer_shangpin_big = false;
  492. if(CSetting::GetParam("setting_printer_shangpin_big") == "1")
  493. {
  494. setting_printer_shangpin_big = true;
  495. }
  496. if(guige == "58")
  497. {
  498. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  499. }
  500. else
  501. {
  502. POS_TextOut("商品 单价 数量 金额", false, setting_printer_shangpin_big);
  503. }
  504. POS_FeedLine();
  505. //商品内容
  506. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  507. {
  508. std::string food_name = (*it).m_food_name;
  509. std::string food_price = (*it).m_item_price;
  510. std::string quantity = (*it).m_quantity;
  511. //计算总价
  512. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  513. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  514. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  515. //先输出商品名字
  516. std::vector<string> m_names;
  517. if(guige == "58")
  518. {
  519. m_names = HandleFoodname(food_name, 1);
  520. }
  521. else
  522. {
  523. m_names = HandleFoodname(food_name, 2);
  524. }
  525. std::string firstLine = m_names[0];
  526. int nGuige;
  527. if(guige == "58")
  528. {
  529. nGuige = 1;
  530. }
  531. else
  532. {
  533. nGuige = 2;
  534. }
  535. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  536. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  537. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  538. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  539. POS_TextOut(firstLineShow, false, setting_printer_shangpin_big);
  540. POS_FeedLine();
  541. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  542. {
  543. POS_TextOut((*it), false, setting_printer_shangpin_big);
  544. POS_FeedLine();
  545. }
  546. }
  547. //判断是否有备注
  548. if(order.m_memo.length() > 0)
  549. {
  550. bool setting_printer_memo_big = false;
  551. if(CSetting::GetParam("setting_printer_memo_big") == "1")
  552. {
  553. setting_printer_memo_big = true;
  554. }
  555. POS_FeedLine();
  556. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  557. POS_TextOut(address, false, setting_printer_memo_big);
  558. POS_FeedLine();
  559. }
  560. //结束商品详情打印
  561. POS_TextOut(lines);
  562. POS_FeedLine();
  563. //开始打印其他的费用
  564. bool is_other_money = false;
  565. bool setting_printer_price_big = false;
  566. if(CSetting::GetParam("setting_printer_price_big") == "1")
  567. {
  568. setting_printer_price_big = true;
  569. }
  570. if(order.m_delivery > 0)
  571. {
  572. string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
  573. POS_TextOut(delivery, false, setting_printer_price_big);
  574. POS_FeedLine();
  575. is_other_money = true;
  576. }
  577. if(order.m_is_dabao == "1")
  578. {
  579. string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
  580. POS_TextOut(dabao, false, setting_printer_price_big);
  581. POS_FeedLine();
  582. is_other_money = true;
  583. }
  584. //开始显示增值服务费
  585. if(order.m_addservie.size() > 0)
  586. {
  587. for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
  588. {
  589. string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
  590. POS_TextOut(addservice, false, setting_printer_price_big);
  591. POS_FeedLine();
  592. is_other_money = true;
  593. }
  594. is_other_money = true;
  595. }
  596. if(order.m_discount < 10)
  597. {
  598. string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
  599. POS_TextOut(discount, false, setting_printer_price_big);
  600. POS_FeedLine();
  601. is_other_money = true;
  602. }
  603. if(order.m_is_member_discount == "1")
  604. {
  605. string member_discount = "会员优惠:-" + order.m_member_discount;
  606. POS_TextOut(member_discount, false, setting_printer_price_big);
  607. POS_FeedLine();
  608. is_other_money = true;
  609. }
  610. if(order.m_is_firstcut == "1")
  611. {
  612. string firstcut = "首单优惠:-" + order.m_firstcut_value;
  613. POS_TextOut(firstcut, false, setting_printer_price_big);
  614. POS_FeedLine();
  615. is_other_money = true;
  616. }
  617. //满减
  618. string::size_type position = order.m_promotion.find(":");
  619. if(position != order.m_promotion.npos)
  620. {
  621. string promotion_value;
  622. promotion_value.assign(order.m_promotion, position + 1);
  623. string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
  624. POS_TextOut(promotion, false, setting_printer_price_big);
  625. POS_FeedLine();
  626. is_other_money = true;
  627. }
  628. if(order.m_coupon > 0)
  629. {
  630. string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
  631. POS_TextOut(coupon, false, setting_printer_price_big);
  632. POS_FeedLine();
  633. is_other_money = true;
  634. }
  635. if(is_other_money)
  636. {
  637. POS_TextOut(lines);
  638. POS_FeedLine();
  639. }
  640. //最后显示总价
  641. POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, setting_printer_price_big, 2);
  642. POS_FeedLine();
  643. //显示付款方式
  644. bool setting_printer_pay_big = false;
  645. if(CSetting::GetParam("setting_printer_pay_big") == "1")
  646. {
  647. setting_printer_pay_big = true;
  648. }
  649. POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, setting_printer_pay_big, 2);
  650. POS_FeedLine();
  651. //走纸几行再切
  652. POS_FeedLine();
  653. POS_FeedLine();
  654. POS_FeedLine();
  655. POS_FeedLine();
  656. POS_FeedLine();
  657. POS_CutPaper();
  658. }
  659. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  660. POS_Reset();
  661. //关闭设备
  662. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  663. {
  664. CloseHandle((*it).hPort);
  665. }
  666. }
  667. void CPosPrinter::PrintWaimaiOrderBiaoqian(CWaimaiOrder& order)
  668. {
  669. m_type = 1;
  670. std::string printer_usb = CSetting::GetParam("setting_biaoqian_printer_usb");
  671. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  672. //连接usb端口
  673. InitOneUsb(ws_printer_usb);
  674. //初始化标签打印机
  675. BIAOQIAN_Reset();
  676. std::string restaurant_number = order.m_restaurant_number;
  677. std::string order_num = "订单号:" + order.m_order_num;
  678. std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
  679. //先计算商品的总数量
  680. int foodNum = 0;
  681. for (std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  682. {
  683. std::string quantity = (*it).m_quantity;
  684. foodNum += atoi(quantity.c_str());
  685. }
  686. //商品内容
  687. int curFoodNum = 0;
  688. for (std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  689. {
  690. std::string food_name = (*it).m_food_name;
  691. std::string food_price = (*it).m_item_price;
  692. std::string quantity = (*it).m_quantity;
  693. //每份商品打印一个标签
  694. int nQuantity = atoi(quantity.c_str());
  695. for (int i = 0; i < nQuantity; i++)
  696. {
  697. curFoodNum++;
  698. std::string order_num_info = "外卖 #" + restaurant_number;
  699. BIAOQIAN_TEXTOUT(order_num_info, 16, 16, 1, 1);
  700. std::string numInfo = to_string(curFoodNum) + "/" + to_string(foodNum);
  701. BIAOQIAN_TEXTOUT(numInfo, 264, 16, 1, 1);
  702. //每行最多显示12个汉字,这里要计算一下换行(要先转成ANSI格式)
  703. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  704. std::vector<std::string> foodNameVector = HandleBiaoqianFoodname(food_name);
  705. int nRow = 0;
  706. //考虑是否用大号字体
  707. bool is_big_name = false;
  708. if (foodNameVector.size() <= 2)
  709. {
  710. //不超过2行,可以用大的字体
  711. is_big_name = true;
  712. }
  713. for (std::vector<std::string>::iterator it = foodNameVector.begin(); it != foodNameVector.end(); it++)
  714. {
  715. if (is_big_name)
  716. {
  717. BIAOQIAN_TEXTOUT(*it, 16, 56 + 64 * nRow, 1, 2);
  718. }
  719. else
  720. {
  721. BIAOQIAN_TEXTOUT(*it, 16, 56 + 32 * nRow, 1, 1);
  722. }
  723. nRow++;
  724. }
  725. //打印价格
  726. std::string priceInfo = food_price + "元";
  727. BIAOQIAN_TEXTOUT(priceInfo, 16, 180, 1, 1);
  728. //打印订单号
  729. BIAOQIAN_TEXTOUT(order_num, 16, 210, 1, 1);
  730. BIAOQIAN_PRINT();
  731. BIAOQIAN_CLS();
  732. }
  733. }
  734. }
  735. void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
  736. {
  737. //设置模式,后面输出数据不会错
  738. m_type = 2;
  739. //读取厨房打印机信息
  740. std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
  741. for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
  742. {
  743. ChufangPrinter printer = *it;
  744. std::string ip = printer.ip;
  745. //初始化连接
  746. try
  747. {
  748. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
  749. m_socket.connect(ep);
  750. }
  751. catch(std::exception& e)
  752. {
  753. std::string err = e.what();
  754. LOG_INFO("连接厨房打印机失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
  755. //连接失败了,处理下一个厨房打印机
  756. continue;
  757. }
  758. std::string guige = printer.guige;
  759. std::string fendan = printer.fendan;
  760. std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
  761. if(fendan == "0")
  762. {
  763. POS_Reset();
  764. POS_TextOut("派工单", true, true, 1);
  765. POS_FeedLine();
  766. POS_FeedLine();
  767. string order_type = "#" + order.m_restaurant_number + " ";
  768. if (order.m_is_selftake == "1")
  769. {
  770. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  771. POS_FeedLine();
  772. POS_FeedLine();
  773. }
  774. else
  775. {
  776. POS_TextOut(order_type + "外卖订单", false, false, 1);
  777. POS_FeedLine();
  778. POS_FeedLine();
  779. }
  780. //预设选项
  781. if(order.m_order_field.size() > 1)
  782. {
  783. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  784. {
  785. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  786. POS_TextOut(order_field, false, false, 0);
  787. POS_FeedLine();
  788. }
  789. }
  790. //准备开始打印商品详情
  791. string lines;
  792. if(guige == "58")
  793. {
  794. lines = "--------------------------------";
  795. }
  796. else
  797. {
  798. lines = "------------------------------------------------";
  799. }
  800. POS_TextOut(lines);
  801. POS_FeedLine();
  802. //商品标题
  803. if(guige == "58")
  804. {
  805. POS_TextOut("商品 单价 数量 金额");
  806. }
  807. else
  808. {
  809. POS_TextOut("商品 单价 数量 金额");
  810. }
  811. POS_FeedLine();
  812. //商品内容
  813. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  814. {
  815. std::string food_name = (*it).m_food_name;
  816. std::string food_price = (*it).m_item_price;
  817. std::string quantity = (*it).m_quantity;
  818. //计算总价
  819. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  820. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  821. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  822. //先输出商品名字
  823. std::vector<string> m_names;
  824. if(guige == "58")
  825. {
  826. m_names = HandleFoodname(food_name, 1);
  827. }
  828. else
  829. {
  830. m_names = HandleFoodname(food_name, 2);
  831. }
  832. std::string firstLine = m_names[0];
  833. int nGuige;
  834. if(guige == "58")
  835. {
  836. nGuige = 1;
  837. }
  838. else
  839. {
  840. nGuige = 2;
  841. }
  842. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  843. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  844. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  845. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  846. POS_TextOut(firstLineShow, false, false, 0);
  847. POS_FeedLine();
  848. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  849. {
  850. POS_TextOut((*it), false, false, 0);
  851. POS_FeedLine();
  852. }
  853. }
  854. //判断是否有备注
  855. if(order.m_memo.length() > 0)
  856. {
  857. POS_FeedLine();
  858. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  859. POS_TextOut(address, false, false, 0);
  860. POS_FeedLine();
  861. }
  862. //结束商品详情打印
  863. POS_TextOut(lines);
  864. POS_FeedLine();
  865. POS_FeedLine();
  866. POS_FeedLine();
  867. POS_FeedLine();
  868. POS_FeedLine();
  869. POS_FeedLine();
  870. POS_CutPaper();
  871. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  872. POS_Reset();
  873. }
  874. else
  875. {
  876. POS_Reset();
  877. //分单模式下,每个商品打印一张单
  878. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  879. {
  880. POS_TextOut("派工单", true, true, 1);
  881. POS_FeedLine();
  882. POS_FeedLine();
  883. string order_type = "#" + order.m_restaurant_number + " ";
  884. if (order.m_is_selftake == "1")
  885. {
  886. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  887. POS_FeedLine();
  888. POS_FeedLine();
  889. }
  890. else
  891. {
  892. POS_TextOut(order_type + "外卖订单", false, false, 1);
  893. POS_FeedLine();
  894. POS_FeedLine();
  895. }
  896. //预设选项
  897. if(order.m_order_field.size() > 1)
  898. {
  899. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  900. {
  901. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  902. POS_TextOut(order_field, false, false, 0);
  903. POS_FeedLine();
  904. }
  905. }
  906. //准备开始打印商品详情
  907. string lines;
  908. if(guige == "58")
  909. {
  910. lines = "--------------------------------";
  911. }
  912. else
  913. {
  914. lines = "------------------------------------------------";
  915. }
  916. POS_TextOut(lines);
  917. POS_FeedLine();
  918. //商品标题
  919. if(guige == "58")
  920. {
  921. POS_TextOut("商品 单价 数量 金额");
  922. }
  923. else
  924. {
  925. POS_TextOut("商品 单价 数量 金额");
  926. }
  927. POS_FeedLine();
  928. std::string food_name = (*it).m_food_name;
  929. std::string food_price = (*it).m_item_price;
  930. std::string quantity = (*it).m_quantity;
  931. //计算总价
  932. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  933. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  934. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  935. //先输出商品名字
  936. std::vector<string> m_names;
  937. if(guige == "58")
  938. {
  939. m_names = HandleFoodname(food_name, 1);
  940. }
  941. else
  942. {
  943. m_names = HandleFoodname(food_name, 2);
  944. }
  945. std::string firstLine = m_names[0];
  946. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  947. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  948. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  949. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  950. POS_TextOut(firstLineShow, false, false, 0);
  951. POS_FeedLine();
  952. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  953. {
  954. POS_TextOut((*it), false, false, 0);
  955. POS_FeedLine();
  956. }
  957. //判断是否有备注
  958. if(order.m_memo.length() > 0)
  959. {
  960. POS_FeedLine();
  961. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  962. POS_TextOut(address, false, false, 0);
  963. POS_FeedLine();
  964. }
  965. //结束商品详情打印
  966. POS_TextOut(lines);
  967. POS_FeedLine();
  968. POS_FeedLine();
  969. POS_FeedLine();
  970. POS_FeedLine();
  971. POS_FeedLine();
  972. POS_FeedLine();
  973. POS_CutPaper();
  974. }
  975. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  976. POS_Reset();
  977. }
  978. m_socket.close();
  979. }
  980. }
  981. std::vector<std::wstring> CPosPrinter::getUsbDevices()
  982. {
  983. return m_usb_devices;
  984. }
  985. int CPosPrinter::WriteData(string msg)
  986. {
  987. return WriteBuf(msg.c_str(), msg.length());
  988. }
  989. int CPosPrinter::WriteBuf(const char* buf, int len)
  990. {
  991. if(m_type == 1)
  992. {
  993. DWORD dwWrite;
  994. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  995. {
  996. HANDLE hPort = (*it).hPort;
  997. if((*it).type == 3)
  998. {
  999. //串口,同步写数据
  1000. //LOG_INFO("before com writefile handle:" << hPort);
  1001. WriteFile(hPort, buf, len, &dwWrite, NULL);
  1002. //LOG_INFO("after com writefile handle:" << hPort);
  1003. }
  1004. else
  1005. {
  1006. //usb和并口,异步写数据
  1007. OVERLAPPED overlap;
  1008. memset(&overlap, 0, sizeof(overlap));
  1009. BOOL rc = WriteFile(hPort, buf, len, &dwWrite, &overlap);
  1010. if(rc)
  1011. {
  1012. //LOG_INFO("writefile success immediately, handle:" << hPort);
  1013. }
  1014. else
  1015. {
  1016. if(GetLastError() == ERROR_IO_PENDING)
  1017. {
  1018. //LOG_INFO("Request queued, waiting... handle:" << hPort);
  1019. WaitForSingleObject(hPort, 1000);
  1020. //LOG_INFO("Request completed, handle:" << hPort);
  1021. //计算写入了多少字节的数据
  1022. DWORD numread;
  1023. rc = GetOverlappedResult(
  1024. hPort,
  1025. &overlap,
  1026. &numread,
  1027. FALSE
  1028. );
  1029. //LOG_INFO("Write success " << numread << " bytes");
  1030. }
  1031. }
  1032. }
  1033. }
  1034. return 1;
  1035. }
  1036. else
  1037. {
  1038. try
  1039. {
  1040. m_socket.write_some(boost::asio::buffer(buf, len));
  1041. }
  1042. catch(const std::exception& e)
  1043. {
  1044. LOG_INFO(e.what());
  1045. }
  1046. return 0;
  1047. }
  1048. }
  1049. /*
  1050. *测试打印机是否连接
  1051. **/
  1052. bool CPosPrinter::PortTest(HANDLE hPort)
  1053. {
  1054. //标签打印机的查询状态指令
  1055. char chInitCode[2] = { 0x1b, 0x40};
  1056. DWORD dwWrite;
  1057. OVERLAPPED overlap;
  1058. memset(&overlap, 0, sizeof(overlap));
  1059. BOOL rc = WriteFile(hPort, chInitCode, 2, &dwWrite, &overlap);
  1060. if(rc)
  1061. {
  1062. LOG_INFO("writefile success immediately, handle:" << hPort);
  1063. return true;
  1064. }
  1065. else
  1066. {
  1067. if(GetLastError() == ERROR_IO_PENDING)
  1068. {
  1069. DWORD ret = WaitForSingleObject(hPort, 1000);
  1070. if(ret == 0)
  1071. {
  1072. LOG_INFO("printer is connect, handle:" << hPort);
  1073. return true;
  1074. }
  1075. else
  1076. {
  1077. LOG_INFO("printer is not connect, handle:" << hPort);
  1078. }
  1079. }
  1080. }
  1081. return false;
  1082. }
  1083. int CPosPrinter::POS_Reset(void)
  1084. {
  1085. char s[2] = {0x1B, 0x40};
  1086. WriteBuf(s, 2);
  1087. return 0;
  1088. }
  1089. int CPosPrinter::POS_FeedLine(void)
  1090. {
  1091. char s[1] = {0x0A};
  1092. WriteBuf(s, 1);
  1093. return 0;
  1094. }
  1095. int CPosPrinter::POS_Feed(void)
  1096. {
  1097. char s[3] = { 0x1B, 0x4A, 0x00 };
  1098. WriteBuf(s, 3);
  1099. return 0;
  1100. }
  1101. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  1102. {
  1103. string s;
  1104. s = "\x1D\x50\xB4\xB4";
  1105. WriteData(s);
  1106. s = "\x1B\x53";
  1107. WriteData(s);
  1108. return 0;
  1109. }
  1110. int CPosPrinter::POS_SET_MOVE_X()
  1111. {
  1112. char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
  1113. WriteBuf(s2, 6);
  1114. return 0;
  1115. }
  1116. int CPosPrinter::POS_MOVE_X()
  1117. {
  1118. char s[1] = { 0x09 };
  1119. WriteBuf(s, 1);
  1120. return 0;
  1121. }
  1122. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  1123. {
  1124. char cx = (char)(x);
  1125. char cy = (char)(y);
  1126. char s1[4] = { 0x1B, 0x24, cx, cy };
  1127. WriteBuf(s1, 4);
  1128. return 0;
  1129. }
  1130. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  1131. {
  1132. char cx = (char)(x);
  1133. char cy = (char)(y);
  1134. char s1[4] = { 0x1D, 0x57, cx, cy };
  1135. WriteBuf(s1, 4);
  1136. return 0;
  1137. }
  1138. /*
  1139. *align_type:0 左对齐 1 居中对齐 2右对齐
  1140. **/
  1141. int CPosPrinter::POS_TextOut(string abc, bool is_double_width, bool is_double_height, int align_type)
  1142. {
  1143. if(is_double_width && is_double_height)
  1144. {
  1145. char s1[3] = { 0x1B, 0x21, 0x30 };
  1146. WriteBuf(s1, 3);
  1147. char s2[3] = { 0x1C, 0x21, 0x0c };
  1148. WriteBuf(s2, 3);
  1149. }
  1150. else if(is_double_width && !is_double_height)
  1151. {
  1152. char s1[3] = { 0x1B, 0x21, 0x20 };
  1153. WriteBuf(s1, 3);
  1154. char s2[3] = { 0x1C, 0x21, 0x04 };
  1155. WriteBuf(s2, 3);
  1156. }
  1157. else if(!is_double_width && is_double_height)
  1158. {
  1159. char s1[3] = { 0x1B, 0x21, 0x10 };
  1160. WriteBuf(s1, 3);
  1161. char s2[3] = { 0x1C, 0x21, 0x08 };
  1162. WriteBuf(s2, 3);
  1163. }
  1164. else
  1165. {
  1166. char s1[3] = { 0x1B, 0x21, 0x00 };
  1167. WriteBuf(s1, 3);
  1168. char s2[3] = { 0x1C, 0x57, 0x00 };
  1169. WriteBuf(s2, 3);
  1170. }
  1171. if(align_type == 0)
  1172. {
  1173. char s1[3] = { 0x1B, 0x61, 0x00 };
  1174. WriteBuf(s1, 3);
  1175. }
  1176. else if(align_type == 1)
  1177. {
  1178. char s1[3] = { 0x1B, 0x61, 0x01 };
  1179. WriteBuf(s1, 3);
  1180. }
  1181. else if(align_type == 2)
  1182. {
  1183. char s1[3] = { 0x1B, 0x61, 0x02 };
  1184. WriteBuf(s1, 3);
  1185. }
  1186. else
  1187. {
  1188. }
  1189. WriteData(abc);
  1190. return 0;
  1191. }
  1192. /*
  1193. *打印机切纸,直接切不走纸
  1194. **/
  1195. int CPosPrinter::POS_CutPaper()
  1196. {
  1197. char s[4] = { 0x1D, 0x56, 0x01};
  1198. WriteBuf(s, 3);
  1199. return 0;
  1200. }
  1201. int CPosPrinter::POS_OutQRCode()
  1202. {
  1203. char QRCode1[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x05 };
  1204. char QRCode2[16] = { 0x1d, 0x28, 0x6b, 0x0b, 0x00, 0x31, 0x50, 0x30, 0x47, 0x70, 0x72, 0x69,
  1205. 0x6e, 0x74, 0x65, 0x72
  1206. };
  1207. char QRCode3[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30 };
  1208. WriteBuf(QRCode1, 8);
  1209. WriteBuf(QRCode2, 16);
  1210. WriteBuf(QRCode3, 8);
  1211. return 0;
  1212. }
  1213. void CPosPrinter::BIAOQIAN_Reset()
  1214. {
  1215. char endTag[3] = {0x0d, 0x0a, 0x00};
  1216. std::string size = "SIZE 40 mm, 30 mm";
  1217. size += endTag;
  1218. std::string gap = "GAP 2 mm,0 mm";
  1219. gap += endTag;
  1220. std::string referent = "REFERENCE 0,0";
  1221. referent += endTag;
  1222. std::string speed = "SPEED 2.0";
  1223. speed += endTag;
  1224. std::string density = "DENSITY 8";
  1225. density += endTag;
  1226. std::string direction = "DIRECTION 0";
  1227. direction += endTag;
  1228. std::string REFERENCE = "REFERENCE 0,0";
  1229. REFERENCE += endTag;
  1230. std::string PEER = "SET PEEL OFF";
  1231. PEER += endTag;
  1232. std::string TEAR = "SET TEAR ON";
  1233. TEAR += endTag;
  1234. std::string KEY = "SET PRINTKEY OFF";
  1235. KEY += endTag;
  1236. std::string cls = "CLS";
  1237. cls += endTag;
  1238. std::string data = size + gap + referent + speed + density + direction + REFERENCE + PEER + TEAR + KEY + cls;
  1239. WriteData(data);
  1240. }
  1241. /*
  1242. *前进一张纸
  1243. **/
  1244. void CPosPrinter::BIAOQIAN_FORMFEED()
  1245. {
  1246. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1247. std::string formfeed = "FORMFEED";
  1248. formfeed += endTag;
  1249. WriteData(formfeed);
  1250. }
  1251. void CPosPrinter::BIAOQIAN_TEXTOUT(std::string content, int x, int y, int x_multiplication, int y_multiplication)
  1252. {
  1253. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1254. string text = "TEXT ";
  1255. text += to_string(x) + "," + to_string(y) + ",";
  1256. text += "\"TSS24.BF2\",";
  1257. text += to_string(0) + ",";
  1258. text += to_string(x_multiplication) + "," + to_string(y_multiplication) + ",";
  1259. text += "\"" + content + "\"";
  1260. text += endTag;
  1261. WriteData(text);
  1262. }
  1263. void CPosPrinter::BIAOQIAN_PRINT()
  1264. {
  1265. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1266. std::string print = "PRINT 1";
  1267. print += endTag;
  1268. WriteData(print);
  1269. }
  1270. void CPosPrinter::BIAOQIAN_CLS()
  1271. {
  1272. char endTag[3] = { 0x0d, 0x0a, 0x00 };
  1273. std::string cls = "CLS";
  1274. cls += endTag;
  1275. WriteData(cls);
  1276. }
  1277. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  1278. {
  1279. nHanzi = 0;
  1280. nZimu = 0;
  1281. const char* buffer = s.c_str();
  1282. while(*buffer != '\0')
  1283. {
  1284. if(!(*buffer >= 0 && *buffer <= 127))
  1285. {
  1286. //汉字
  1287. buffer++;
  1288. buffer++;
  1289. nHanzi++;
  1290. }
  1291. else
  1292. {
  1293. //字母
  1294. buffer++;
  1295. nZimu++;
  1296. }
  1297. }
  1298. }
  1299. /*
  1300. *规格 1:58mm 2:80mm
  1301. **/
  1302. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  1303. {
  1304. std::vector<std::string> newnameArray;
  1305. int nHanzi, nZimu;
  1306. CalWord(oldname, nHanzi, nZimu);
  1307. int nWidth = nHanzi * 2 + nZimu;
  1308. int maxWidth;
  1309. if(guige == 1)
  1310. {
  1311. maxWidth = 15;
  1312. }
  1313. else
  1314. {
  1315. maxWidth = 28;
  1316. }
  1317. if(nWidth <= maxWidth)
  1318. {
  1319. //对于nWidth补空格
  1320. for(int i = 0; i < maxWidth - nWidth; i++)
  1321. {
  1322. oldname += " ";
  1323. }
  1324. newnameArray.push_back(oldname);
  1325. return newnameArray;
  1326. }
  1327. //宽度大于15的情况,如果超过了,就要进行换行截取
  1328. const char* s = oldname.c_str();
  1329. int nTmp = 0;
  1330. int nTotal = 0;
  1331. while(*s != '\0')
  1332. {
  1333. if(!(*s >= 0 && *s <= 127))
  1334. {
  1335. //汉字的情况
  1336. s++;
  1337. nTmp++;
  1338. nTotal++;
  1339. s++;
  1340. nTmp++;
  1341. nTotal++;
  1342. }
  1343. else
  1344. {
  1345. //字母的情况
  1346. s++;
  1347. nTmp++;
  1348. nTotal++;
  1349. }
  1350. if(nTmp == maxWidth)
  1351. {
  1352. //这里开始要换行了
  1353. string newnameItem;
  1354. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1355. newnameArray.push_back(newnameItem);
  1356. nTmp = 0;
  1357. }
  1358. else if(nTmp == maxWidth - 1)
  1359. {
  1360. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1361. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1362. {
  1363. //下一个是中文,也要换行了,补齐一个空格
  1364. string newnameItem;
  1365. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1366. newnameItem += " ";
  1367. newnameArray.push_back(newnameItem);
  1368. nTmp = 0;
  1369. }
  1370. }
  1371. }
  1372. if(nTmp > 0)
  1373. {
  1374. //处理分隔后的最后一行
  1375. string newnameItem;
  1376. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1377. for(int i = 0; i < maxWidth - nTmp; i++)
  1378. {
  1379. newnameItem += " ";
  1380. }
  1381. newnameArray.push_back(newnameItem);
  1382. }
  1383. return newnameArray;
  1384. }
  1385. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  1386. {
  1387. double price = atof(oldprice.c_str());
  1388. if(price < 10.00)
  1389. {
  1390. //单位数
  1391. if(guige == 1)
  1392. {
  1393. return " " + oldprice;
  1394. }
  1395. else
  1396. {
  1397. return " " + oldprice;
  1398. }
  1399. }
  1400. else if(price > 9.99 && price < 100.00)
  1401. {
  1402. //双位数
  1403. if(guige == 1)
  1404. {
  1405. return " " + oldprice;
  1406. }
  1407. else
  1408. {
  1409. return " " + oldprice;
  1410. }
  1411. }
  1412. else if(price > 99.99 && price < 1000.00)
  1413. {
  1414. //三位数
  1415. if(guige == 1)
  1416. {
  1417. return " " + oldprice;
  1418. }
  1419. else
  1420. {
  1421. return " " + oldprice;
  1422. }
  1423. }
  1424. else
  1425. {
  1426. //四位数
  1427. if(guige == 1)
  1428. {
  1429. return " " + oldprice;
  1430. }
  1431. else
  1432. {
  1433. return " " + oldprice;
  1434. }
  1435. }
  1436. }
  1437. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  1438. {
  1439. int n = atoi(oldquantity.c_str());
  1440. if(n < 10)
  1441. {
  1442. if(guige == 1)
  1443. {
  1444. return " " + oldquantity;
  1445. }
  1446. else
  1447. {
  1448. return " " + oldquantity;
  1449. }
  1450. }
  1451. else if(n >= 10 && n <= 99)
  1452. {
  1453. if(guige == 1)
  1454. {
  1455. return " " + oldquantity;
  1456. }
  1457. else
  1458. {
  1459. return " " + oldquantity;
  1460. }
  1461. }
  1462. else
  1463. {
  1464. if(guige == 1)
  1465. {
  1466. return "" + oldquantity;
  1467. }
  1468. else
  1469. {
  1470. return " " + oldquantity;
  1471. }
  1472. }
  1473. }
  1474. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  1475. {
  1476. double price = atof(oldprice.c_str());
  1477. if(price < 10.00)
  1478. {
  1479. //单位数
  1480. if(guige == 1)
  1481. {
  1482. return " " + oldprice;
  1483. }
  1484. else
  1485. {
  1486. return " " + oldprice;
  1487. }
  1488. }
  1489. else if(price > 9.99 && price < 100.00)
  1490. {
  1491. //双位数
  1492. if(guige == 1)
  1493. {
  1494. return " " + oldprice;
  1495. }
  1496. else
  1497. {
  1498. return " " + oldprice;
  1499. }
  1500. }
  1501. else if(price > 99.99 && price < 1000.00)
  1502. {
  1503. //三位数
  1504. if(guige == 1)
  1505. {
  1506. return " " + oldprice;
  1507. }
  1508. else
  1509. {
  1510. return " " + oldprice;
  1511. }
  1512. }
  1513. else
  1514. {
  1515. //四位数
  1516. if(guige == 1)
  1517. {
  1518. return " " + oldprice;
  1519. }
  1520. else
  1521. {
  1522. return " " + oldprice;
  1523. }
  1524. }
  1525. }
  1526. std::vector<std::string> CPosPrinter::HandleBiaoqianFoodname(std::string oldname)
  1527. {
  1528. std::vector<std::string> newnameArray;
  1529. int nHanzi, nZimu;
  1530. CalWord(oldname, nHanzi, nZimu);
  1531. int nWidth = nHanzi * 2 + nZimu;
  1532. //40 *30mmm的标签,宽度最大24
  1533. int maxWidth = 24;
  1534. if (nWidth <= maxWidth)
  1535. {
  1536. //对于nWidth补空格
  1537. for (int i = 0; i < maxWidth - nWidth; i++)
  1538. {
  1539. oldname += " ";
  1540. }
  1541. newnameArray.push_back(oldname);
  1542. return newnameArray;
  1543. }
  1544. //宽度大于15的情况,如果超过了,就要进行换行截取
  1545. const char* s = oldname.c_str();
  1546. int nTmp = 0;
  1547. int nTotal = 0;
  1548. while (*s != '\0')
  1549. {
  1550. if (!(*s >= 0 && *s <= 127))
  1551. {
  1552. //汉字的情况
  1553. s++;
  1554. nTmp++;
  1555. nTotal++;
  1556. s++;
  1557. nTmp++;
  1558. nTotal++;
  1559. }
  1560. else
  1561. {
  1562. //字母的情况
  1563. s++;
  1564. nTmp++;
  1565. nTotal++;
  1566. }
  1567. if (nTmp == maxWidth)
  1568. {
  1569. //这里开始要换行了
  1570. string newnameItem;
  1571. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1572. newnameArray.push_back(newnameItem);
  1573. nTmp = 0;
  1574. }
  1575. else if (nTmp == maxWidth - 1)
  1576. {
  1577. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1578. if (!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1579. {
  1580. //下一个是中文,也要换行了,补齐一个空格
  1581. string newnameItem;
  1582. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1583. newnameItem += " ";
  1584. newnameArray.push_back(newnameItem);
  1585. nTmp = 0;
  1586. }
  1587. }
  1588. }
  1589. if (nTmp > 0)
  1590. {
  1591. //处理分隔后的最后一行
  1592. string newnameItem;
  1593. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1594. for (int i = 0; i < maxWidth - nTmp; i++)
  1595. {
  1596. newnameItem += " ";
  1597. }
  1598. newnameArray.push_back(newnameItem);
  1599. }
  1600. return newnameArray;
  1601. }