CPosPrinter.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  1. #include "../pch/pch.h"
  2. #include "CPosPrinter.h"
  3. #include <winioctl.h>
  4. #include <setupapi.h>
  5. using boost::asio::ip::tcp;
  6. CPosPrinter::CPosPrinter() : m_socket(m_io)
  7. {
  8. }
  9. CPosPrinter::~CPosPrinter()
  10. {
  11. }
  12. void CPosPrinter::InitUsb()
  13. {
  14. //设置中文字符
  15. setlocale(LC_CTYPE, "chs");
  16. //取设备路径
  17. int nDevice = GetDevicePath((LPGUID)&USB_GUID);
  18. LOG_INFO("可用的USB打印机数量:" << nDevice);
  19. //添加usb端口
  20. int i = 0;
  21. while(i < nDevice)
  22. {
  23. LOG_INFO("准备打开端口 Port = " << m_usb_devices[i].c_str());
  24. HANDLE hPort = CreateFile(m_usb_devices[i].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  25. if(hPort == INVALID_HANDLE_VALUE)
  26. {
  27. // 打开端口失败
  28. DWORD error = GetLastError();
  29. if(error == 2)
  30. {
  31. //没有指定的文件
  32. LOG_INFO("没有找对对应的usb端口");
  33. }
  34. else if(error == 5)
  35. {
  36. LOG_INFO("usb端口被占用!");
  37. }
  38. continue;
  39. }
  40. LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
  41. if(PortTest(hPort) == true)
  42. {
  43. //端口测试连通,保存起来
  44. PrinterHandle newHandle;
  45. newHandle.hPort = hPort;
  46. newHandle.type = 1;
  47. m_hPorts.push_back(newHandle);
  48. }
  49. else
  50. {
  51. //端口打印机没有连接,那么就直接关闭掉并口,避免占用
  52. CloseHandle(hPort);
  53. }
  54. i++;
  55. }
  56. }
  57. void CPosPrinter::InitOneUsb(wstring usb_path)
  58. {
  59. HANDLE hPort = CreateFile(usb_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  60. if(hPort == INVALID_HANDLE_VALUE)
  61. {
  62. // 打开端口失败
  63. DWORD error = GetLastError();
  64. if(error == 2)
  65. {
  66. //没有指定的文件
  67. LOG_INFO("没有找对对应的usb端口");
  68. }
  69. else if(error == 5)
  70. {
  71. LOG_INFO("usb端口被占用!");
  72. }
  73. }
  74. LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
  75. if(PortTest(hPort) == true)
  76. {
  77. //端口测试连通,保存起来
  78. PrinterHandle newHandle;
  79. newHandle.hPort = hPort;
  80. newHandle.type = 1;
  81. m_hPorts.push_back(newHandle);
  82. }
  83. else
  84. {
  85. //端口打印机没有连接,那么就直接关闭掉并口,避免占用
  86. CloseHandle(hPort);
  87. }
  88. }
  89. void CPosPrinter::InitBingkou()
  90. {
  91. std::wstring LptStr = L"lpt1";
  92. HANDLE hPort = CreateFile(LptStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
  93. if(hPort == INVALID_HANDLE_VALUE)
  94. {
  95. DWORD error = GetLastError();
  96. if(error == 2)
  97. {
  98. //没有指定的文件
  99. LOG_INFO("本机器没有并口!");
  100. }
  101. else if(error == 5)
  102. {
  103. LOG_INFO("并口被占用!");
  104. }
  105. }
  106. else
  107. {
  108. //这个表示并口可以使用
  109. LOG_INFO("找到并口,准备进行打印机检测! hPort:" << hPort);
  110. if(PortTest(hPort) == true)
  111. {
  112. //并口测试连通,保存起来
  113. PrinterHandle newHandle;
  114. newHandle.hPort = hPort;
  115. newHandle.type = 2;
  116. m_hPorts.push_back(newHandle);
  117. }
  118. else
  119. {
  120. //并口打印机没有连接,那么就直接关闭掉并口,避免占用
  121. CloseHandle(hPort);
  122. }
  123. }
  124. }
  125. void CPosPrinter::InitCom()
  126. {
  127. int comNum = 10;
  128. for(int i = 1; i <= 10; i++)
  129. {
  130. std::wstring com2Str = L"com" + CLewaimaiString::ANSIToUnicode(to_string(i));
  131. HANDLE hPort = CreateFile(com2Str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  132. if(hPort == INVALID_HANDLE_VALUE)
  133. {
  134. DWORD error = GetLastError();
  135. if(error == 2)
  136. {
  137. //没有指定的文件
  138. LOG_INFO("没有找到对应的串口 " << com2Str.c_str());
  139. }
  140. else if(error == 5)
  141. {
  142. LOG_INFO("串口被占用 " << com2Str.c_str());
  143. }
  144. }
  145. else
  146. {
  147. LOG_INFO(com2Str.c_str() << " success, handle:" << hPort);
  148. //扎到了可用的串口,设置串口波特率
  149. DCB dcb;
  150. dcb.DCBlength = sizeof(dcb);
  151. GetCommState(hPort, &dcb);
  152. //佳博的串口打印机,默认是这个波特率
  153. dcb.BaudRate = 19200;
  154. if(!SetCommState(hPort, &dcb))
  155. {
  156. LOG_INFO("set baudRate failed!");
  157. CloseHandle(hPort);
  158. continue;
  159. }
  160. //设定通讯端口超时参数
  161. COMMTIMEOUTS tmouts;
  162. tmouts.ReadIntervalTimeout = 100;
  163. tmouts.ReadTotalTimeoutMultiplier = 100;
  164. tmouts.ReadTotalTimeoutConstant = 100;
  165. tmouts.WriteTotalTimeoutConstant = 100;
  166. tmouts.WriteTotalTimeoutMultiplier = 100;
  167. SetCommTimeouts(hPort, &tmouts);
  168. //设置端口缓冲
  169. SetupComm(hPort, 1024, 1024);
  170. //清除通讯端口缓存
  171. PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
  172. //保存端口
  173. PrinterHandle newHandle;
  174. newHandle.hPort = hPort;
  175. newHandle.type = 3;
  176. m_hPorts.push_back(newHandle);
  177. }
  178. }
  179. }
  180. /*
  181. *找到所有可用的小票打印机,包括USB、并口、串口3个类型,注意这里不包含网口
  182. **/
  183. void CPosPrinter::InitShouyin()
  184. {
  185. //开始添加usb
  186. InitUsb();
  187. //开始添加并口的端口
  188. InitBingkou();
  189. //开始添加串口
  190. InitCom();
  191. }
  192. /*
  193. *获取CreateFile的USB端口号
  194. **/
  195. int CPosPrinter::GetDevicePath(LPGUID lpGuid)
  196. {
  197. HDEVINFO hDevInfoSet;
  198. SP_DEVINFO_DATA spDevInfoData;
  199. SP_DEVICE_INTERFACE_DATA ifData;
  200. PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
  201. int nCount;
  202. int nTotle;
  203. BOOL bResult;
  204. //这2个字符串,用于根据usb的名字对比是否为打印机设备
  205. wstring strUSBPrint = TEXT("USB 打印支持");
  206. //xp上是英文
  207. wstring strUSBPrint_EN = L"USB Printing Support";
  208. // 取得一个该GUID相关的设备信息集句柄
  209. hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
  210. NULL, // 无关键字
  211. NULL, // 不指定父窗口句柄
  212. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
  213. // 失败...
  214. if(hDevInfoSet == INVALID_HANDLE_VALUE)
  215. {
  216. LOG_INFO("SetupDiGetClassDevs failed \r\n");
  217. return 0;
  218. }
  219. // 申请设备接口数据空间
  220. pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
  221. pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  222. nTotle = -1;
  223. nCount = 0;
  224. bResult = TRUE;
  225. // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
  226. while(bResult)
  227. {
  228. nTotle++;
  229. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  230. // 枚举符合该GUID的设备接口
  231. bResult = ::SetupDiEnumDeviceInfo(
  232. hDevInfoSet, // 设备信息集句柄
  233. (ULONG)nTotle, // 设备信息集里的设备序号
  234. &spDevInfoData); // 设备接口信息
  235. if(bResult)
  236. {
  237. DWORD DataT;
  238. TCHAR buf[MAX_PATH] = { 0 };
  239. DWORD nSize = 0;
  240. // get Friendly Name or Device Description
  241. if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  242. SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  243. {
  244. }
  245. else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  246. SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  247. {
  248. }
  249. else
  250. {
  251. lstrcpy(buf, _T("Unknown"));
  252. }
  253. wstring ws_buf = buf;
  254. //是否是要找的设备类型
  255. if(ws_buf != strUSBPrint && ws_buf != strUSBPrint_EN)
  256. {
  257. LOG_INFO("not good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  258. continue;
  259. }
  260. LOG_INFO("good usb, buf:" << CLewaimaiString::UnicodeToANSI(ws_buf).c_str());
  261. ifData.cbSize = sizeof(ifData);
  262. // 枚舉符合該GUID的設備接口
  263. bResult = ::SetupDiEnumDeviceInterfaces(
  264. hDevInfoSet, // 設備信息集句柄
  265. NULL, // 不需額外的設備描述
  266. lpGuid, // GUID
  267. (ULONG)nTotle, // 設備信息集里的設備序號
  268. &ifData); // 設備接口信息
  269. if(bResult)
  270. {
  271. // 取得该设备接口的细节(设备路径)
  272. bResult = SetupDiGetInterfaceDeviceDetail(
  273. hDevInfoSet, // 设备信息集句柄
  274. &ifData, // 设备接口信息
  275. pDetail, // 设备接口细节(设备路径)
  276. INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
  277. NULL, // 不需计算输出缓冲区大小(直接用设定值)
  278. NULL); // 不需额外的设备描述
  279. if(bResult)
  280. {
  281. wstring DevicePath = pDetail->DevicePath;
  282. wstring vid = DevicePath.substr(DevicePath.find(_T("vid_"), 0) + 4, 4);
  283. wstring pid = DevicePath.substr(DevicePath.find(_T("pid_"), 0) + 4, 4);
  284. LOG_INFO("Vid:" << vid.c_str() << ", Pid:" << pid.c_str());
  285. if(GetPrinterType(vid, pid) == 2)
  286. {
  287. //标签打印机,暂时不处理打印
  288. LOG_INFO("标签打印机,暂时不打印!");
  289. continue;
  290. }
  291. // 复制设备路径到输出缓冲区
  292. m_usb_devices.push_back(DevicePath);
  293. // 调整计数值
  294. nCount++;
  295. LOG_INFO("Cnt = " << nCount << ",pDetail->DevicePath =" << pDetail->DevicePath);
  296. }
  297. }
  298. }
  299. else
  300. {
  301. //LOG_INFO("SetupDiEnumDeviceInfo FAILED!@");
  302. }
  303. }
  304. // 释放设备接口数据空间
  305. ::GlobalFree(pDetail);
  306. // 关闭设备信息集句柄
  307. ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
  308. return nCount;
  309. }
  310. /*
  311. *返回打印机的类型, 1:普通热敏打印机 2:标签打印机
  312. **/
  313. int CPosPrinter::GetPrinterType(wstring vid, wstring pid)
  314. {
  315. if(vid == L"6868" && pid == L"0500")
  316. {
  317. //佳博标签打印机
  318. return 2;
  319. }
  320. return 1;
  321. }
  322. /*
  323. *打印收银小票
  324. **/
  325. void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
  326. {
  327. //设置模式,后面输出数据不会错
  328. m_type = 1;
  329. std::string printer_leixing = CSetting::GetParam("setting_printer_leixing");
  330. if(printer_leixing == "auto")
  331. {
  332. InitShouyin();
  333. }
  334. else if(printer_leixing == "usb")
  335. {
  336. std::string printer_usb = CSetting::GetParam("setting_printer_usb");
  337. std::wstring ws_printer_usb = CLewaimaiString::UTF8ToUnicode(printer_usb);
  338. InitOneUsb(ws_printer_usb);
  339. }
  340. else if(printer_leixing == "bingkou")
  341. {
  342. InitBingkou();
  343. }
  344. else if(printer_leixing == "chuankou")
  345. {
  346. InitCom();
  347. }
  348. else if(printer_leixing == "wangkou")
  349. {
  350. m_type = 2;
  351. std::string wangkou_ip = CSetting::GetParam("setting_printer_wangkou_ip");
  352. //初始化连接
  353. try
  354. {
  355. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(wangkou_ip.c_str()), 9100);
  356. m_socket.connect(ep);
  357. }
  358. catch(std::exception& e)
  359. {
  360. std::string err = e.what();
  361. LOG_INFO("连接厨房打印机失败,IP地址:" << wangkou_ip.c_str() << ",错误信息:" << err.c_str());
  362. }
  363. }
  364. //读取当前收银打印机的设置
  365. std::string guige = CSetting::GetParam("setting_printer_guige");
  366. std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
  367. int n_lianshu = atoi(lianshu.c_str());
  368. //正式开始打印
  369. for(int i = 0; i < n_lianshu; i++)
  370. {
  371. POS_Reset();
  372. string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
  373. POS_TextOut(shop_name, true, true, 1);
  374. POS_FeedLine();
  375. POS_FeedLine();
  376. bool setting_printer_quhuo_big = false;
  377. if(CSetting::GetParam("setting_printer_quhuo_big") == "1")
  378. {
  379. setting_printer_quhuo_big = true;
  380. }
  381. if(order.m_is_selftake == "1")
  382. {
  383. POS_TextOut("到店自取订单", false, setting_printer_quhuo_big, 1);
  384. POS_FeedLine();
  385. POS_FeedLine();
  386. POS_TextOut("取餐号:" + CLewaimaiString::UTF8ToANSI(order.m_take_food_code), true, 1);
  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(atof(order.m_goods_coupon_value.c_str()) > 0.0001)
  636. {
  637. string coupon = "商品券:-" + order.m_goods_coupon_value;
  638. POS_TextOut(coupon, false, setting_printer_price_big);
  639. POS_FeedLine();
  640. is_other_money = true;
  641. }
  642. if(is_other_money)
  643. {
  644. POS_TextOut(lines);
  645. POS_FeedLine();
  646. }
  647. //最后显示总价
  648. POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, setting_printer_price_big, 2);
  649. POS_FeedLine();
  650. //显示付款方式
  651. bool setting_printer_pay_big = false;
  652. if(CSetting::GetParam("setting_printer_pay_big") == "1")
  653. {
  654. setting_printer_pay_big = true;
  655. }
  656. POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, setting_printer_pay_big, 2);
  657. POS_FeedLine();
  658. //走纸几行再切
  659. POS_FeedLine();
  660. POS_FeedLine();
  661. POS_FeedLine();
  662. POS_FeedLine();
  663. POS_FeedLine();
  664. POS_CutPaper();
  665. }
  666. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  667. POS_Reset();
  668. //关闭设备
  669. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  670. {
  671. CloseHandle((*it).hPort);
  672. }
  673. }
  674. void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
  675. {
  676. //设置模式,后面输出数据不会错
  677. m_type = 2;
  678. //读取厨房打印机信息
  679. std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
  680. for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
  681. {
  682. ChufangPrinter printer = *it;
  683. std::string ip = printer.ip;
  684. //初始化连接
  685. try
  686. {
  687. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
  688. m_socket.connect(ep);
  689. }
  690. catch(std::exception& e)
  691. {
  692. std::string err = e.what();
  693. LOG_INFO("连接厨房打印机失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
  694. //连接失败了,处理下一个厨房打印机
  695. continue;
  696. }
  697. std::string guige = printer.guige;
  698. std::string fendan = printer.fendan;
  699. std::string fenlei = printer.fenlei;
  700. std::string fenlei_ids = printer.fenlei_ids;
  701. std::vector<CWaimaiOrderItem> cur_printer_use;
  702. if(fenlei == "0")
  703. {
  704. cur_printer_use = order.m_order_items;
  705. }
  706. else
  707. {
  708. //如果开启了分类打印,就要比对了
  709. std::map<string, bool> ids_map;
  710. std::vector<string> ids = CLewaimaiString::Split(fenlei_ids, ",");
  711. for(std::vector<string>::iterator it = ids.begin(); it != ids.end(); it++)
  712. {
  713. ids_map[(*it)] = true;
  714. }
  715. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  716. {
  717. if(ids_map.find((*it).m_type_id) != ids_map.end())
  718. {
  719. //如果当前商品的分类,在厨房打印机设置的分类里面,才加入
  720. cur_printer_use.push_back(*it);
  721. }
  722. }
  723. }
  724. if(fendan == "0")
  725. {
  726. POS_Reset();
  727. POS_TextOut("派工单", true, true, 1);
  728. POS_FeedLine();
  729. POS_FeedLine();
  730. string order_type = "#" + order.m_restaurant_number + " ";
  731. if(order.m_is_selftake == "1")
  732. {
  733. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  734. POS_FeedLine();
  735. POS_FeedLine();
  736. }
  737. else
  738. {
  739. POS_TextOut(order_type + "外卖订单", false, false, 1);
  740. POS_FeedLine();
  741. POS_FeedLine();
  742. }
  743. //预设选项
  744. if(order.m_order_field.size() > 1)
  745. {
  746. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  747. {
  748. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  749. POS_TextOut(order_field, false, false, 0);
  750. POS_FeedLine();
  751. }
  752. }
  753. //准备开始打印商品详情
  754. string lines;
  755. if(guige == "58")
  756. {
  757. lines = "--------------------------------";
  758. }
  759. else
  760. {
  761. lines = "------------------------------------------------";
  762. }
  763. POS_TextOut(lines);
  764. POS_FeedLine();
  765. //商品标题
  766. if(guige == "58")
  767. {
  768. POS_TextOut("商品 单价 数量 金额");
  769. }
  770. else
  771. {
  772. POS_TextOut("商品 单价 数量 金额");
  773. }
  774. POS_FeedLine();
  775. //商品内容
  776. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  777. {
  778. std::string food_name = (*it).m_food_name;
  779. std::string food_price = (*it).m_item_price;
  780. std::string quantity = (*it).m_quantity;
  781. //计算总价
  782. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  783. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  784. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  785. //先输出商品名字
  786. std::vector<string> m_names;
  787. if(guige == "58")
  788. {
  789. m_names = HandleFoodname(food_name, 1);
  790. }
  791. else
  792. {
  793. m_names = HandleFoodname(food_name, 2);
  794. }
  795. std::string firstLine = m_names[0];
  796. int nGuige;
  797. if(guige == "58")
  798. {
  799. nGuige = 1;
  800. }
  801. else
  802. {
  803. nGuige = 2;
  804. }
  805. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  806. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  807. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  808. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  809. POS_TextOut(firstLineShow, false, false, 0);
  810. POS_FeedLine();
  811. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  812. {
  813. POS_TextOut((*it), false, false, 0);
  814. POS_FeedLine();
  815. }
  816. }
  817. //判断是否有备注
  818. if(order.m_memo.length() > 0)
  819. {
  820. POS_FeedLine();
  821. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  822. POS_TextOut(address, false, false, 0);
  823. POS_FeedLine();
  824. }
  825. //结束商品详情打印
  826. POS_TextOut(lines);
  827. POS_FeedLine();
  828. POS_FeedLine();
  829. POS_FeedLine();
  830. POS_FeedLine();
  831. POS_FeedLine();
  832. POS_FeedLine();
  833. POS_CutPaper();
  834. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  835. POS_Reset();
  836. }
  837. else
  838. {
  839. POS_Reset();
  840. //分单模式下,每个商品打印一张单
  841. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  842. {
  843. POS_TextOut("派工单", true, true, 1);
  844. POS_FeedLine();
  845. POS_FeedLine();
  846. string order_type = "#" + order.m_restaurant_number + " ";
  847. if(order.m_is_selftake == "1")
  848. {
  849. POS_TextOut(order_type + "到店自取订单", false, false, 1);
  850. POS_FeedLine();
  851. POS_FeedLine();
  852. }
  853. else
  854. {
  855. POS_TextOut(order_type + "外卖订单", false, false, 1);
  856. POS_FeedLine();
  857. POS_FeedLine();
  858. }
  859. //预设选项
  860. if(order.m_order_field.size() > 1)
  861. {
  862. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  863. {
  864. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  865. POS_TextOut(order_field, false, false, 0);
  866. POS_FeedLine();
  867. }
  868. }
  869. //准备开始打印商品详情
  870. string lines;
  871. if(guige == "58")
  872. {
  873. lines = "--------------------------------";
  874. }
  875. else
  876. {
  877. lines = "------------------------------------------------";
  878. }
  879. POS_TextOut(lines);
  880. POS_FeedLine();
  881. //商品标题
  882. if(guige == "58")
  883. {
  884. POS_TextOut("商品 单价 数量 金额");
  885. }
  886. else
  887. {
  888. POS_TextOut("商品 单价 数量 金额");
  889. }
  890. POS_FeedLine();
  891. std::string food_name = (*it).m_food_name;
  892. std::string food_price = (*it).m_item_price;
  893. std::string quantity = (*it).m_quantity;
  894. //计算总价
  895. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  896. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  897. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  898. //先输出商品名字
  899. std::vector<string> m_names;
  900. if(guige == "58")
  901. {
  902. m_names = HandleFoodname(food_name, 1);
  903. }
  904. else
  905. {
  906. m_names = HandleFoodname(food_name, 2);
  907. }
  908. std::string firstLine = m_names[0];
  909. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  910. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  911. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  912. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  913. POS_TextOut(firstLineShow, false, false, 0);
  914. POS_FeedLine();
  915. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  916. {
  917. POS_TextOut((*it), false, false, 0);
  918. POS_FeedLine();
  919. }
  920. //判断是否有备注
  921. if(order.m_memo.length() > 0)
  922. {
  923. POS_FeedLine();
  924. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  925. POS_TextOut(address, false, false, 0);
  926. POS_FeedLine();
  927. }
  928. //结束商品详情打印
  929. POS_TextOut(lines);
  930. POS_FeedLine();
  931. POS_FeedLine();
  932. POS_FeedLine();
  933. POS_FeedLine();
  934. POS_FeedLine();
  935. POS_FeedLine();
  936. POS_CutPaper();
  937. }
  938. //还原打印机初始设置,有些傻逼收银设备居然不是自动调用,比如哗啦啦
  939. POS_Reset();
  940. }
  941. m_socket.close();
  942. }
  943. }
  944. std::vector<std::wstring> CPosPrinter::getUsbDevices()
  945. {
  946. return m_usb_devices;
  947. }
  948. int CPosPrinter::WriteData(string msg)
  949. {
  950. return WriteBuf(msg.c_str(), msg.length());
  951. }
  952. int CPosPrinter::WriteBuf(const char* buf, int len)
  953. {
  954. if(m_type == 1)
  955. {
  956. DWORD dwWrite;
  957. for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  958. {
  959. HANDLE hPort = (*it).hPort;
  960. if((*it).type == 3)
  961. {
  962. //串口,同步写数据
  963. //LOG_INFO("before com writefile handle:" << hPort);
  964. WriteFile(hPort, buf, len, &dwWrite, NULL);
  965. //LOG_INFO("after com writefile handle:" << hPort);
  966. }
  967. else
  968. {
  969. //usb和并口,异步写数据
  970. OVERLAPPED overlap;
  971. memset(&overlap, 0, sizeof(overlap));
  972. BOOL rc = WriteFile(hPort, buf, len, &dwWrite, &overlap);
  973. if(rc)
  974. {
  975. //LOG_INFO("writefile success immediately, handle:" << hPort);
  976. }
  977. else
  978. {
  979. if(GetLastError() == ERROR_IO_PENDING)
  980. {
  981. //LOG_INFO("Request queued, waiting... handle:" << hPort);
  982. WaitForSingleObject(hPort, 1000);
  983. //LOG_INFO("Request completed, handle:" << hPort);
  984. //计算写入了多少字节的数据
  985. DWORD numread;
  986. rc = GetOverlappedResult(
  987. hPort,
  988. &overlap,
  989. &numread,
  990. FALSE
  991. );
  992. }
  993. }
  994. }
  995. }
  996. return 1;
  997. }
  998. else
  999. {
  1000. try
  1001. {
  1002. m_socket.write_some(boost::asio::buffer(buf, len));
  1003. }
  1004. catch(const std::exception& e)
  1005. {
  1006. LOG_INFO(e.what());
  1007. }
  1008. return 0;
  1009. }
  1010. }
  1011. /*
  1012. *测试打印机是否连接
  1013. **/
  1014. bool CPosPrinter::PortTest(HANDLE hPort)
  1015. {
  1016. //标签打印机的查询状态指令
  1017. char chInitCode[2] = { 0x1b, 0x40 };
  1018. DWORD dwWrite;
  1019. OVERLAPPED overlap;
  1020. memset(&overlap, 0, sizeof(overlap));
  1021. BOOL rc = WriteFile(hPort, chInitCode, 2, &dwWrite, &overlap);
  1022. if(rc)
  1023. {
  1024. LOG_INFO("writefile success immediately, handle:" << hPort);
  1025. return true;
  1026. }
  1027. else
  1028. {
  1029. if(GetLastError() == ERROR_IO_PENDING)
  1030. {
  1031. DWORD ret = WaitForSingleObject(hPort, 1000);
  1032. if(ret == 0)
  1033. {
  1034. LOG_INFO("printer is connect, handle:" << hPort);
  1035. return true;
  1036. }
  1037. else
  1038. {
  1039. LOG_INFO("printer is not connect, handle:" << hPort);
  1040. }
  1041. }
  1042. }
  1043. return false;
  1044. }
  1045. int CPosPrinter::POS_Reset(void)
  1046. {
  1047. char s[2] = { 0x1B, 0x40 };
  1048. WriteBuf(s, 2);
  1049. return 0;
  1050. }
  1051. int CPosPrinter::POS_FeedLine(void)
  1052. {
  1053. char s[1] = { 0x0A };
  1054. WriteBuf(s, 1);
  1055. return 0;
  1056. }
  1057. int CPosPrinter::POS_Feed(void)
  1058. {
  1059. char s[3] = { 0x1B, 0x4A, 0x00 };
  1060. WriteBuf(s, 3);
  1061. return 0;
  1062. }
  1063. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  1064. {
  1065. string s;
  1066. s = "\x1D\x50\xB4\xB4";
  1067. WriteData(s);
  1068. s = "\x1B\x53";
  1069. WriteData(s);
  1070. return 0;
  1071. }
  1072. int CPosPrinter::POS_SET_MOVE_X()
  1073. {
  1074. char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00 };
  1075. WriteBuf(s2, 6);
  1076. return 0;
  1077. }
  1078. int CPosPrinter::POS_MOVE_X()
  1079. {
  1080. char s[1] = { 0x09 };
  1081. WriteBuf(s, 1);
  1082. return 0;
  1083. }
  1084. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  1085. {
  1086. char cx = (char)(x);
  1087. char cy = (char)(y);
  1088. char s1[4] = { 0x1B, 0x24, cx, cy };
  1089. WriteBuf(s1, 4);
  1090. return 0;
  1091. }
  1092. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  1093. {
  1094. char cx = (char)(x);
  1095. char cy = (char)(y);
  1096. char s1[4] = { 0x1D, 0x57, cx, cy };
  1097. WriteBuf(s1, 4);
  1098. return 0;
  1099. }
  1100. /*
  1101. *align_type:0 左对齐 1 居中对齐 2右对齐
  1102. **/
  1103. int CPosPrinter::POS_TextOut(string abc, bool is_double_width, bool is_double_height, int align_type)
  1104. {
  1105. if(is_double_width && is_double_height)
  1106. {
  1107. char s1[3] = { 0x1B, 0x21, 0x30 };
  1108. WriteBuf(s1, 3);
  1109. char s2[3] = { 0x1C, 0x21, 0x0c };
  1110. WriteBuf(s2, 3);
  1111. }
  1112. else if(is_double_width && !is_double_height)
  1113. {
  1114. char s1[3] = { 0x1B, 0x21, 0x20 };
  1115. WriteBuf(s1, 3);
  1116. char s2[3] = { 0x1C, 0x21, 0x04 };
  1117. WriteBuf(s2, 3);
  1118. }
  1119. else if(!is_double_width && is_double_height)
  1120. {
  1121. char s1[3] = { 0x1B, 0x21, 0x10 };
  1122. WriteBuf(s1, 3);
  1123. char s2[3] = { 0x1C, 0x21, 0x08 };
  1124. WriteBuf(s2, 3);
  1125. }
  1126. else
  1127. {
  1128. char s1[3] = { 0x1B, 0x21, 0x00 };
  1129. WriteBuf(s1, 3);
  1130. char s2[3] = { 0x1C, 0x57, 0x00 };
  1131. WriteBuf(s2, 3);
  1132. }
  1133. if(align_type == 0)
  1134. {
  1135. char s1[3] = { 0x1B, 0x61, 0x00 };
  1136. WriteBuf(s1, 3);
  1137. }
  1138. else if(align_type == 1)
  1139. {
  1140. char s1[3] = { 0x1B, 0x61, 0x01 };
  1141. WriteBuf(s1, 3);
  1142. }
  1143. else if(align_type == 2)
  1144. {
  1145. char s1[3] = { 0x1B, 0x61, 0x02 };
  1146. WriteBuf(s1, 3);
  1147. }
  1148. else
  1149. {
  1150. }
  1151. WriteData(abc);
  1152. return 0;
  1153. }
  1154. /*
  1155. *打印机切纸,直接切不走纸
  1156. **/
  1157. int CPosPrinter::POS_CutPaper()
  1158. {
  1159. char s[4] = { 0x1D, 0x56, 0x01 };
  1160. WriteBuf(s, 3);
  1161. return 0;
  1162. }
  1163. int CPosPrinter::POS_OutQRCode()
  1164. {
  1165. char QRCode1[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x05 };
  1166. char QRCode2[16] = { 0x1d, 0x28, 0x6b, 0x0b, 0x00, 0x31, 0x50, 0x30, 0x47, 0x70, 0x72, 0x69,
  1167. 0x6e, 0x74, 0x65, 0x72
  1168. };
  1169. char QRCode3[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30 };
  1170. WriteBuf(QRCode1, 8);
  1171. WriteBuf(QRCode2, 16);
  1172. WriteBuf(QRCode3, 8);
  1173. return 0;
  1174. }
  1175. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  1176. {
  1177. nHanzi = 0;
  1178. nZimu = 0;
  1179. const char* buffer = s.c_str();
  1180. while(*buffer != '\0')
  1181. {
  1182. if(!(*buffer >= 0 && *buffer <= 127))
  1183. {
  1184. //汉字
  1185. buffer++;
  1186. buffer++;
  1187. nHanzi++;
  1188. }
  1189. else
  1190. {
  1191. //字母
  1192. buffer++;
  1193. nZimu++;
  1194. }
  1195. }
  1196. }
  1197. /*
  1198. *规格 1:58mm 2:80mm
  1199. **/
  1200. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  1201. {
  1202. std::vector<std::string> newnameArray;
  1203. int nHanzi, nZimu;
  1204. CalWord(oldname, nHanzi, nZimu);
  1205. int nWidth = nHanzi * 2 + nZimu;
  1206. int maxWidth;
  1207. if(guige == 1)
  1208. {
  1209. maxWidth = 15;
  1210. }
  1211. else
  1212. {
  1213. maxWidth = 28;
  1214. }
  1215. if(nWidth <= maxWidth)
  1216. {
  1217. //对于nWidth补空格
  1218. for(int i = 0; i < maxWidth - nWidth; i++)
  1219. {
  1220. oldname += " ";
  1221. }
  1222. newnameArray.push_back(oldname);
  1223. return newnameArray;
  1224. }
  1225. //宽度大于15的情况,如果超过了,就要进行换行截取
  1226. const char* s = oldname.c_str();
  1227. int nTmp = 0;
  1228. int nTotal = 0;
  1229. while(*s != '\0')
  1230. {
  1231. if(!(*s >= 0 && *s <= 127))
  1232. {
  1233. //汉字的情况
  1234. s++;
  1235. nTmp++;
  1236. nTotal++;
  1237. s++;
  1238. nTmp++;
  1239. nTotal++;
  1240. }
  1241. else
  1242. {
  1243. //字母的情况
  1244. s++;
  1245. nTmp++;
  1246. nTotal++;
  1247. }
  1248. if(nTmp == maxWidth)
  1249. {
  1250. //这里开始要换行了
  1251. string newnameItem;
  1252. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1253. newnameArray.push_back(newnameItem);
  1254. nTmp = 0;
  1255. }
  1256. else if(nTmp == maxWidth - 1)
  1257. {
  1258. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  1259. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  1260. {
  1261. //下一个是中文,也要换行了,补齐一个空格
  1262. string newnameItem;
  1263. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1264. newnameItem += " ";
  1265. newnameArray.push_back(newnameItem);
  1266. nTmp = 0;
  1267. }
  1268. }
  1269. }
  1270. if(nTmp > 0)
  1271. {
  1272. //处理分隔后的最后一行
  1273. string newnameItem;
  1274. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  1275. for(int i = 0; i < maxWidth - nTmp; i++)
  1276. {
  1277. newnameItem += " ";
  1278. }
  1279. newnameArray.push_back(newnameItem);
  1280. }
  1281. return newnameArray;
  1282. }
  1283. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  1284. {
  1285. double price = atof(oldprice.c_str());
  1286. if(price < 10.00)
  1287. {
  1288. //单位数
  1289. if(guige == 1)
  1290. {
  1291. return " " + oldprice;
  1292. }
  1293. else
  1294. {
  1295. return " " + oldprice;
  1296. }
  1297. }
  1298. else if(price > 9.99 && price < 100.00)
  1299. {
  1300. //双位数
  1301. if(guige == 1)
  1302. {
  1303. return " " + oldprice;
  1304. }
  1305. else
  1306. {
  1307. return " " + oldprice;
  1308. }
  1309. }
  1310. else if(price > 99.99 && price < 1000.00)
  1311. {
  1312. //三位数
  1313. if(guige == 1)
  1314. {
  1315. return " " + oldprice;
  1316. }
  1317. else
  1318. {
  1319. return " " + oldprice;
  1320. }
  1321. }
  1322. else
  1323. {
  1324. //四位数
  1325. if(guige == 1)
  1326. {
  1327. return " " + oldprice;
  1328. }
  1329. else
  1330. {
  1331. return " " + oldprice;
  1332. }
  1333. }
  1334. }
  1335. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  1336. {
  1337. int n = atoi(oldquantity.c_str());
  1338. if(n < 10)
  1339. {
  1340. if(guige == 1)
  1341. {
  1342. return " " + oldquantity;
  1343. }
  1344. else
  1345. {
  1346. return " " + oldquantity;
  1347. }
  1348. }
  1349. else if(n >= 10 && n <= 99)
  1350. {
  1351. if(guige == 1)
  1352. {
  1353. return " " + oldquantity;
  1354. }
  1355. else
  1356. {
  1357. return " " + oldquantity;
  1358. }
  1359. }
  1360. else
  1361. {
  1362. if(guige == 1)
  1363. {
  1364. return "" + oldquantity;
  1365. }
  1366. else
  1367. {
  1368. return " " + oldquantity;
  1369. }
  1370. }
  1371. }
  1372. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  1373. {
  1374. double price = atof(oldprice.c_str());
  1375. if(price < 10.00)
  1376. {
  1377. //单位数
  1378. if(guige == 1)
  1379. {
  1380. return " " + oldprice;
  1381. }
  1382. else
  1383. {
  1384. return " " + oldprice;
  1385. }
  1386. }
  1387. else if(price > 9.99 && price < 100.00)
  1388. {
  1389. //双位数
  1390. if(guige == 1)
  1391. {
  1392. return " " + oldprice;
  1393. }
  1394. else
  1395. {
  1396. return " " + oldprice;
  1397. }
  1398. }
  1399. else if(price > 99.99 && price < 1000.00)
  1400. {
  1401. //三位数
  1402. if(guige == 1)
  1403. {
  1404. return " " + oldprice;
  1405. }
  1406. else
  1407. {
  1408. return " " + oldprice;
  1409. }
  1410. }
  1411. else
  1412. {
  1413. //四位数
  1414. if(guige == 1)
  1415. {
  1416. return " " + oldprice;
  1417. }
  1418. else
  1419. {
  1420. return " " + oldprice;
  1421. }
  1422. }
  1423. }