CPosPrinter.cpp 32 KB

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