CPosPrinter.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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. bool CPosPrinter::InitShouyin()
  13. {
  14. //遍历USB设备,找到POS打印机路径
  15. //设备路径
  16. TCHAR* szDevicePath[MAX_DEVICE];
  17. //设置中文字符
  18. setlocale(LC_CTYPE, "chs");
  19. TCHAR* Port = NULL;
  20. //分配需要的空间
  21. for(int i = 0; i < MAX_DEVICE; i++)
  22. {
  23. szDevicePath[i] = new TCHAR[256];
  24. }
  25. //取设备路径
  26. int nDevice = GetDevicePath((LPGUID)&USB_GUID, szDevicePath);
  27. //LOG_INFO("nDevice:" << nDevice);
  28. int i = 0;
  29. while(i < nDevice)
  30. {
  31. Port = szDevicePath[i++];
  32. //LOG_INFO("device.Port = " << Port);
  33. HANDLE hPort = CreateFile(Port, GENERIC_READ | GENERIC_WRITE,
  34. 0, NULL,
  35. OPEN_EXISTING,
  36. FILE_ATTRIBUTE_NORMAL, NULL);
  37. if (hPort == INVALID_HANDLE_VALUE)
  38. {
  39. // 打开端口失败
  40. continue;
  41. }
  42. //把当前发现的保存下来
  43. m_hPorts.push_back(hPort);
  44. }
  45. return true;
  46. }
  47. void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
  48. {
  49. //设置模式,后面输出数据不会错
  50. m_type = 1;
  51. //初始化收银打印机的链接
  52. bool ret = InitShouyin();
  53. if(ret == false)
  54. {
  55. LOG_INFO("打开收银打印机端口失败!");
  56. return;
  57. }
  58. //读取当前收银打印机的设置
  59. std::string guige = CSetting::GetParam("setting_printer_guige");
  60. std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
  61. int n_lianshu = atoi(lianshu.c_str());
  62. //正式开始打印
  63. for(int i = 0; i < n_lianshu; i++)
  64. {
  65. POS_Reset();
  66. string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
  67. POS_TextOut(shop_name, true, 1);
  68. POS_FeedLine();
  69. POS_FeedLine();
  70. if(order.m_is_selftake == "1")
  71. {
  72. POS_TextOut("到店自取订单", false, 1);
  73. POS_FeedLine();
  74. POS_FeedLine();
  75. }
  76. else
  77. {
  78. POS_TextOut("外卖订单", false, 1);
  79. POS_FeedLine();
  80. }
  81. POS_FeedLine();
  82. //订单来源
  83. string from_type = "订单来源:" + CLewaimaiString::UTF8ToANSI(order.m_from_type);
  84. POS_TextOut(from_type);
  85. POS_FeedLine();
  86. //订单号
  87. string order_num = "订单号:" + CLewaimaiString::UTF8ToANSI(order.m_order_num);
  88. POS_TextOut(order_num);
  89. POS_FeedLine();
  90. //姓名
  91. if(order.m_is_selftake != "1")
  92. {
  93. string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
  94. POS_TextOut(name);
  95. POS_FeedLine();
  96. }
  97. //电话
  98. string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
  99. POS_TextOut(phone);
  100. POS_FeedLine();
  101. //地址
  102. if(order.m_is_selftake != "1")
  103. {
  104. string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
  105. POS_TextOut(address);
  106. POS_FeedLine();
  107. }
  108. //预设选项
  109. if(order.m_order_field.size() > 1)
  110. {
  111. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  112. {
  113. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  114. POS_TextOut(order_field, false, 0);
  115. POS_FeedLine();
  116. }
  117. }
  118. //下单时间
  119. string order_date = "下单时间:" + CLewaimaiString::UTF8ToANSI(order.m_order_date);
  120. POS_TextOut(order_date);
  121. POS_FeedLine();
  122. //配送时间
  123. if(order.m_is_selftake == "1")
  124. {
  125. string date = "自取时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  126. POS_TextOut(date);
  127. POS_FeedLine();
  128. }
  129. else
  130. {
  131. string date = "配送时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
  132. POS_TextOut(date);
  133. POS_FeedLine();
  134. }
  135. //准备开始打印商品详情
  136. string lines;
  137. if(guige == "58")
  138. {
  139. lines = "--------------------------------";
  140. }
  141. else
  142. {
  143. lines = "------------------------------------------------";
  144. }
  145. POS_TextOut(lines);
  146. POS_FeedLine();
  147. //商品标题
  148. if(guige == "58")
  149. {
  150. POS_TextOut("商品 单价 数量 金额");
  151. }
  152. else
  153. {
  154. POS_TextOut("商品 单价 数量 金额");
  155. }
  156. POS_FeedLine();
  157. //商品内容
  158. for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
  159. {
  160. std::string food_name = (*it).m_food_name;
  161. std::string food_price = (*it).m_item_price;
  162. std::string quantity = (*it).m_quantity;
  163. //计算总价
  164. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  165. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  166. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  167. //先输出商品名字
  168. std::vector<string> m_names;
  169. if(guige == "58")
  170. {
  171. m_names = HandleFoodname(food_name, 1);
  172. }
  173. else
  174. {
  175. m_names = HandleFoodname(food_name, 2);
  176. }
  177. std::string firstLine = m_names[0];
  178. int nGuige;
  179. if(guige == "58")
  180. {
  181. nGuige = 1;
  182. }
  183. else
  184. {
  185. nGuige = 2;
  186. }
  187. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  188. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  189. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  190. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  191. POS_TextOut(firstLineShow, false, 0);
  192. POS_FeedLine();
  193. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  194. {
  195. POS_TextOut((*it), false, 0);
  196. POS_FeedLine();
  197. }
  198. }
  199. //判断是否有备注
  200. if(order.m_memo.length() > 0)
  201. {
  202. POS_FeedLine();
  203. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  204. POS_TextOut(address, false, 0);
  205. POS_FeedLine();
  206. }
  207. //结束商品详情打印
  208. POS_TextOut(lines);
  209. POS_FeedLine();
  210. //开始打印其他的费用
  211. bool is_other_money = false;
  212. if(order.m_delivery > 0)
  213. {
  214. string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
  215. POS_TextOut(delivery, false, 0);
  216. POS_FeedLine();
  217. is_other_money = true;
  218. }
  219. if(order.m_is_dabao == "1")
  220. {
  221. string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
  222. POS_TextOut(dabao, false, 0);
  223. POS_FeedLine();
  224. is_other_money = true;
  225. }
  226. //开始显示增值服务费
  227. if(order.m_addservie.size() > 0)
  228. {
  229. for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
  230. {
  231. string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
  232. POS_TextOut(addservice, false, 0);
  233. POS_FeedLine();
  234. is_other_money = true;
  235. }
  236. is_other_money = true;
  237. }
  238. if(order.m_discount < 10)
  239. {
  240. string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
  241. POS_TextOut(discount, false, 0);
  242. POS_FeedLine();
  243. is_other_money = true;
  244. }
  245. if(order.m_is_member_discount == "1")
  246. {
  247. string member_discount = "会员优惠:-" + order.m_member_discount;
  248. POS_TextOut(member_discount, false, 0);
  249. POS_FeedLine();
  250. is_other_money = true;
  251. }
  252. if(order.m_is_firstcut == "1")
  253. {
  254. string firstcut = "首单优惠:-" + order.m_firstcut_value;
  255. POS_TextOut(firstcut, false, 0);
  256. POS_FeedLine();
  257. is_other_money = true;
  258. }
  259. //满减
  260. string::size_type position = order.m_promotion.find(":");
  261. if(position != order.m_promotion.npos)
  262. {
  263. string promotion_value;
  264. promotion_value.assign(order.m_promotion, position + 1);
  265. string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
  266. POS_TextOut(promotion, false, 0);
  267. POS_FeedLine();
  268. is_other_money = true;
  269. }
  270. if(order.m_coupon > 0)
  271. {
  272. string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
  273. POS_TextOut(coupon, false, 0);
  274. POS_FeedLine();
  275. is_other_money = true;
  276. }
  277. if(is_other_money)
  278. {
  279. POS_TextOut(lines);
  280. POS_FeedLine();
  281. }
  282. //最后显示总价
  283. POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, 2);
  284. POS_FeedLine();
  285. //显示付款方式
  286. POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, 2);
  287. POS_FeedLine();
  288. POS_FeedLine();
  289. POS_CutPaper();
  290. }
  291. }
  292. void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
  293. {
  294. //设置模式,后面输出数据不会错
  295. m_type = 2;
  296. //读取厨房打印机信息
  297. std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
  298. for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
  299. {
  300. ChufangPrinter printer = *it;
  301. std::string ip = printer.ip;
  302. //初始化连接
  303. try
  304. {
  305. boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
  306. m_socket.connect(ep);
  307. }
  308. catch(std::exception& e)
  309. {
  310. std::string err = e.what();
  311. LOG_INFO("连接厨房打印机失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
  312. //连接失败了,处理下一个厨房打印机
  313. continue;
  314. }
  315. std::string guige = printer.guige;
  316. std::string fendan = printer.fendan;
  317. std::vector<CWaimaiOrderItem> cur_printer_use = order.m_order_items;
  318. if(fendan == "0")
  319. {
  320. POS_Reset();
  321. POS_TextOut("派工单", true, 1);
  322. POS_FeedLine();
  323. POS_FeedLine();
  324. //预设选项
  325. if(order.m_order_field.size() > 1)
  326. {
  327. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  328. {
  329. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  330. POS_TextOut(order_field, false, 0);
  331. POS_FeedLine();
  332. }
  333. }
  334. //准备开始打印商品详情
  335. string lines;
  336. if(guige == "58")
  337. {
  338. lines = "--------------------------------";
  339. }
  340. else
  341. {
  342. lines = "------------------------------------------------";
  343. }
  344. POS_TextOut(lines);
  345. POS_FeedLine();
  346. //商品标题
  347. if(guige == "58")
  348. {
  349. POS_TextOut("商品 单价 数量 金额");
  350. }
  351. else
  352. {
  353. POS_TextOut("商品 单价 数量 金额");
  354. }
  355. POS_FeedLine();
  356. //商品内容
  357. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  358. {
  359. std::string food_name = (*it).m_food_name;
  360. std::string food_price = (*it).m_item_price;
  361. std::string quantity = (*it).m_quantity;
  362. //计算总价
  363. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  364. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  365. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  366. //先输出商品名字
  367. std::vector<string> m_names;
  368. if(guige == "58")
  369. {
  370. m_names = HandleFoodname(food_name, 1);
  371. }
  372. else
  373. {
  374. m_names = HandleFoodname(food_name, 2);
  375. }
  376. std::string firstLine = m_names[0];
  377. int nGuige;
  378. if(guige == "58")
  379. {
  380. nGuige = 1;
  381. }
  382. else
  383. {
  384. nGuige = 2;
  385. }
  386. std::string priceShow = HandleFoodItemPrice(food_price, nGuige);
  387. std::string quantityShow = HandleFoodQuantity(quantity, nGuige);
  388. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, nGuige);
  389. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  390. POS_TextOut(firstLineShow, false, 0);
  391. POS_FeedLine();
  392. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  393. {
  394. POS_TextOut((*it), false, 0);
  395. POS_FeedLine();
  396. }
  397. }
  398. //判断是否有备注
  399. if(order.m_memo.length() > 0)
  400. {
  401. POS_FeedLine();
  402. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  403. POS_TextOut(address, false, 0);
  404. POS_FeedLine();
  405. }
  406. //结束商品详情打印
  407. POS_TextOut(lines);
  408. POS_FeedLine();
  409. POS_FeedLine();
  410. POS_CutPaper();
  411. }
  412. else
  413. {
  414. POS_Reset();
  415. //分单模式下,每个商品打印一张单
  416. for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
  417. {
  418. POS_TextOut("派工单", true, 1);
  419. POS_FeedLine();
  420. POS_FeedLine();
  421. //预设选项
  422. if(order.m_order_field.size() > 1)
  423. {
  424. for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
  425. {
  426. string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
  427. POS_TextOut(order_field, false, 0);
  428. POS_FeedLine();
  429. }
  430. }
  431. //准备开始打印商品详情
  432. string lines;
  433. if(guige == "58")
  434. {
  435. lines = "--------------------------------";
  436. }
  437. else
  438. {
  439. lines = "------------------------------------------------";
  440. }
  441. POS_TextOut(lines);
  442. POS_FeedLine();
  443. //商品标题
  444. if(guige == "58")
  445. {
  446. POS_TextOut("商品 单价 数量 金额");
  447. }
  448. else
  449. {
  450. POS_TextOut("商品 单价 数量 金额");
  451. }
  452. POS_FeedLine();
  453. std::string food_name = (*it).m_food_name;
  454. std::string food_price = (*it).m_item_price;
  455. std::string quantity = (*it).m_quantity;
  456. //计算总价
  457. double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
  458. std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
  459. food_name = CLewaimaiString::UTF8ToANSI(food_name);
  460. //先输出商品名字
  461. std::vector<string> m_names;
  462. if(guige == "58")
  463. {
  464. m_names = HandleFoodname(food_name, 1);
  465. }
  466. else
  467. {
  468. m_names = HandleFoodname(food_name, 2);
  469. }
  470. std::string firstLine = m_names[0];
  471. std::string priceShow = HandleFoodItemPrice(food_price, 2);
  472. std::string quantityShow = HandleFoodQuantity(quantity, 2);
  473. std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
  474. std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
  475. POS_TextOut(firstLineShow, false, 0);
  476. POS_FeedLine();
  477. for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
  478. {
  479. POS_TextOut((*it), false, 0);
  480. POS_FeedLine();
  481. }
  482. //判断是否有备注
  483. if(order.m_memo.length() > 0)
  484. {
  485. POS_FeedLine();
  486. string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
  487. POS_TextOut(address, false, 0);
  488. POS_FeedLine();
  489. }
  490. //结束商品详情打印
  491. POS_TextOut(lines);
  492. POS_FeedLine();
  493. POS_FeedLine();
  494. POS_CutPaper();
  495. }
  496. }
  497. m_socket.close();
  498. }
  499. }
  500. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  501. //获取CreateFile的USB端口号
  502. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  503. // 根据GUID获得设备路径
  504. // lpGuid: GUID指针
  505. // pszDevicePath: 设备路径指针的指针,用于返回找到的路径
  506. // 返回: 成功得到的设备路径个数,可能不止1个
  507. int CPosPrinter::GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath)
  508. {
  509. HDEVINFO hDevInfoSet;
  510. SP_DEVINFO_DATA spDevInfoData;
  511. SP_DEVICE_INTERFACE_DATA ifData;
  512. PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
  513. int nCount;
  514. int nTotle;
  515. BOOL bResult;
  516. //这2个字符串,用于根据usb的名字对比是否为打印机设备
  517. wstring strUSBPrint = TEXT("USB 打印支持");
  518. wstring strUSBPrint_EN = L"USB Printing Support";
  519. // 取得一个该GUID相关的设备信息集句柄
  520. hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
  521. NULL, // 无关键字
  522. NULL, // 不指定父窗口句柄
  523. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
  524. // 失败...
  525. if(hDevInfoSet == INVALID_HANDLE_VALUE)
  526. {
  527. LOG_INFO("failed \r\n");
  528. return 0;
  529. }
  530. // 申请设备接口数据空间
  531. pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
  532. pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  533. nTotle = -1;
  534. nCount = 0;
  535. bResult = TRUE;
  536. // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
  537. while(bResult)
  538. {
  539. nTotle++;
  540. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  541. // 枚举符合该GUID的设备接口
  542. bResult = ::SetupDiEnumDeviceInfo(
  543. hDevInfoSet, // 设备信息集句柄
  544. (ULONG)nTotle, // 设备信息集里的设备序号
  545. &spDevInfoData); // 设备接口信息
  546. if(bResult)
  547. {
  548. DWORD DataT;
  549. TCHAR buf[MAX_PATH];
  550. DWORD nSize = 0;
  551. // get Friendly Name or Device Description
  552. if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  553. SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  554. {
  555. }
  556. else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  557. SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  558. {
  559. }
  560. else
  561. {
  562. lstrcpy(buf, _T("Unknown"));
  563. }
  564. //LOG_INFO("buf:" << buf);
  565. //是否是要找的设备类型
  566. if(_tcscmp(buf, strUSBPrint.c_str()) != 0 && _tcscmp(buf, strUSBPrint_EN.c_str()) != 0)
  567. {
  568. continue;
  569. }
  570. //LOG_INFO("找到打印的USB设备!");
  571. ifData.cbSize = sizeof(ifData);
  572. // 枚舉符合該GUID的設備接口
  573. bResult = ::SetupDiEnumDeviceInterfaces(
  574. hDevInfoSet, // 設備信息集句柄
  575. NULL, // 不需額外的設備描述
  576. lpGuid, // GUID
  577. (ULONG)nTotle, // 設備信息集里的設備序號
  578. &ifData); // 設備接口信息
  579. if(bResult)
  580. {
  581. // 取得该设备接口的细节(设备路径)
  582. bResult = SetupDiGetInterfaceDeviceDetail(
  583. hDevInfoSet, // 设备信息集句柄
  584. &ifData, // 设备接口信息
  585. pDetail, // 设备接口细节(设备路径)
  586. INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
  587. NULL, // 不需计算输出缓冲区大小(直接用设定值)
  588. NULL); // 不需额外的设备描述
  589. if(bResult)
  590. {
  591. // 复制设备路径到输出缓冲区
  592. ::_tcscpy_s(pszDevicePath[nCount], 256, pDetail->DevicePath);
  593. // 调整计数值
  594. nCount++;
  595. _tprintf(_T("Cnt = %d,pDetail->DevicePath =%s\r\n"), nCount, pDetail->DevicePath);
  596. }
  597. }
  598. }
  599. }
  600. // 释放设备接口数据空间
  601. ::GlobalFree(pDetail);
  602. // 关闭设备信息集句柄
  603. ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
  604. return nCount;
  605. }
  606. int CPosPrinter::WriteData(string msg)
  607. {
  608. if(m_type == 1)
  609. {
  610. DWORD dwWrite;
  611. for (std::vector<HANDLE>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  612. {
  613. WriteFile(*it, msg.c_str(), (DWORD)msg.length(), &dwWrite, NULL);
  614. }
  615. return 1;
  616. }
  617. else
  618. {
  619. try
  620. {
  621. m_socket.write_some(boost::asio::buffer(msg.c_str(), msg.length()));
  622. }
  623. catch(const std::exception& e)
  624. {
  625. LOG_INFO(e.what());
  626. }
  627. return 0;
  628. }
  629. }
  630. int CPosPrinter::WriteBuf(char* buf, int len)
  631. {
  632. if(m_type == 1)
  633. {
  634. DWORD dwWrite;
  635. for (std::vector<HANDLE>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
  636. {
  637. WriteFile(*it, buf, len, &dwWrite, NULL);
  638. }
  639. return 1;
  640. }
  641. else
  642. {
  643. try
  644. {
  645. m_socket.write_some(boost::asio::buffer(buf, len));
  646. }
  647. catch(const std::exception& e)
  648. {
  649. LOG_INFO(e.what());
  650. }
  651. return 0;
  652. }
  653. }
  654. int CPosPrinter::POS_Reset(void)
  655. {
  656. char s[2] = {0x1B, 0x40};
  657. WriteBuf(s, 2);
  658. return 0;
  659. }
  660. int CPosPrinter::POS_FeedLine(void)
  661. {
  662. char s[1] = {0x0A};
  663. WriteBuf(s, 1);
  664. return 0;
  665. }
  666. int CPosPrinter::POS_Feed(void)
  667. {
  668. char s[3] = { 0x1B, 0x4A, 0x00 };
  669. WriteBuf(s, 3);
  670. return 0;
  671. }
  672. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  673. {
  674. string s;
  675. s = "\x1D\x50\xB4\xB4";
  676. WriteData(s);
  677. s = "\x1B\x53";
  678. WriteData(s);
  679. return 0;
  680. }
  681. int CPosPrinter::POS_SET_MOVE_X()
  682. {
  683. char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
  684. WriteBuf(s2, 6);
  685. return 0;
  686. }
  687. int CPosPrinter::POS_MOVE_X()
  688. {
  689. char s[1] = { 0x09 };
  690. WriteBuf(s, 1);
  691. return 0;
  692. }
  693. int CPosPrinter::POS_SET_ABS_X(int x, int y)
  694. {
  695. char cx = (char)(x);
  696. char cy = (char)(y);
  697. char s1[4] = { 0x1B, 0x24, cx, cy };
  698. WriteBuf(s1, 4);
  699. return 0;
  700. }
  701. int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
  702. {
  703. char cx = (char)(x);
  704. char cy = (char)(y);
  705. char s1[4] = { 0x1D, 0x57, cx, cy };
  706. WriteBuf(s1, 4);
  707. return 0;
  708. }
  709. /*
  710. *align_type:0 左对齐 1 居中对齐 2右对齐
  711. **/
  712. int CPosPrinter::POS_TextOut(string abc, bool is_double, int align_type)
  713. {
  714. if(is_double)
  715. {
  716. char s1[3] = { 0x1B, 0x21, 0x30 };
  717. WriteBuf(s1, 3);
  718. char s2[3] = { 0x1C, 0x21, 0x0c };
  719. WriteBuf(s2, 3);
  720. }
  721. else
  722. {
  723. char s1[3] = { 0x1B, 0x21, 0x00 };
  724. WriteBuf(s1, 3);
  725. char s2[3] = { 0x1C, 0x57, 0x00 };
  726. WriteBuf(s2, 3);
  727. }
  728. if(align_type == 0)
  729. {
  730. char s1[3] = { 0x1B, 0x61, 0x00 };
  731. WriteBuf(s1, 3);
  732. }
  733. else if(align_type == 1)
  734. {
  735. char s1[3] = { 0x1B, 0x61, 0x01 };
  736. WriteBuf(s1, 3);
  737. }
  738. else if(align_type == 2)
  739. {
  740. char s1[3] = { 0x1B, 0x61, 0x02 };
  741. WriteBuf(s1, 3);
  742. }
  743. else
  744. {
  745. }
  746. WriteData(abc);
  747. return 0;
  748. }
  749. int CPosPrinter::POS_CutPaper()
  750. {
  751. char s[4] = { 0x1D, 0x56, 0x41, 0x00 };
  752. WriteBuf(s, 4);
  753. return 0;
  754. }
  755. int CPosPrinter::POS_OutQRCode()
  756. {
  757. char QRCode1[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x05 };
  758. char QRCode2[16] = { 0x1d, 0x28, 0x6b, 0x0b, 0x00, 0x31, 0x50, 0x30, 0x47, 0x70, 0x72, 0x69,
  759. 0x6e, 0x74, 0x65, 0x72
  760. };
  761. char QRCode3[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30 };
  762. WriteBuf(QRCode1, 8);
  763. WriteBuf(QRCode2, 16);
  764. WriteBuf(QRCode3, 8);
  765. return 0;
  766. }
  767. void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
  768. {
  769. nHanzi = 0;
  770. nZimu = 0;
  771. const char* buffer = s.c_str();
  772. while(*buffer++ != '\0')
  773. {
  774. if(!(*buffer >= 0 && *buffer <= 127))
  775. {
  776. //汉字
  777. buffer++;
  778. nHanzi++;
  779. }
  780. else
  781. {
  782. //字母
  783. nZimu++;
  784. }
  785. }
  786. }
  787. /*
  788. *规格 1:58mm 2:80mm
  789. **/
  790. std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
  791. {
  792. std::vector<std::string> newnameArray;
  793. int nHanzi, nZimu;
  794. CalWord(oldname, nHanzi, nZimu);
  795. int nWidth = nHanzi * 2 + nZimu;
  796. int maxWidth;
  797. if(guige == 1)
  798. {
  799. maxWidth = 15;
  800. }
  801. else
  802. {
  803. maxWidth = 28;
  804. }
  805. if(nWidth <= maxWidth)
  806. {
  807. //对于nWidth补空格
  808. for(int i = 0; i < maxWidth - nWidth; i++)
  809. {
  810. oldname += " ";
  811. }
  812. newnameArray.push_back(oldname);
  813. return newnameArray;
  814. }
  815. //宽度大于15的情况,如果超过了,就要进行换行截取
  816. const char* s = oldname.c_str();
  817. int nTmp = 0;
  818. int nTotal = 0;
  819. while(*s++ != '\0')
  820. {
  821. nTmp++;
  822. nTotal++;
  823. if(!(*(s) >= 0 && *(s) <= 127))
  824. {
  825. //汉字的情况
  826. s++;
  827. nTmp++;
  828. nTotal++;
  829. }
  830. if(nTmp == maxWidth)
  831. {
  832. //这里开始要换行了
  833. string newnameItem;
  834. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  835. newnameArray.push_back(newnameItem);
  836. nTmp = 0;
  837. }
  838. else if(nTmp == maxWidth - 1)
  839. {
  840. //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
  841. if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
  842. {
  843. //下一个是中文,也要换行了,补齐一个空格
  844. string newnameItem;
  845. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  846. newnameItem += " ";
  847. newnameArray.push_back(newnameItem);
  848. nTmp = 0;
  849. }
  850. }
  851. }
  852. if(nTmp > 0)
  853. {
  854. //处理分隔后的最后一行
  855. string newnameItem;
  856. newnameItem.assign(oldname, nTotal - nTmp, nTmp);
  857. for(int i = 0; i < maxWidth - nTmp; i++)
  858. {
  859. newnameItem += " ";
  860. }
  861. newnameArray.push_back(newnameItem);
  862. }
  863. return newnameArray;
  864. }
  865. std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
  866. {
  867. double price = atof(oldprice.c_str());
  868. if(price < 10.00)
  869. {
  870. //单位数
  871. if(guige == 1)
  872. {
  873. return " " + oldprice;
  874. }
  875. else
  876. {
  877. return " " + oldprice;
  878. }
  879. }
  880. else if(price > 9.99 && price < 100.00)
  881. {
  882. //双位数
  883. if(guige == 1)
  884. {
  885. return " " + oldprice;
  886. }
  887. else
  888. {
  889. return " " + oldprice;
  890. }
  891. }
  892. else if(price > 99.99 && price < 1000.00)
  893. {
  894. //三位数
  895. if(guige == 1)
  896. {
  897. return " " + oldprice;
  898. }
  899. else
  900. {
  901. return " " + oldprice;
  902. }
  903. }
  904. else
  905. {
  906. //四位数
  907. if(guige == 1)
  908. {
  909. return " " + oldprice;
  910. }
  911. else
  912. {
  913. return " " + oldprice;
  914. }
  915. }
  916. }
  917. std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
  918. {
  919. int n = atoi(oldquantity.c_str());
  920. if(n < 10)
  921. {
  922. if(guige == 1)
  923. {
  924. return " " + oldquantity;
  925. }
  926. else
  927. {
  928. return " " + oldquantity;
  929. }
  930. }
  931. else if(n >= 10 && n <= 99)
  932. {
  933. if(guige == 1)
  934. {
  935. return " " + oldquantity;
  936. }
  937. else
  938. {
  939. return " " + oldquantity;
  940. }
  941. }
  942. else
  943. {
  944. if(guige == 1)
  945. {
  946. return "" + oldquantity;
  947. }
  948. else
  949. {
  950. return " " + oldquantity;
  951. }
  952. }
  953. }
  954. std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
  955. {
  956. double price = atof(oldprice.c_str());
  957. if(price < 10.00)
  958. {
  959. //单位数
  960. if(guige == 1)
  961. {
  962. return " " + oldprice;
  963. }
  964. else
  965. {
  966. return " " + oldprice;
  967. }
  968. }
  969. else if(price > 9.99 && price < 100.00)
  970. {
  971. //双位数
  972. if(guige == 1)
  973. {
  974. return " " + oldprice;
  975. }
  976. else
  977. {
  978. return " " + oldprice;
  979. }
  980. }
  981. else if(price > 99.99 && price < 1000.00)
  982. {
  983. //三位数
  984. if(guige == 1)
  985. {
  986. return " " + oldprice;
  987. }
  988. else
  989. {
  990. return " " + oldprice;
  991. }
  992. }
  993. else
  994. {
  995. //四位数
  996. if(guige == 1)
  997. {
  998. return " " + oldprice;
  999. }
  1000. else
  1001. {
  1002. return " " + oldprice;
  1003. }
  1004. }
  1005. }