ReceiptBase.cpp 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. // ReceiptBase.cpp : Defines the entry point for the DLL application.
  2. //
  3. #include "ReceiptBase.h"
  4. //m_iUsedLeng修改成用纸长度,原为行数。
  5. ReceiptBase::ReceiptBase()
  6. {
  7. m_iLetSpace = 0;
  8. m_iUsedLeng = 0;
  9. m_iMinPaper = 400; //400*0.125=50mm
  10. m_iLineHeight = 30;
  11. m_iNVHeight = 0;
  12. m_iLineLeng = 48;
  13. m_iLastPos = 0;
  14. m_iLinesPrted = 0;
  15. m_iMMPerSec = 80;
  16. }
  17. ReceiptBase::~ReceiptBase()
  18. {
  19. m_cSerial.Close();
  20. }
  21. void ReceiptBase::SetLinePara(int iLineLeng, int iLeftSpace, int iSpeed)
  22. {
  23. m_iLetSpace = iLeftSpace;
  24. m_iLineLeng = iLineLeng;
  25. m_iMMPerSec = iSpeed;
  26. }
  27. //凭条打印机 判断端口打开
  28. bool ReceiptBase::Isopen()
  29. {
  30. return m_cSerial.IsOpen();
  31. }
  32. int ReceiptBase::Open(const int iVID, const int iPID, const int iFD)
  33. {
  34. if(m_cSerial.IsOpen()) return RECEIPT_SUCCESS;
  35. //sew此处需要修改
  36. if (m_cSerial.Open("{28d78fad-5a12-11D1-ae5b-0000f803a8c2}", iVID, iPID) == false)
  37. return -1;
  38. Init();
  39. return RECEIPT_SUCCESS;
  40. }
  41. // 关闭端口
  42. int ReceiptBase::Close()
  43. {
  44. if(m_cSerial.IsOpen() == false)
  45. return RECEIPT_SUCCESS;
  46. m_cSerial.Close();
  47. return RECEIPT_SUCCESS;
  48. }
  49. //通用发送数据接口
  50. int ReceiptBase::SendData(const unsigned char *sSend,int iDatLen,int iTimeout)
  51. {
  52. int iCount = 0;
  53. int ia;
  54. for (ia=0; ia<iDatLen; ia++)
  55. if (sSend[ia] == '\n') iCount++;
  56. if (iCount > 50 && iCount > iDatLen / 10)
  57. iCount = 50;
  58. static INT64 iPrintTime = 0;
  59. if (iCount)
  60. {
  61. m_iLinesPrted += iCount;
  62. if (m_iLinesPrted > 20) //50行处理一次
  63. {
  64. int iTimeWant = m_iLinesPrted * m_iLineHeight * 1000 / (8 * m_iMMPerSec);
  65. INT64 iCurTime = ::GetSystemTime();
  66. int iUse = (int)(iCurTime - iPrintTime);
  67. if (iUse < 0 || iUse > iTimeWant) iUse = iTimeWant;
  68. int iSleep = iTimeWant - iUse;
  69. if (iSleep > 0)
  70. Sleep(iSleep);
  71. iPrintTime = iCurTime;
  72. m_iLinesPrted = 0;
  73. }
  74. }
  75. return m_cSerial.Send(sSend, iDatLen, iTimeout);
  76. }
  77. //通用接收数据接口
  78. int ReceiptBase::ReadData(unsigned char *sRead,int iWant,int iTimeout)
  79. {
  80. return m_cSerial.Receive((unsigned char *)sRead, iWant, iTimeout);
  81. }
  82. //打印机初始化
  83. int ReceiptBase::Init()
  84. {
  85. if(m_cSerial.IsOpen() == false) return RECEIPT_ERR_NO_OPEN;
  86. unsigned char szSendBuf[20];
  87. szSendBuf[0] = 0x1b;
  88. szSendBuf[1] = 0x40;
  89. int nRet = SendData(szSendBuf, 2, 3000);
  90. if (nRet < 2)
  91. return RECEIPT_ERR_RCV_ERR;
  92. SetRowDistance(0);
  93. SetLeftDistance(0);
  94. SetFont(false, false, false, false, false);
  95. SetAlign(0);
  96. SetScale(1,1);
  97. SetInverse(false);
  98. SetRotate(false);
  99. SetBlackFlag(false);
  100. return RECEIPT_SUCCESS;
  101. }
  102. //通用发送数据接口
  103. int ReceiptBase::SendHexStr(const char *sData, int iDataLen)
  104. {
  105. if (iDataLen == 0) iDataLen = strlen(sData);
  106. if (iDataLen > 1000) return RECEIPT_ERR_IN_ERR;
  107. unsigned char sSend[512];
  108. int iCount = ::HexStrToANSI(sData, sSend, iDataLen);
  109. if (iCount <= 0)
  110. return RECEIPT_ERR_IN_ERR;
  111. int iRet = SendData(sSend, iCount);
  112. return iRet;
  113. }
  114. //数据输出,打印
  115. int ReceiptBase::PrintData(const char * sData)
  116. {
  117. unsigned char sSend[128];
  118. if(sData == NULL || strlen((char*)sData) < 1)
  119. {
  120. return RECEIPT_ERR_IN_ERR;
  121. }
  122. int iLeng = strlen((char*)sData);
  123. int iPos = 0, iLinePos = 0;
  124. int iChCount = 0;
  125. unsigned char chTemp;
  126. bool bLineMax, bLineEnd, bDataEnd;
  127. int iLengMax = m_iLineLeng;
  128. for (iPos = 0 ; iPos < iLeng; )
  129. {
  130. bLineMax = FALSE;
  131. bLineEnd = FALSE;
  132. bDataEnd = FALSE;
  133. iChCount = 0; //半个汉字计算
  134. memset(sSend, ' ', m_iLetSpace);
  135. if (m_iLastPos)
  136. iLinePos = m_iLastPos;
  137. else
  138. iLinePos = m_iLetSpace;
  139. while (iLinePos < iLengMax)
  140. {
  141. chTemp = (unsigned char)sData[iPos++];
  142. if (chTemp == '\r' || chTemp == '\n')
  143. { //一行结束
  144. bLineEnd = TRUE;
  145. break;
  146. }
  147. //数据不包含换行
  148. sSend[iLinePos++] = chTemp;
  149. //计算半个汉字
  150. if (chTemp >= 128)
  151. iChCount++;
  152. else
  153. iChCount = 0;
  154. if (iPos >= iLeng)
  155. { //总数据结束
  156. bDataEnd = TRUE;
  157. break;
  158. }
  159. }
  160. if (iLinePos >= iLengMax)
  161. bLineMax = TRUE;
  162. //处理半个汉字,最后是半个汉字,前移一字节
  163. if ((iChCount % 2) == 1 && (bLineMax || bDataEnd))
  164. {
  165. iLinePos--;
  166. iPos--;
  167. }
  168. if (bLineMax || bLineEnd)
  169. {
  170. sSend[iLinePos] = '\n';
  171. iLinePos++;
  172. m_iUsedLeng += m_iLineHeight;
  173. }
  174. sSend[iLinePos] = 0;
  175. int iStart = 0;
  176. if (m_iLastPos)
  177. {
  178. iStart = m_iLastPos;
  179. m_iLastPos = 0;
  180. }
  181. if (SendData(sSend + iStart, iLinePos - iStart, 1000) < iLinePos - iStart)
  182. return RECEIPT_ERR_RCV_ERR;
  183. //数据的最后一个字符是汉字的半个字符
  184. if ((iChCount % 2) == 1 && iPos == (iLeng - 1))
  185. {
  186. m_iLastPos = iLinePos;
  187. return iPos;
  188. }
  189. //windows格式\r\n换行处理
  190. if (chTemp == '\r' && sData[iPos] == '\n')
  191. iPos++;
  192. }
  193. if (bLineMax || bLineEnd)
  194. m_iLastPos = 0;
  195. else
  196. m_iLastPos = iLinePos;
  197. return iPos;
  198. }
  199. //走纸n行
  200. int ReceiptBase::FeedLines(int iLines)
  201. {
  202. for (int ia = 0; ia < iLines; ia++)
  203. {
  204. int iRet = SendData((unsigned char*)"\r\n", 2, 500);
  205. Sleep(40);
  206. m_iUsedLeng += m_iLineHeight;
  207. m_iLastPos = 0;
  208. if (iRet < 1) return RECEIPT_ERR_RCV_ERR;
  209. }
  210. return RECEIPT_SUCCESS;
  211. }
  212. //走纸n行
  213. int ReceiptBase::PrintChar(char ch, int iCount)
  214. {
  215. for (int ia = 0; ia < iCount; ia++)
  216. {
  217. int iRet = SendData((unsigned char*)&ch, 1, 500);
  218. if (ch == '\n'){
  219. SendData((unsigned char*)"\r", 1, 500);
  220. Sleep(40);
  221. m_iUsedLeng += m_iLineHeight;
  222. m_iLastPos = 0;
  223. }
  224. if (iRet < 1) return RECEIPT_ERR_RCV_ERR;
  225. }
  226. return RECEIPT_SUCCESS;
  227. }
  228. //走纸n行
  229. int ReceiptBase::PrintLine(const char* sLine)
  230. {
  231. int iRet = PrintData(sLine);
  232. if (iRet < 0) return iRet;
  233. return FeedLines(1);
  234. }
  235. //横向移动
  236. int ReceiptBase::MoveX(int iX)
  237. {
  238. if(iX < 0 || iX > 255)
  239. {
  240. return RECEIPT_ERR_IN_ERR;
  241. }
  242. unsigned char sCmd[4]={0x1b, 0x5c, (unsigned char)iX, 0};
  243. int nRet = SendData(sCmd,4);
  244. if (nRet < 0) return nRet;
  245. return RECEIPT_SUCCESS;
  246. }
  247. //走纸1点阵
  248. int ReceiptBase::FeedPaper(short iMode, short iDistance)
  249. {
  250. unsigned char cpSend[10]={0};
  251. int nRet = 0;
  252. if ( iMode == 0 ) //往前走
  253. {
  254. m_iUsedLeng += iDistance;
  255. cpSend[0] = 0x1B;
  256. cpSend[1] = 0x4A;
  257. while (iDistance > 0 ) //line 大于 0 才执行后走纸命令
  258. {
  259. if (iDistance > 80)
  260. {
  261. cpSend[2] = 80;
  262. iDistance -= 80;
  263. }
  264. else
  265. {
  266. cpSend[2] = (char)iDistance;
  267. iDistance = 0;
  268. }
  269. nRet = SendData(cpSend,3,500);
  270. }
  271. if (nRet < 0) return nRet;
  272. SendData((unsigned char*)"\r", 1, 500);
  273. return RECEIPT_SUCCESS;
  274. }
  275. return RECEIPT_ERR_RCV_ERR;
  276. }
  277. //0:全切 1:半切
  278. int ReceiptBase::Cut(int iMode)
  279. {
  280. SetFont(0, 0, 0, 0, 0);
  281. SendData((unsigned char*)"\r", 1, 500); //为保证最后一行数据输出
  282. if (m_iUsedLeng < m_iMinPaper -160 )
  283. FeedPaper(0, (m_iMinPaper - m_iUsedLeng - 160) >> 3);
  284. //机械结构,切刀位置在2cm位置,必须走纸2cm后切纸,否则最后数据被切掉
  285. FeedPaper(0, 160);
  286. unsigned char cpSend[20];
  287. int nRet = 0;
  288. cpSend[0]=0x1B;
  289. cpSend[1]=0x4E;
  290. cpSend[2]=0x03;
  291. cpSend[3]=iMode;
  292. SendData(cpSend,4,300);
  293. cpSend[0]=0x1D;
  294. cpSend[1]=0x56;
  295. cpSend[2]=0x42;
  296. cpSend[3]=0x00;
  297. Sleep(300);
  298. nRet = SendData(cpSend,4,300);
  299. m_iUsedLeng=0;
  300. m_cSerial.Flush();
  301. Sleep(300);
  302. if (nRet < 0) return nRet;
  303. return RECEIPT_SUCCESS;
  304. }
  305. //nAlign 0 - 左对齐 ,1 - 居中, 2 - 右对齐
  306. int ReceiptBase::SetAlign(short nAlign)
  307. {
  308. if(nAlign > 2 || nAlign <0)
  309. {
  310. return RECEIPT_ERR_IN_ERR;
  311. }
  312. unsigned char szSendBuf[20];
  313. int nRet = 0;
  314. memset(szSendBuf,'\0',20);
  315. szSendBuf[0] = 0x1b;
  316. szSendBuf[1] = 0x61;
  317. szSendBuf[2] = (char)nAlign;//0 - 左对齐 ,1 - 居中, 2 - 右对齐
  318. nRet = SendData(szSendBuf,3);
  319. if (nRet < 0) return nRet;
  320. return RECEIPT_SUCCESS;
  321. }
  322. //DH 倍高 DW倍宽 DD黑体 UL下划线
  323. int ReceiptBase::SetFont(bool bDH, bool bDW, bool bDD, bool bUL, bool bCharB)
  324. {
  325. unsigned char sCmd[4], chChi = 0, chAsc = 0;
  326. //英文模式
  327. sCmd[0]=0x1B;
  328. sCmd[1]=0x21;
  329. if (bDH)
  330. {
  331. chAsc += 16;
  332. chChi += 8;
  333. m_iLineHeight = 54;
  334. }
  335. else
  336. m_iLineHeight = 30;
  337. if (bUL)
  338. {
  339. chAsc += 0x80;
  340. chChi += 0x80;
  341. }
  342. if (bDW)
  343. {
  344. chAsc += 32;
  345. chChi += 4;
  346. }
  347. if (bDD)
  348. {
  349. chAsc += 8;
  350. }
  351. if (bCharB)
  352. {
  353. chAsc += 1;
  354. }
  355. sCmd[2] = chAsc;
  356. SendData(sCmd,3,500);
  357. //汉字倍宽倍高模式
  358. sCmd[0]=0x1C;
  359. sCmd[1]=0x21;
  360. sCmd[2] = chChi;
  361. SendData(sCmd,3,500);
  362. // sCmd[0]=0x1b;
  363. // sCmd[1]=0x2d;
  364. // sCmd[2]=bUL ? 1 : 0;
  365. // SendData(sCmd,3,500);
  366. // sCmd[0]=0x1c;
  367. // sCmd[1]=0x57;
  368. // sCmd[2]=(bDW | bDH) ? 1 : 0;
  369. // SendData(sCmd,3,500);
  370. return RECEIPT_SUCCESS;
  371. }
  372. //设置打印放大倍数(1-6)
  373. int ReceiptBase::SetScale(int iX, int iY)
  374. {
  375. iX--;
  376. iY--;
  377. if (iX < 0 || iX > 5) iX = 0;
  378. if (iY < 0 || iY > 5) iY = 0;
  379. unsigned char sCmd[4];
  380. sCmd[0]=0x1D;
  381. sCmd[1]=0x21;
  382. sCmd[2] = (iX << 4) + iY;
  383. int nRet = SendData(sCmd,3,300);
  384. m_cSerial.Flush();
  385. if (nRet < 0) return nRet;
  386. return RECEIPT_SUCCESS;
  387. }
  388. //设置黑白反显打印模式
  389. int ReceiptBase::SetInverse(bool b)
  390. {
  391. unsigned char sCmd[4];
  392. sCmd[0]=0x1D;
  393. sCmd[1]=0x42;
  394. sCmd[2] = b;
  395. int nRet = SendData(sCmd,3,300);
  396. m_cSerial.Flush();
  397. if (nRet < 0) return nRet;
  398. return RECEIPT_SUCCESS;
  399. }
  400. //选择/取消顺时针旋转90度
  401. int ReceiptBase::SetRotate(bool b)
  402. {
  403. unsigned char sCmd[4];
  404. sCmd[0]=0x1B;
  405. sCmd[1]=0x56;
  406. sCmd[2] = b;
  407. int nRet = SendData(sCmd,3,300);
  408. m_cSerial.Flush();
  409. if (nRet < 0) return nRet;
  410. return RECEIPT_SUCCESS;
  411. }
  412. //是否黑标纸
  413. int ReceiptBase::SetBlackFlag(bool b)
  414. {
  415. unsigned char sCmd[4];
  416. sCmd[0]=0x1B;
  417. sCmd[1]=0x63;
  418. sCmd[2]=0x30;
  419. sCmd[3]=b?153:0;
  420. int nRet = SendData(sCmd,4,300);
  421. m_cSerial.Flush();
  422. if (nRet < 0) return nRet;
  423. return RECEIPT_SUCCESS;
  424. }
  425. //设置黑标位置
  426. int ReceiptBase::SetBlackPos(int iLen)
  427. {
  428. int iRet = SetBlackFlag(true);
  429. if (iRet!=RECEIPT_SUCCESS)
  430. return iRet;
  431. unsigned char sCmd[6];
  432. sCmd[0]=0x1B;
  433. sCmd[1]=0x63;
  434. sCmd[2]=0x31;
  435. if (iLen < 19)
  436. iLen = 19;
  437. if (iLen > 65534)
  438. iLen = 65534;
  439. sCmd[3] = iLen%256;
  440. sCmd[4] = iLen/256;
  441. int nRet = SendData(sCmd,5,300);
  442. m_cSerial.Flush();
  443. if (nRet < 0) return nRet;
  444. return RECEIPT_SUCCESS;
  445. }
  446. //获取固件版本
  447. int ReceiptBase::GetFirmware(char* s)
  448. {
  449. unsigned char sCmd[128];
  450. sCmd[0]=0x1D;
  451. sCmd[1]=0x49;
  452. sCmd[2]=3;
  453. int nRet = SendData(sCmd,3,300);
  454. if (nRet < 0) return nRet;
  455. //Sleep(10);
  456. int iLen = ReadData(sCmd, 127, 300);
  457. if(iLen < 1)
  458. return RECEIPT_ERR_RCV_ERR;
  459. sCmd[iLen] = 0;
  460. strcpy(s, (char*)sCmd);
  461. return RECEIPT_SUCCESS;
  462. }
  463. //打印高速20,中速10,低速 00
  464. int ReceiptBase::SetPrintSpeed(short nSpeed)
  465. {
  466. return RECEIPT_SUCCESS;
  467. }
  468. int ReceiptBase::SetRowDistance(int nDis)
  469. {
  470. unsigned char cpSend[10]={0};
  471. int nRet = 0;
  472. if(nDis == 0)
  473. {
  474. cpSend[0]=0x1B;
  475. cpSend[1]=0x32;
  476. nRet = SendData(cpSend,2,300);
  477. m_iLineHeight = 30;
  478. }
  479. else
  480. {
  481. cpSend[0]=0x1B;
  482. cpSend[1]=0x33;
  483. cpSend[2]=(char)nDis&0xFF;
  484. nRet = SendData(cpSend,3,300);
  485. m_iLineHeight = nDis;
  486. }
  487. if (nRet < 0) return nRet;
  488. return RECEIPT_SUCCESS;
  489. }
  490. //设置左边空白
  491. int ReceiptBase::SetLeftDistance(int iLeft)
  492. {
  493. unsigned char cmd[]={0x1D, 0x4C, 0x00, 0x00};
  494. cmd[2] = iLeft%256;
  495. cmd[3] = iLeft/256;
  496. int nRet = SendData(cmd,4,300);
  497. if (nRet < 0) return nRet;
  498. return RECEIPT_SUCCESS;
  499. }
  500. //设置字符间隔,单位为点(0.125mm)
  501. int ReceiptBase::SetCharSpace(char iLeft, char iRight)
  502. {
  503. char iWide = (iLeft + iRight) >> 1;
  504. unsigned char cmd[]={0x1B, 0x20, iWide};
  505. int nRet = SendData(cmd,3,300);
  506. if (nRet < 0) return nRet;
  507. unsigned char cmd2[]={0x1C, 0x53, (char)iLeft, (char)iRight};
  508. SendData(cmd2,4,300);
  509. return RECEIPT_SUCCESS;
  510. }
  511. //设置打印区域
  512. int ReceiptBase::SetPrintArea(int iLeng)
  513. {
  514. unsigned char cmd[]={0x1D, 0x57, 0x00, 0x00};
  515. cmd[2] = iLeng%256;
  516. cmd[3] = iLeng/256;
  517. int nRet = SendData(cmd,4,300);
  518. if (nRet < 0) return nRet;
  519. return RECEIPT_SUCCESS;
  520. }
  521. // ctype 1 EAN13 =2CODE128-A =3 CODE128-B =4 CODE128-C
  522. // iBytePos 0不打印HRI字符 1条码上方打印 2条码下方打印 3条码上下方打印
  523. int ReceiptBase::PrinterCode(const char* cdata, UINT ctype,
  524. UINT iBytePos,UINT BarCodeWidth,UINT BarCodeHeight, bool bCharB)
  525. {
  526. if(iBytePos > 3) return RECEIPT_ERR_IN_ERR;
  527. if(strlen(cdata) > 80) return RECEIPT_ERR_IN_ERR;
  528. //字符打印位置 0不打印HRI字符 1条码上方打印 2条码下方打印 3条码上下方打印
  529. char szSendBuf[100];
  530. szSendBuf[0]=0x1D;
  531. szSendBuf[1]=0x48;
  532. szSendBuf[2]=iBytePos;
  533. int nRet = SendData((unsigned char*)szSendBuf, 3,3000);
  534. if(nRet < RECEIPT_SUCCESS) return nRet;
  535. if(BarCodeWidth<2)
  536. BarCodeWidth = 2;
  537. if(BarCodeWidth>6)
  538. BarCodeWidth = 6;
  539. //设置条形码宽度 2 - 6
  540. szSendBuf[0]=0x1D;
  541. szSendBuf[1]=0x77;
  542. szSendBuf[2]=BarCodeWidth;
  543. nRet = SendData((unsigned char*)szSendBuf, 3,3000);
  544. if(nRet < RECEIPT_SUCCESS) return nRet;
  545. memset(szSendBuf, 0x00, sizeof(szSendBuf));
  546. //条码高度,点
  547. szSendBuf[0]=0x1D;
  548. szSendBuf[1]=0x68;
  549. szSendBuf[2]=BarCodeHeight;
  550. m_iUsedLeng += BarCodeHeight;
  551. nRet = SendData((unsigned char*)szSendBuf, 3,3000);
  552. if(nRet < RECEIPT_SUCCESS) return nRet;
  553. memset(szSendBuf, 0x00, sizeof(szSendBuf));
  554. //条码下面字符的字体
  555. szSendBuf[0]=0x1D;
  556. szSendBuf[1]=0x66;
  557. szSendBuf[2]=bCharB;
  558. nRet = SendData((unsigned char*)szSendBuf, 3,3000);
  559. if(nRet < RECEIPT_SUCCESS) return nRet;
  560. memset(szSendBuf, 0x00, sizeof(szSendBuf));
  561. unsigned char cmdbuf[256] = {0x1d, 0x6b, };
  562. int iDatPos = 0;
  563. if (ctype > 0 && ctype < 9)
  564. cmdbuf[2] = ctype + 'A';
  565. else
  566. return RECEIPT_ERR_IN_ERR;
  567. int nInLen = strlen(cdata);
  568. cmdbuf[3] = nInLen;
  569. iDatPos = 4;
  570. memcpy(cmdbuf+iDatPos,cdata,nInLen);
  571. FeedLines(1);
  572. nRet = SendData(cmdbuf, iDatPos + nInLen + 1, 3000);
  573. if(nRet < RECEIPT_SUCCESS) return nRet;
  574. return RECEIPT_SUCCESS;
  575. }
  576. ///////////////////////////////////////////////////////////
  577. int ReceiptBase::Bit24ToBit1(const unsigned char* sInData, int iWidth, int iHigh, unsigned char** psOutData)
  578. {
  579. //存储模式为4字节对齐
  580. int iInXBytes = ((iWidth * 3) + 3) & 0xfffc;
  581. int iX, iY, iPos, iRGB;
  582. int iOutXytes = ((iWidth + 31) >> 3) & 0xfffc;
  583. int iSizeOut = iOutXytes * iHigh;
  584. unsigned char* sOutData = new unsigned char[iSizeOut];
  585. memset(sOutData, 0, iSizeOut);
  586. for(iY = 0; iY < iHigh; iY++)
  587. {
  588. for (iX = 0; iX < iWidth; iX++)
  589. {
  590. //在真彩图位置和数据
  591. iPos = (iInXBytes * iY) + iX * 3;
  592. iRGB = sInData[iPos++] + sInData[iPos++] + sInData[iPos];
  593. //在单色图位置
  594. if (iRGB > 100)
  595. {
  596. iPos = (iOutXytes * iY) + (iX >> 3);
  597. sOutData[iPos] |= (1 << (7-(iX % 8)));
  598. }
  599. }
  600. }
  601. *psOutData = sOutData;
  602. return iSizeOut;
  603. }
  604. int ReceiptBase::Bit1ToPrint(unsigned char* sInData, int iWidth, int iHigh, BOOL bRotate, unsigned char** psOutData)
  605. {
  606. //旋转后的位图
  607. int nBytesOfWidth = ((iWidth+31) >> 3) & 0xfffc; //bmp为标准4字节对齐
  608. int nBytesOfHeight = (iHigh+7) >> 3; //打印像素竖向排列
  609. int nBytesOutWidth = (iWidth+7) >> 3; //bmp为标准4字节对齐,输出为1字节对齐
  610. int iBufSize= nBytesOutWidth * nBytesOfHeight * 8;
  611. unsigned char *pOutData = new unsigned char[iBufSize];
  612. *psOutData = pOutData;
  613. memset(pOutData,0x00,iBufSize);
  614. //反色,BMP的 1 为白色的底色,与打印机定义相反
  615. int iTemp = 0;
  616. for(iTemp = 0; iTemp < nBytesOfWidth * iHigh; iTemp++)
  617. {
  618. sInData[iTemp] = 0xff - sInData[iTemp];
  619. }
  620. //BMP为横向排列,先从左到右再从下到上,
  621. if (bRotate)
  622. {
  623. //打印机下载数据为纵向排列,先从上到下再从左到右
  624. for (int iX = 0; iX < iWidth; iX++)
  625. {
  626. for (int iY = 0; iY < iHigh; iY++)
  627. {
  628. //计算在bmp中的位置
  629. int iYPos = iHigh - 1 - iY;//Y向倒转后的位置
  630. int iXPos = iX >> 3; //字节位置,/8
  631. //位图中字节位置
  632. iTemp = (nBytesOfWidth * iYPos) + iXPos;
  633. //字节中的位置
  634. iXPos = 7 - (iX % 8); //测试结果,高位在左
  635. bool bIsBlack = (sInData[iTemp] >> iXPos) & 1;
  636. //打印机单个字节内的顺序为从下到上
  637. //打印机字节位置
  638. iTemp = iX * nBytesOfHeight + (iY >> 3);
  639. //字节中的位置,需要Y向倒置
  640. iYPos = (7 - (iY % 8));
  641. pOutData[iTemp] |= (bIsBlack << iYPos);
  642. }
  643. }
  644. }
  645. else
  646. {
  647. int iRight = iWidth % 8;
  648. unsigned char chRight = 0;
  649. while (iRight)
  650. {
  651. chRight |= (1 << (8 - iRight));
  652. iRight--;
  653. }
  654. //打印机下载数据为从上到下
  655. for (int iY = 0; iY < iHigh; iY++)
  656. {
  657. //计算在bmp中的位置
  658. int iOldStart = nBytesOfWidth * (iHigh - 1 - iY);
  659. int iNewStart = nBytesOutWidth * iY;
  660. memcpy(pOutData + iNewStart, sInData + iOldStart, nBytesOutWidth);
  661. //此处可能会出现右侧黑边,需要处理最后一个字节像素
  662. if (chRight) *(pOutData + iNewStart + nBytesOutWidth - 1) &= chRight;
  663. }
  664. }
  665. return iBufSize;
  666. }
  667. //位图打印
  668. int ReceiptBase::GetBmpData(const char *sData, bool bBASE64, unsigned char** psOut, int* iPrintW, int* iPrintH, BOOL bRotate)
  669. {
  670. if (sData==NULL)
  671. return RECEIPT_ERR_IN_ERR;
  672. unsigned char* sBmpDat=NULL;
  673. int iBmpLen=0;
  674. if (bBASE64)
  675. {
  676. int iLen = strlen(sData);
  677. if (iLen < 256)
  678. return RECEIPT_ERR_IN_ERR;
  679. sBmpDat = new unsigned char[iLen+1];
  680. iBmpLen = ::Base64Decode(sData, iLen, sBmpDat, iLen);
  681. if (iBmpLen < 256) {
  682. delete sBmpDat;
  683. return RECEIPT_ERR_IN_ERR;
  684. }
  685. }
  686. else
  687. {
  688. char sName[128];
  689. if (sData==NULL || sData[0]==0)
  690. strcpy(sName,"logo.bmp");
  691. else
  692. strcpy(sName,sData);
  693. FILE* file = fopen(sName, "rb");
  694. if (file == NULL)
  695. return RECEIPT_ERR_IN_ERR;
  696. fseek(file, 0, SEEK_END);
  697. iBmpLen=ftell(file);
  698. fseek(file,0, SEEK_SET);
  699. if (iBmpLen<192)
  700. return RECEIPT_ERR_IN_ERR;
  701. sBmpDat=new unsigned char[iBmpLen+1];
  702. fread(sBmpDat, 1, iBmpLen,file);
  703. fclose(file);
  704. }
  705. ///////////////////////////////
  706. BITMAPFILEHEADER bfh;
  707. BITMAPINFOHEADER bih;
  708. int iReadPos = 0;
  709. memcpy(&bfh, sBmpDat, sizeof(bfh));
  710. iReadPos = sizeof(bfh);
  711. memcpy(&bih, sBmpDat + iReadPos, sizeof(bih));
  712. iReadPos = bfh.bfOffBits;
  713. int iSizeAll = (bih.biWidth * bih.biHeight * bih.biBitCount) >> 3;
  714. if (iBmpLen < iSizeAll + iReadPos) return RECEIPT_ERR_IN_ERR;
  715. //获得实际图像尺寸
  716. *iPrintW = bih.biWidth;
  717. *iPrintH = bih.biHeight;
  718. unsigned char* sTemp = NULL;
  719. if (bih.biBitCount == 24)
  720. {
  721. Bit24ToBit1(sBmpDat + iReadPos, bih.biWidth, bih.biHeight, &sTemp);
  722. iBmpLen = Bit1ToPrint(sTemp, bih.biWidth, bih.biHeight, bRotate, psOut);
  723. delete sBmpDat;
  724. return iBmpLen;
  725. }
  726. if (bih.biBitCount == 1)
  727. {
  728. iBmpLen = Bit1ToPrint(sBmpDat+iReadPos, bih.biWidth, bih.biHeight, bRotate, psOut);
  729. delete sBmpDat;
  730. return iBmpLen;
  731. }
  732. //////////////////////////////
  733. return RECEIPT_ERR_IN_ERR;
  734. }
  735. //功能:将单色 bmp 图像数据下载到硬件
  736. int ReceiptBase::DownloadNVBmp(const char *sData, bool bBASE64)
  737. {
  738. unsigned char* sBmpDat=NULL;
  739. int iWidth, iHeight;
  740. int iRet = GetBmpData(sData, bBASE64, &sBmpDat, &iWidth, &iHeight, TRUE);
  741. if (iRet < 0) return iRet;
  742. int iWidthByte = (iWidth+7) >> 3; //bmp4字节对齐,此处不需要,1字节对齐即可
  743. int iHeightByte = (iHeight+7) >> 3;
  744. m_iNVHeight = iHeightByte * 8;
  745. //下载位图
  746. unsigned char cmd1[8]={0x1C, 0x71, 1, 0, 0, 0, 0};
  747. cmd1[3] = iWidthByte;
  748. cmd1[5] = iHeightByte;
  749. SendData(cmd1, 7);
  750. // Sleep(10);
  751. int iMax = iWidthByte * iHeightByte * 8;
  752. for (int ia=0; ia<iMax; )
  753. {
  754. int iOnce = min(iMax - ia, 2000);
  755. SendData((unsigned char*)sBmpDat + ia, iOnce);
  756. ia += iOnce;
  757. Sleep(100);
  758. }
  759. // Sleep(50);
  760. delete sBmpDat;
  761. return RECEIPT_SUCCESS;
  762. }
  763. //打印NV位图
  764. int ReceiptBase::PrintNVBmp(bool bDH, bool bDW)
  765. {
  766. //打印位图
  767. unsigned char cmd2[]={0x1C,0x70,1,0};
  768. if (bDH) cmd2[3] += 2;
  769. if (bDW) cmd2[3] += 1;
  770. SendData(cmd2, 4);
  771. m_iUsedLeng += m_iNVHeight;
  772. return 0;
  773. }
  774. //功能:将单色 bmp 图像数据下载到硬件
  775. int ReceiptBase::PrintMemBmp(const char *sData, bool bBASE64)
  776. {
  777. //光栅位图方试
  778. unsigned char* sBmpDat=NULL;
  779. int iWidth, iHeight;
  780. int iRet = GetBmpData(sData, bBASE64, &sBmpDat, &iWidth, &iHeight, FALSE);
  781. if (iRet < 0) return iRet;
  782. int iWidthByte = (iWidth + 7) >> 3; //宽度字节数
  783. int iWidthDown = iWidthByte < 80 ? iWidthByte : 80;
  784. int iOutHeight = ((4096 / iWidthDown) / 24) * 24; //高度需要是24倍数
  785. unsigned char sSend[4*1024];
  786. sSend[0] = 0x1D; //光栅位图打印
  787. sSend[1] = 0x76;
  788. sSend[2] = 0x30;
  789. sSend[3] = 0;
  790. sSend[4] = iWidthDown; //宽度方向字节数
  791. sSend[5] = 0;
  792. sSend[7] = 0;
  793. int iSendPos;
  794. for (int iHPos = 0; iHPos < iHeight; iHPos += iOutHeight)
  795. {
  796. int iBmpHeight = (iHeight - iHPos < iOutHeight) ? iHeight - iHPos : iOutHeight;
  797. sSend[6] = iBmpHeight; //高度方向点数
  798. iSendPos = 8;
  799. for ( int ia = 0; ia < iBmpHeight; ia++)//宽度循环
  800. {
  801. memcpy(sSend + iSendPos, sBmpDat + iWidthByte * (iHPos + ia), iWidthDown);
  802. iSendPos += iWidthDown;
  803. }
  804. SendData(sSend, iSendPos);
  805. Sleep(100);
  806. }
  807. delete sBmpDat;
  808. m_iUsedLeng += iHeight;
  809. return RECEIPT_SUCCESS;
  810. }
  811. //原有,转换成字符串
  812. short ReceiptBase::GetBinBuf(short val,char *buf)
  813. {
  814. char sBufs[][8]=
  815. {
  816. "0000", "0001", "0010", "0011",
  817. "0100", "0101", "0110", "0111",
  818. "1000", "1001", "1010", "1011",
  819. "1100", "1101", "1110", "1111"
  820. };
  821. if (val >= 0 && val <= 15)
  822. {
  823. memcpy(buf, sBufs[val], 4);
  824. return 0;
  825. }
  826. return 1;
  827. }
  828. //原有,变量转化成字串
  829. short ReceiptBase::ByteToBytes(short value,char *buf)
  830. {
  831. char low_byte,high_byte,low_buf[5],high_buf[5];
  832. short ret1,ret2;
  833. memset(low_buf,'\0',5);
  834. memset(high_buf,'\0',5);
  835. if(value<0 || value>255)
  836. return 2; //非法值
  837. low_byte=value%16;
  838. high_byte=value/16;
  839. ret1=GetBinBuf(low_byte,low_buf);
  840. ret2=GetBinBuf(high_byte,high_buf);
  841. if(ret1!=0 || ret2!=0)
  842. return 1; //计算错误
  843. memcpy(buf,high_buf,4);
  844. memcpy(buf+4,low_buf,4);
  845. return 0;
  846. }
  847. int ReceiptBase::GetStateData(const char* sCmd1, const char* sCmd2, const char* sCmd3, const char* sCmd4)
  848. {
  849. if(sCmd2[1] == '1' || sCmd3[4]=='1') //打印机故障
  850. return RECEIPT_ERR_DEV_ERR;
  851. if(sCmd2[5] == '1') //打印机头抬起
  852. return RECEIPT_WARN_HEAD_UP;
  853. if(sCmd4[7] == '1') //卡纸
  854. return RECEIPT_WARN_JAMMED;
  855. if(sCmd4[2] == '1') //缺纸
  856. return RECEIPT_WARN_NO_MEDIA;
  857. if(sCmd4[4] == '1') //纸将尽
  858. return RECEIPT_WARN_PAP_LOW;
  859. return RECEIPT_SUCCESS;
  860. }
  861. int ReceiptBase::GetState()
  862. {
  863. char sState[4][16];
  864. int iRet = ReadStateData(NULL, sState[1], sState[2], sState[3]);
  865. if (iRet != RECEIPT_SUCCESS) return iRet;
  866. return GetStateData(NULL, sState[1], sState[2], sState[3]);
  867. }
  868. //发送状态查询命令
  869. int ReceiptBase::ReadStateData(short nNum,char * buf)
  870. {
  871. unsigned char szSendBuf[20];
  872. int ret,back;
  873. char chRcvbuf[10] = {0};
  874. short nRcvlen;
  875. int nRet = 0;
  876. memset(szSendBuf,'\0',20);
  877. szSendBuf[0]=0x10;
  878. szSendBuf[1]=0x04;
  879. szSendBuf[2]=(char)nNum;
  880. nRet = SendData(szSendBuf,3,300);
  881. if(nRet <=0 )
  882. {
  883. return RECEIPT_ERR_RCV_ERR;
  884. }
  885. //Sleep(10);
  886. nRcvlen = ReadData((unsigned char*)chRcvbuf, 1, 3000);
  887. if(nRcvlen < 1)
  888. {
  889. return RECEIPT_ERR_RCV_ERR;
  890. }
  891. back=chRcvbuf[0];
  892. ret=ByteToBytes(back,buf);
  893. if(ret!=0)
  894. {
  895. return RECEIPT_ERR_IN_ERR;
  896. }
  897. buf[8] = 0;
  898. return RECEIPT_SUCCESS;
  899. }
  900. //取状态
  901. int ReceiptBase::ReadStateData(char * buf1, char* buf2, char * buf3, char * buf4)
  902. {
  903. int nRet = 0;
  904. if(buf1)
  905. {
  906. nRet = ReadStateData(1,buf1);
  907. if(nRet != RECEIPT_SUCCESS)
  908. return nRet;
  909. }
  910. /////////////////
  911. if(buf2)
  912. {
  913. nRet = ReadStateData(2,buf2);
  914. if(nRet != RECEIPT_SUCCESS)
  915. return nRet;
  916. }
  917. /////////////////
  918. if(buf3)
  919. {
  920. nRet = ReadStateData(3,buf3);
  921. if(nRet != RECEIPT_SUCCESS)
  922. return nRet;
  923. }
  924. /////////////////
  925. if(buf4)
  926. {
  927. nRet = ReadStateData(4,buf4);
  928. return nRet;
  929. }
  930. return RECEIPT_SUCCESS;
  931. }