CPosPrinter.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "../pch/pch.h"
  2. #include "CPosPrinter.h"
  3. #include <winioctl.h>
  4. #include <setupapi.h>
  5. CPosPrinter::CPosPrinter()
  6. {
  7. Init();
  8. }
  9. CPosPrinter::~CPosPrinter()
  10. {
  11. }
  12. bool CPosPrinter::Init()
  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::PrintWaimaiOrder(CWaimaiOrder& order)
  45. {
  46. POS_Reset();
  47. POS_SetMotionUnit(180, 180);
  48. string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
  49. POS_S_TextOut(shop_name);
  50. POS_FeedLine();
  51. POS_FeedLine();
  52. POS_FeedLine();
  53. POS_CutPaper();
  54. }
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  56. //获取CreateFile的USB端口号
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. // 根据GUID获得设备路径
  59. // lpGuid: GUID指针
  60. // pszDevicePath: 设备路径指针的指针,用于返回找到的路径
  61. // 返回: 成功得到的设备路径个数,可能不止1个
  62. int CPosPrinter::GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath)
  63. {
  64. HDEVINFO hDevInfoSet;
  65. SP_DEVINFO_DATA spDevInfoData;
  66. SP_DEVICE_INTERFACE_DATA ifData;
  67. PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
  68. int nCount;
  69. int nTotle;
  70. BOOL bResult;
  71. wstring strUSBPrint = TEXT("USB 打印支持");
  72. // 取得一个该GUID相关的设备信息集句柄
  73. hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
  74. NULL, // 无关键字
  75. NULL, // 不指定父窗口句柄
  76. DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
  77. // 失败...
  78. if(hDevInfoSet == INVALID_HANDLE_VALUE)
  79. {
  80. printf("failed \r\n");
  81. return 0;
  82. }
  83. // 申请设备接口数据空间
  84. pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
  85. pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
  86. nTotle = -1;
  87. nCount = 0;
  88. bResult = TRUE;
  89. // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
  90. while(bResult)
  91. {
  92. nTotle++;
  93. spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  94. // 枚举符合该GUID的设备接口
  95. bResult = ::SetupDiEnumDeviceInfo(
  96. hDevInfoSet, // 设备信息集句柄
  97. (ULONG)nTotle, // 设备信息集里的设备序号
  98. &spDevInfoData); // 设备接口信息
  99. if(bResult)
  100. {
  101. DWORD DataT;
  102. TCHAR buf[MAX_PATH];
  103. DWORD nSize = 0;
  104. // get Friendly Name or Device Description
  105. if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  106. SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  107. {
  108. }
  109. else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
  110. SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
  111. {
  112. }
  113. else
  114. {
  115. lstrcpy(buf, _T("Unknown"));
  116. }
  117. _tprintf(_T("buf = %s \r\n"), buf);
  118. //是否是要找的设备类型
  119. if(_tcscmp(buf, strUSBPrint.c_str()) != 0)
  120. {
  121. continue;
  122. }
  123. _tprintf(_T("OK\r\n"));
  124. ifData.cbSize = sizeof(ifData);
  125. // 枚舉符合該GUID的設備接口
  126. bResult = ::SetupDiEnumDeviceInterfaces(
  127. hDevInfoSet, // 設備信息集句柄
  128. NULL, // 不需額外的設備描述
  129. lpGuid, // GUID
  130. (ULONG)nTotle, // 設備信息集里的設備序號
  131. &ifData); // 設備接口信息
  132. if(bResult)
  133. {
  134. // 取得该设备接口的细节(设备路径)
  135. bResult = SetupDiGetInterfaceDeviceDetail(
  136. hDevInfoSet, // 设备信息集句柄
  137. &ifData, // 设备接口信息
  138. pDetail, // 设备接口细节(设备路径)
  139. INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
  140. NULL, // 不需计算输出缓冲区大小(直接用设定值)
  141. NULL); // 不需额外的设备描述
  142. if(bResult)
  143. {
  144. // 复制设备路径到输出缓冲区
  145. ::_tcscpy_s(pszDevicePath[nCount], 256, pDetail->DevicePath);
  146. // 调整计数值
  147. nCount++;
  148. _tprintf(_T("Cnt = %d,pDetail->DevicePath =%s\r\n"), nCount, pDetail->DevicePath);
  149. }
  150. }
  151. }
  152. }
  153. // 释放设备接口数据空间
  154. ::GlobalFree(pDetail);
  155. // 关闭设备信息集句柄
  156. ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
  157. return nCount;
  158. }
  159. int CPosPrinter::WriteData(string meg)
  160. {
  161. DWORD dwWrite;
  162. return WriteFile(m_hPort, meg.c_str(), (DWORD)meg.length(), &dwWrite, NULL);
  163. }
  164. int CPosPrinter::WriteBuf(char *buf, int len)
  165. {
  166. DWORD dwWrite;
  167. return WriteFile(m_hPort, buf, len, &dwWrite, NULL);
  168. }
  169. int CPosPrinter::POS_Reset(void)
  170. {
  171. string s;
  172. s = "\x1B\x40";
  173. WriteData(s);
  174. return 0;
  175. }
  176. int CPosPrinter::POS_FeedLine(void)
  177. {
  178. string s;
  179. s = "\x0A";
  180. WriteData(s);
  181. return 0;
  182. }
  183. int CPosPrinter::POS_SetMotionUnit(int x, int y)
  184. {
  185. string s;
  186. s = "\x1D\x50\xB4\xB4";
  187. WriteData(s);
  188. s = "\x1B\x53";
  189. WriteData(s);
  190. return 0;
  191. }
  192. int CPosPrinter::POS_S_TextOut(string &abc)
  193. {
  194. string s;
  195. char SetAbsPos[4] = { 0x1B, 0x24, 0x46, 0x00 };
  196. WriteBuf(SetAbsPos, 4);
  197. char SelctFontType[3] = { 0x1B, 0x4D, 0x03 };
  198. WriteBuf(SelctFontType, 3);
  199. char SelctOutMode[3] = { 0x1D, 0x21, 0x00 };
  200. WriteBuf(SelctOutMode, 3);
  201. WriteData(abc);
  202. return 0;
  203. }
  204. int CPosPrinter::POS_CutPaper()
  205. {
  206. char CutPaperMode[4] = { 0x1D, 0x56, 0x41, 0x00 };
  207. WriteBuf(CutPaperMode, 4);
  208. return 0;
  209. }
  210. int CPosPrinter::POS_OutQRCode()
  211. {
  212. char QRCode1[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x05 };
  213. char QRCode2[16] = { 0x1d, 0x28, 0x6b, 0x0b, 0x00, 0x31, 0x50, 0x30, 0x47, 0x70, 0x72, 0x69,
  214. 0x6e, 0x74, 0x65, 0x72
  215. };
  216. char QRCode3[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30 };
  217. WriteBuf(QRCode1, 8);
  218. WriteBuf(QRCode2, 16);
  219. WriteBuf(QRCode3, 8);
  220. return 0;
  221. }