| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244 |
- #include "../pch/pch.h"
- #include "CPosPrinter.h"
- #include <winioctl.h>
- #include <setupapi.h>
- using boost::asio::ip::tcp;
- CPosPrinter::CPosPrinter(): m_socket(m_io)
- {
- }
- CPosPrinter::~CPosPrinter()
- {
- }
- bool CPosPrinter::InitShouyin()
- {
- //遍历USB设备,找到POS打印机路径
- //设备路径
- TCHAR* szDevicePath[MAX_DEVICE];
- //设置中文字符
- setlocale(LC_CTYPE, "chs");
- TCHAR* Port = NULL;
- //分配需要的空间
- for(int i = 0; i < MAX_DEVICE; i++)
- {
- szDevicePath[i] = new TCHAR[256];
- }
- //取设备路径
- int nDevice = GetDevicePath((LPGUID)&USB_GUID, szDevicePath);
- int i = 0;
- while(i < nDevice)
- {
- Port = szDevicePath[i++];
- LOG_INFO("device.Port = " << Port);
- }
- m_hPort = CreateFile(Port, GENERIC_READ | GENERIC_WRITE,
- 0, NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL, NULL);
- if(m_hPort == INVALID_HANDLE_VALUE)
- {
- // 打开端口失败
- return false;
- }
- return true;
- }
- void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
- {
- //设置模式,后面输出数据不会错
- m_type = 1;
- //初始化收银打印机的链接
- bool ret = InitShouyin();
- if(ret == false)
- {
- LOG_INFO("打开收银打印机端口失败!");
- return;
- }
- //读取当前收银打印机的设置
- std::string guige = CSetting::GetParam("setting_printer_guige");
- std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
- int n_lianshu = atoi(lianshu.c_str());
- //正式开始打印
- for(int i = 0; i < n_lianshu; i++)
- {
- POS_Reset();
- string shop_name = "#" + order.m_restaurant_number + " " + CLewaimaiString::UTF8ToANSI(order.m_shop_name);
- POS_TextOut(shop_name, true, 1);
- POS_FeedLine();
- POS_FeedLine();
- if(order.m_is_selftake == "1")
- {
- POS_TextOut("到店自取订单", false, 1);
- POS_FeedLine();
- POS_FeedLine();
- POS_TextOut("取餐号:" + CLewaimaiString::UTF8ToANSI(order.m_take_food_code), true, 1);
- POS_FeedLine();
- }
- else
- {
- POS_TextOut("外卖订单", false, 1);
- POS_FeedLine();
- }
- POS_FeedLine();
- //订单来源
- string from_type = "订单来源:" + CLewaimaiString::UTF8ToANSI(order.m_from_type);
- POS_TextOut(from_type);
- POS_FeedLine();
- //订单号
- string order_no = "订单号:" + CLewaimaiString::UTF8ToANSI(order.m_order_no);
- POS_TextOut(order_no);
- POS_FeedLine();
- //姓名
- if(order.m_is_selftake != "1")
- {
- string name = "姓名:" + CLewaimaiString::UTF8ToANSI(order.m_customer_name);
- POS_TextOut(name);
- POS_FeedLine();
- }
- //电话
- string phone = "电话:" + CLewaimaiString::UTF8ToANSI(order.m_phone);
- POS_TextOut(phone);
- POS_FeedLine();
- //地址
- if(order.m_is_selftake != "1")
- {
- string address = "地址:" + CLewaimaiString::UTF8ToANSI(order.m_address);
- POS_TextOut(address);
- POS_FeedLine();
- }
- //预设选项
- if(order.m_order_field.size() > 1)
- {
- for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
- {
- string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
- POS_TextOut(order_field, false, 0);
- POS_FeedLine();
- }
- }
- //下单时间
- string order_date = "下单时间:" + CLewaimaiString::UTF8ToANSI(order.m_order_date);
- POS_TextOut(order_date);
- POS_FeedLine();
- //配送时间
- if(order.m_is_selftake == "1")
- {
- string date = "自取时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
- POS_TextOut(date);
- POS_FeedLine();
- }
- else
- {
- string date = "配送时间:" + CLewaimaiString::UTF8ToANSI(order.m_delivery_date);
- POS_TextOut(date);
- POS_FeedLine();
- }
- //准备开始打印商品详情
- string lines;
- if(guige == "58")
- {
- lines = "--------------------------------";
- }
- else
- {
- lines = "------------------------------------------------";
- }
- POS_TextOut(lines);
- POS_FeedLine();
- //商品标题
- if(guige == "58")
- {
- POS_TextOut("商品 单价 数量 金额");
- }
- else
- {
- POS_TextOut("商品 单价 数量 金额");
- }
- POS_FeedLine();
- //商品内容
- for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
- {
- std::string food_name = (*it).m_food_name;
- std::string food_price = (*it).m_item_price;
- std::string quantity = (*it).m_quantity;
- //计算总价
- double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
- std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
- food_name = CLewaimaiString::UTF8ToANSI(food_name);
- //先输出商品名字
- std::vector<string> m_names;
- if(guige == "58")
- {
- m_names = HandleFoodname(food_name, 1);
- }
- else
- {
- m_names = HandleFoodname(food_name, 2);
- }
- std::string firstLine = m_names[0];
- std::string priceShow = HandleFoodItemPrice(food_price, 2);
- std::string quantityShow = HandleFoodQuantity(quantity, 2);
- std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
- std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
- POS_TextOut(firstLineShow, false, 0);
- POS_FeedLine();
- for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
- {
- POS_TextOut((*it), false, 0);
- POS_FeedLine();
- }
- }
- //判断是否有备注
- if(order.m_memo.length() > 0)
- {
- POS_FeedLine();
- string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
- POS_TextOut(address, false, 0);
- POS_FeedLine();
- }
- //结束商品详情打印
- POS_TextOut(lines);
- POS_FeedLine();
- //开始打印其他的费用
- bool is_other_money = false;
- if(order.m_delivery > 0)
- {
- string delivery = "配送费:" + CLewaimaiString::DoubleToString((double)order.m_delivery, 2);
- POS_TextOut(delivery, false, 0);
- POS_FeedLine();
- is_other_money = true;
- }
- if(order.m_is_dabao == "1")
- {
- string dabao = "打包费:" + CLewaimaiString::UTF8ToANSI(order.m_dabao_money);
- POS_TextOut(dabao, false, 0);
- POS_FeedLine();
- is_other_money = true;
- }
- //开始显示增值服务费
- if(order.m_addservie.size() > 0)
- {
- for(std::vector<CWaimaiOrderField>::iterator it = order.m_addservie.begin(); it != order.m_addservie.end(); it++)
- {
- string addservice = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::DoubleToString((double)atof((*it).value.c_str()), 2);
- POS_TextOut(addservice, false, 0);
- POS_FeedLine();
- is_other_money = true;
- }
- is_other_money = true;
- }
- if(order.m_discount < 10)
- {
- string discount = "打折:" + CLewaimaiString::DoubleToString((double)order.m_discount, 2) + "折";
- POS_TextOut(discount, false, 0);
- POS_FeedLine();
- is_other_money = true;
- }
- if(order.m_is_member_discount == "1")
- {
- string member_discount = "会员优惠:-" + order.m_member_discount;
- POS_TextOut(member_discount, false, 0);
- POS_FeedLine();
- is_other_money = true;
- }
- if(order.m_is_firstcut == "1")
- {
- string firstcut = "首单优惠:-" + order.m_firstcut_value;
- POS_TextOut(firstcut, false, 0);
- POS_FeedLine();
- is_other_money = true;
- }
- //满减
- string::size_type position = order.m_promotion.find(":");
- if(position != order.m_promotion.npos)
- {
- string promotion_value;
- promotion_value.assign(order.m_promotion, position + 1);
- string promotion = "满减优惠:-" + CLewaimaiString::DoubleToString((double)(atof(promotion_value.c_str())), 2);
- POS_TextOut(promotion, false, 0);
- POS_FeedLine();
- is_other_money = true;
- }
- if(order.m_coupon > 0)
- {
- string coupon = "优惠券:-" + CLewaimaiString::DoubleToString((double)order.m_coupon, 2);
- POS_TextOut(coupon, false, 0);
- POS_FeedLine();
- is_other_money = true;
- }
- if(atof(order.m_goods_coupon_value.c_str()) > 0.0001)
- {
- string coupon = "商品券:-" + order.m_goods_coupon_value;
- POS_TextOut(coupon, false, 0);
- POS_FeedLine();
- is_other_money = true;
- }
- if(is_other_money)
- {
- POS_TextOut(lines);
- POS_FeedLine();
- }
- //最后显示总价
- POS_TextOut("总计:¥" + CLewaimaiString::DoubleToString(order.m_price, 2), false, 2);
- POS_FeedLine();
- //显示付款方式
- POS_TextOut("支付方式:" + CLewaimaiString::UTF8ToANSI(order.m_pay_type), false, 2);
- POS_FeedLine();
- POS_FeedLine();
- POS_CutPaper();
- }
- }
- void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
- {
- //设置模式,后面输出数据不会错
- m_type = 2;
- //读取厨房打印机信息
- std::vector<ChufangPrinter> total_printers = CSetting::getChufangPrints();
- for(std::vector<ChufangPrinter>::iterator it = total_printers.begin(); it != total_printers.end(); it++)
- {
- ChufangPrinter printer = *it;
- std::string ip = printer.ip;
- //初始化连接
- try
- {
- boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string(ip.c_str()), 9100);
- m_socket.connect(ep);
- }
- catch(std::exception& e)
- {
- std::string err = e.what();
- LOG_INFO("连接厨房打印机失败,IP地址:" << ip.c_str() << ",错误信息:" << err.c_str());
- //连接失败了,处理下一个厨房打印机
- continue;
- }
- std::string guige = printer.guige;
- std::string fendan = printer.fendan;
- std::string fenlei = printer.fenlei;
- std::string fenlei_ids = printer.fenlei_ids;
- std::vector<CWaimaiOrderItem> cur_printer_use;
- if(fenlei == "0")
- {
- cur_printer_use = order.m_order_items;
- }
- else
- {
- //如果开启了分类打印,就要比对了
- std::map<string, bool> ids_map;
- std::vector<string> ids = CLewaimaiString::Split(fenlei_ids, ",");
- for(std::vector<string>::iterator it = ids.begin(); it != ids.end(); it++)
- {
- ids_map[(*it)] = true;
- }
- for(std::vector<CWaimaiOrderItem>::iterator it = order.m_order_items.begin(); it != order.m_order_items.end(); it++)
- {
- if(ids_map.find((*it).m_type_id) != ids_map.end())
- {
- //如果当前商品的分类,在厨房打印机设置的分类里面,才加入
- cur_printer_use.push_back(*it);
- }
- }
- }
- if(fendan == "0")
- {
- POS_Reset();
- POS_TextOut("派工单", true, 1);
- POS_FeedLine();
- POS_FeedLine();
- //预设选项
- if(order.m_order_field.size() > 1)
- {
- for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
- {
- string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
- POS_TextOut(order_field, false, 0);
- POS_FeedLine();
- }
- }
- //准备开始打印商品详情
- string lines;
- if(guige == "58")
- {
- lines = "--------------------------------";
- }
- else
- {
- lines = "------------------------------------------------";
- }
- POS_TextOut(lines);
- POS_FeedLine();
- //商品标题
- if(guige == "58")
- {
- POS_TextOut("商品 单价 数量 金额");
- }
- else
- {
- POS_TextOut("商品 单价 数量 金额");
- }
- POS_FeedLine();
- //商品内容
- for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
- {
- std::string food_name = (*it).m_food_name;
- std::string food_price = (*it).m_item_price;
- std::string quantity = (*it).m_quantity;
- //计算总价
- double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
- std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
- food_name = CLewaimaiString::UTF8ToANSI(food_name);
- //先输出商品名字
- std::vector<string> m_names;
- if(guige == "58")
- {
- m_names = HandleFoodname(food_name, 1);
- }
- else
- {
- m_names = HandleFoodname(food_name, 2);
- }
- std::string firstLine = m_names[0];
- std::string priceShow = HandleFoodItemPrice(food_price, 2);
- std::string quantityShow = HandleFoodQuantity(quantity, 2);
- std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
- std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
- POS_TextOut(firstLineShow, false, 0);
- POS_FeedLine();
- for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
- {
- POS_TextOut((*it), false, 0);
- POS_FeedLine();
- }
- }
- //判断是否有备注
- if(order.m_memo.length() > 0)
- {
- POS_FeedLine();
- string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
- POS_TextOut(address, false, 0);
- POS_FeedLine();
- }
- //结束商品详情打印
- POS_TextOut(lines);
- POS_FeedLine();
- POS_FeedLine();
- POS_CutPaper();
- }
- else
- {
- POS_Reset();
- //分单模式下,每个商品打印一张单
- for(std::vector<CWaimaiOrderItem>::iterator it = cur_printer_use.begin(); it != cur_printer_use.end(); it++)
- {
- POS_TextOut("派工单", true, 1);
- POS_FeedLine();
- POS_FeedLine();
- //预设选项
- if(order.m_order_field.size() > 1)
- {
- for(std::vector<CWaimaiOrderField>::iterator it = order.m_order_field.begin(); it != order.m_order_field.end() - 1; it++)
- {
- string order_field = CLewaimaiString::UTF8ToANSI((*it).name) + ":" + CLewaimaiString::UTF8ToANSI((*it).value);
- POS_TextOut(order_field, false, 0);
- POS_FeedLine();
- }
- }
- //准备开始打印商品详情
- string lines;
- if(guige == "58")
- {
- lines = "--------------------------------";
- }
- else
- {
- lines = "------------------------------------------------";
- }
- POS_TextOut(lines);
- POS_FeedLine();
- //商品标题
- if(guige == "58")
- {
- POS_TextOut("商品 单价 数量 金额");
- }
- else
- {
- POS_TextOut("商品 单价 数量 金额");
- }
- POS_FeedLine();
- std::string food_name = (*it).m_food_name;
- std::string food_price = (*it).m_item_price;
- std::string quantity = (*it).m_quantity;
- //计算总价
- double item_price = atof(food_price.c_str()) * atof(quantity.c_str());
- std::string food_total_price = CLewaimaiString::DoubleToString(item_price, 2);
- food_name = CLewaimaiString::UTF8ToANSI(food_name);
- //先输出商品名字
- std::vector<string> m_names;
- if(guige == "58")
- {
- m_names = HandleFoodname(food_name, 1);
- }
- else
- {
- m_names = HandleFoodname(food_name, 2);
- }
- std::string firstLine = m_names[0];
- std::string priceShow = HandleFoodItemPrice(food_price, 2);
- std::string quantityShow = HandleFoodQuantity(quantity, 2);
- std::string priceTotalShow = HandleFoodTotalPrice(food_total_price, 2);
- std::string firstLineShow = firstLine + priceShow + quantityShow + priceTotalShow;
- POS_TextOut(firstLineShow, false, 0);
- POS_FeedLine();
- for(std::vector<string>::iterator it = m_names.begin() + 1; it != m_names.end(); it++)
- {
- POS_TextOut((*it), false, 0);
- POS_FeedLine();
- }
- //判断是否有备注
- if(order.m_memo.length() > 0)
- {
- POS_FeedLine();
- string address = "顾客备注:" + CLewaimaiString::UTF8ToANSI(order.m_memo);
- POS_TextOut(address, false, 0);
- POS_FeedLine();
- }
- //结束商品详情打印
- POS_TextOut(lines);
- POS_FeedLine();
- POS_FeedLine();
- POS_CutPaper();
- }
- }
- m_socket.close();
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////
- //获取CreateFile的USB端口号
- ////////////////////////////////////////////////////////////////////////////////////////////////////////
- // 根据GUID获得设备路径
- // lpGuid: GUID指针
- // pszDevicePath: 设备路径指针的指针,用于返回找到的路径
- // 返回: 成功得到的设备路径个数,可能不止1个
- int CPosPrinter::GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath)
- {
- HDEVINFO hDevInfoSet;
- SP_DEVINFO_DATA spDevInfoData;
- SP_DEVICE_INTERFACE_DATA ifData;
- PSP_DEVICE_INTERFACE_DETAIL_DATA pDetail;
- int nCount;
- int nTotle;
- BOOL bResult;
- wstring strUSBPrint = TEXT("USB 打印支持");
- // 取得一个该GUID相关的设备信息集句柄
- hDevInfoSet = ::SetupDiGetClassDevs(lpGuid, // class GUID
- NULL, // 无关键字
- NULL, // 不指定父窗口句柄
- DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
- // 失败...
- if(hDevInfoSet == INVALID_HANDLE_VALUE)
- {
- printf("failed \r\n");
- return 0;
- }
- // 申请设备接口数据空间
- pDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)::GlobalAlloc(LMEM_ZEROINIT, INTERFACE_DETAIL_SIZE);
- pDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
- nTotle = -1;
- nCount = 0;
- bResult = TRUE;
- // 设备序号=0,1,2... 逐一测试设备接口,到失败为止
- while(bResult)
- {
- nTotle++;
- spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
- // 枚举符合该GUID的设备接口
- bResult = ::SetupDiEnumDeviceInfo(
- hDevInfoSet, // 设备信息集句柄
- (ULONG)nTotle, // 设备信息集里的设备序号
- &spDevInfoData); // 设备接口信息
- if(bResult)
- {
- DWORD DataT;
- TCHAR buf[MAX_PATH];
- DWORD nSize = 0;
- // get Friendly Name or Device Description
- if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
- SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
- {
- }
- else if(SetupDiGetDeviceRegistryProperty(hDevInfoSet, &spDevInfoData,
- SPDRP_DEVICEDESC, &DataT, (PBYTE)buf, sizeof(buf), &nSize))
- {
- }
- else
- {
- lstrcpy(buf, _T("Unknown"));
- }
- _tprintf(_T("buf = %s \r\n"), buf);
- //是否是要找的设备类型
- if(_tcscmp(buf, strUSBPrint.c_str()) != 0)
- {
- continue;
- }
- _tprintf(_T("OK\r\n"));
- ifData.cbSize = sizeof(ifData);
- // 枚舉符合該GUID的設備接口
- bResult = ::SetupDiEnumDeviceInterfaces(
- hDevInfoSet, // 設備信息集句柄
- NULL, // 不需額外的設備描述
- lpGuid, // GUID
- (ULONG)nTotle, // 設備信息集里的設備序號
- &ifData); // 設備接口信息
- if(bResult)
- {
- // 取得该设备接口的细节(设备路径)
- bResult = SetupDiGetInterfaceDeviceDetail(
- hDevInfoSet, // 设备信息集句柄
- &ifData, // 设备接口信息
- pDetail, // 设备接口细节(设备路径)
- INTERFACE_DETAIL_SIZE, // 输出缓冲区大小
- NULL, // 不需计算输出缓冲区大小(直接用设定值)
- NULL); // 不需额外的设备描述
- if(bResult)
- {
- // 复制设备路径到输出缓冲区
- ::_tcscpy_s(pszDevicePath[nCount], 256, pDetail->DevicePath);
- // 调整计数值
- nCount++;
- _tprintf(_T("Cnt = %d,pDetail->DevicePath =%s\r\n"), nCount, pDetail->DevicePath);
- }
- }
- }
- }
- // 释放设备接口数据空间
- ::GlobalFree(pDetail);
- // 关闭设备信息集句柄
- ::SetupDiDestroyDeviceInfoList(hDevInfoSet);
- return nCount;
- }
- int CPosPrinter::WriteData(string msg)
- {
- if(m_type == 1)
- {
- DWORD dwWrite;
- return WriteFile(m_hPort, msg.c_str(), (DWORD)msg.length(), &dwWrite, NULL);
- }
- else
- {
- try
- {
- m_socket.write_some(boost::asio::buffer(msg.c_str(), msg.length()));
- }
- catch (const std::exception& e)
- {
- LOG_INFO(e.what());
- }
- return 0;
- }
- }
- int CPosPrinter::WriteBuf(char* buf, int len)
- {
- if(m_type == 1)
- {
- DWORD dwWrite;
- return WriteFile(m_hPort, buf, len, &dwWrite, NULL);
- }
- else
- {
- try
- {
- m_socket.write_some(boost::asio::buffer(buf, len));
- }
- catch (const std::exception& e)
- {
- LOG_INFO(e.what());
- }
- return 0;
- }
- }
- int CPosPrinter::POS_Reset(void)
- {
- char s[2] = {0x1B, 0x40};
- WriteBuf(s, 2);
- return 0;
- }
- int CPosPrinter::POS_FeedLine(void)
- {
- char s[1] = {0x0A};
- WriteBuf(s, 1);
- return 0;
- }
- int CPosPrinter::POS_Feed(void)
- {
- char s[3] = { 0x1B, 0x4A, 0x00 };
- WriteBuf(s, 3);
- return 0;
- }
- int CPosPrinter::POS_SetMotionUnit(int x, int y)
- {
- string s;
- s = "\x1D\x50\xB4\xB4";
- WriteData(s);
- s = "\x1B\x53";
- WriteData(s);
- return 0;
- }
- int CPosPrinter::POS_SET_MOVE_X()
- {
- char s2[6] = { 0x1B, 0x44, 0x0C, 0x0E, 0x17, 0x00};
- WriteBuf(s2, 6);
- return 0;
- }
- int CPosPrinter::POS_MOVE_X()
- {
- char s[1] = { 0x09 };
- WriteBuf(s, 1);
- return 0;
- }
- int CPosPrinter::POS_SET_ABS_X(int x, int y)
- {
- char cx = (char)(x);
- char cy = (char)(y);
- char s1[4] = { 0x1B, 0x24, cx, cy };
- WriteBuf(s1, 4);
- return 0;
- }
- int CPosPrinter::POS_SET_PRINT_AREA(int x, int y)
- {
- char cx = (char)(x);
- char cy = (char)(y);
- char s1[4] = { 0x1D, 0x57, cx, cy };
- WriteBuf(s1, 4);
- return 0;
- }
- /*
- *align_type:0 左对齐 1 居中对齐 2右对齐
- **/
- int CPosPrinter::POS_TextOut(string abc, bool is_double, int align_type)
- {
- if(is_double)
- {
- char s1[3] = { 0x1B, 0x21, 0x30 };
- WriteBuf(s1, 3);
- char s2[3] = { 0x1C, 0x21, 0x0c };
- WriteBuf(s2, 3);
- }
- else
- {
- char s1[3] = { 0x1B, 0x21, 0x00 };
- WriteBuf(s1, 3);
- char s2[3] = { 0x1C, 0x57, 0x00 };
- WriteBuf(s2, 3);
- }
- if(align_type == 0)
- {
- char s1[3] = { 0x1B, 0x61, 0x00 };
- WriteBuf(s1, 3);
- }
- else if(align_type == 1)
- {
- char s1[3] = { 0x1B, 0x61, 0x01 };
- WriteBuf(s1, 3);
- }
- else if(align_type == 2)
- {
- char s1[3] = { 0x1B, 0x61, 0x02 };
- WriteBuf(s1, 3);
- }
- else
- {
- }
- WriteData(abc);
- return 0;
- }
- int CPosPrinter::POS_CutPaper()
- {
- char s[4] = { 0x1D, 0x56, 0x41, 0x00 };
- WriteBuf(s, 4);
- return 0;
- }
- int CPosPrinter::POS_OutQRCode()
- {
- char QRCode1[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x43, 0x05 };
- char QRCode2[16] = { 0x1d, 0x28, 0x6b, 0x0b, 0x00, 0x31, 0x50, 0x30, 0x47, 0x70, 0x72, 0x69,
- 0x6e, 0x74, 0x65, 0x72
- };
- char QRCode3[8] = { 0x1d, 0x28, 0x6b, 0x03, 0x00, 0x31, 0x51, 0x30 };
- WriteBuf(QRCode1, 8);
- WriteBuf(QRCode2, 16);
- WriteBuf(QRCode3, 8);
- return 0;
- }
- void CPosPrinter::CalWord(string s, int& nHanzi, int& nZimu)
- {
- nHanzi = 0;
- nZimu = 0;
- const char* buffer = s.c_str();
- while(*buffer++ != '\0')
- {
- if(!(*buffer >= 0 && *buffer <= 127))
- {
- //汉字
- buffer++;
- nHanzi++;
- }
- else
- {
- //字母
- nZimu++;
- }
- }
- }
- /*
- *规格 1:58mm 2:80mm
- **/
- std::vector<std::string>CPosPrinter::HandleFoodname(std::string oldname, int guige)
- {
- std::vector<std::string> newnameArray;
- int nHanzi, nZimu;
- CalWord(oldname, nHanzi, nZimu);
- int nWidth = nHanzi * 2 + nZimu;
- int maxWidth;
- if(guige == 1)
- {
- maxWidth = 15;
- }
- else
- {
- maxWidth = 28;
- }
- if(nWidth <= maxWidth)
- {
- //对于nWidth补空格
- for(int i = 0; i < maxWidth - nWidth; i++)
- {
- oldname += " ";
- }
- newnameArray.push_back(oldname);
- return newnameArray;
- }
- //宽度大于15的情况,如果超过了,就要进行换行截取
- const char* s = oldname.c_str();
- int nTmp = 0;
- int nTotal = 0;
- while(*s++ != '\0')
- {
- nTmp++;
- nTotal++;
- if(!(*(s) >= 0 && *(s) <= 127))
- {
- //汉字的情况
- s++;
- nTmp++;
- nTotal++;
- }
- if(nTmp == maxWidth)
- {
- //这里开始要换行了
- string newnameItem;
- newnameItem.assign(oldname, nTotal - nTmp, nTmp);
- newnameArray.push_back(newnameItem);
- nTmp = 0;
- }
- else if(nTmp == maxWidth - 1)
- {
- //如果是第14个了,判断下一个是不是中文,如果是的话也要换行
- if(!(*(s + 1) >= 0 && *(s + 1) <= 127))
- {
- //下一个是中文,也要换行了,补齐一个空格
- string newnameItem;
- newnameItem.assign(oldname, nTotal - nTmp, nTmp);
- newnameItem += " ";
- newnameArray.push_back(newnameItem);
- nTmp = 0;
- }
- }
- }
- if(nTmp > 0)
- {
- //处理分隔后的最后一行
- string newnameItem;
- newnameItem.assign(oldname, nTotal - nTmp, nTmp);
- for(int i = 0; i < maxWidth - nTmp; i++)
- {
- newnameItem += " ";
- }
- newnameArray.push_back(newnameItem);
- }
- return newnameArray;
- }
- std::string CPosPrinter::HandleFoodItemPrice(std::string oldprice, int guige)
- {
- double price = atof(oldprice.c_str());
- if(price < 10.00)
- {
- //单位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else if(price > 9.99 && price < 100.00)
- {
- //双位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else if(price > 99.99 && price < 1000.00)
- {
- //三位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else
- {
- //四位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- }
- std::string CPosPrinter::HandleFoodQuantity(std::string oldquantity, int guige)
- {
- int n = atoi(oldquantity.c_str());
- if(n < 10)
- {
- if(guige == 1)
- {
- return " " + oldquantity;
- }
- else
- {
- return " " + oldquantity;
- }
- }
- else if(n >= 10 && n <= 99)
- {
- if(guige == 1)
- {
- return " " + oldquantity;
- }
- else
- {
- return " " + oldquantity;
- }
- }
- else
- {
- if(guige == 1)
- {
- return "" + oldquantity;
- }
- else
- {
- return " " + oldquantity;
- }
- }
- }
- std::string CPosPrinter::HandleFoodTotalPrice(std::string oldprice, int guige)
- {
- double price = atof(oldprice.c_str());
- if(price < 10.00)
- {
- //单位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else if(price > 9.99 && price < 100.00)
- {
- //双位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else if(price > 99.99 && price < 1000.00)
- {
- //三位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- else
- {
- //四位数
- if(guige == 1)
- {
- return " " + oldprice;
- }
- else
- {
- return " " + oldprice;
- }
- }
- }
|