Commit 92146f81 by guanghui.cui

小票打印,已完成:厨房、上菜单

parent 3b249878
......@@ -16,10 +16,14 @@
#include <QThread>
#include <QMapIterator>
#include <QTcpSocket>
#include <iconv.h>
#include <errno.h>
#include <stdio.h>
#include "QsLog.h"
#include "Util/IniDataManger.h"
#include "DClasses/Database.h"
#include "BillsManger.h"
#include "pos_print.h"
QThread g_thread;
......@@ -34,6 +38,28 @@ WBillControl::WBillControl()
this->moveToThread(&g_thread);
g_thread.start();
GetPosMenu();
//test print
OrderObject obj;
obj.channelName="美团外卖";
obj.order_id="3014384743591261407";
obj.order_index=56;
ComdObject comd1;
comd1.pid="061301";
comd1.productAmount=4;
obj.proList.push_back(&comd1);
ComdObject comd2;
comd2.pid="006114";
comd2.productAmount=1;
obj.proList.push_back(&comd2);
ComdObject comd3;
comd3.pid="009091";
comd3.productAmount=1;
obj.proList.push_back(&comd3);
//PrintLable(&obj);
printMenu(&obj);
// printCook(&obj,"061301",4);
// printCook(&obj,"006114",2);
// printCook(&obj,"002561",4);
}
WBillControl::~WBillControl()
......@@ -307,18 +333,184 @@ void WBillControl::setMenu(const QByteArray &JsonMenu)
void WBillControl::PrintLable(const OrderObject *obj)
{
QVector<QString> vecPid;
for(int i=0; i < obj->pvm.keys().count(); i++)
for(int i=0; i < obj->proList.size(); i++)
{
QString pid=obj->proList.at(i)->pid;
vecPid.push_back(pid);
int num = obj->proList.at(i)->productAmount;
QMap<QString,QVector<SetMenuItem>>::ConstIterator it;
it = mapSetMenu.find(pid);
if(it!=mapSetMenu.end()){
QLOG_INFO() << "------ setmenu pid:"<<pid;
QVector<SetMenuItem> vecItem = it.value();
for(SetMenuItem item:vecItem){
printCook(obj,item.strMenuId,item.num);
}
}
else{
QLOG_INFO() << "------ menu pid:"<<pid;
printCook(obj,pid,num);
}
}
}
//厨房单打印
void WBillControl::printCook(const OrderObject *obj,const QString& menuId,int num)
{
#ifdef Q_OS_LINUX
for(PrinterMenu menu:vecMenu){
if(menu.strMenuId==menuId){
//print
int fd;
fd = open_port("/dev/ttyS1");
set_port(fd,9600);
//fd = open_port(menu.strDevice.toStdString().data());
//set_port(fd,menu.iBaud);
print(menu.strTitle.toLatin1().data(),3);
QString strDesk=" 台号:";
strDesk+=obj->channelName;
strDesk+=QString::number(obj->order_index);
std::string stdDesk=strDesk.toStdString();
print(charset_u2g(stdDesk).data(),6);
QString order="单据:";
order+=obj->order_id;
std::string stdOrder=order.toStdString();
print(charset_u2g(stdOrder).data(),2);
QString datetime =QDateTime::currentDateTime().toString("日期:yyyy-MM-dd 时间:hh:mm");
std::string stdDatetime=datetime.toStdString();
print(charset_u2g(stdDatetime).data(),2);
print("----------------------------------------",1);
QByteArray menuName=QString::number(num).toLatin1();
menuName.append(" ");
menuName.append(menu.strMenuName.toLatin1());
print(menuName.data(),3);
print("",1);
print("",1);
print("",1);
print("",1);
cut_paper();
break;
}
}
#endif
}
//外卖单打印
void WBillControl::printTakeout()
{
}
//上菜单打印
void WBillControl::printMenu(const OrderObject *obj)
{
//#ifdef Q_OS_LINUX
int fd=-1;
for(int i=0; i < obj->proList.size(); i++)
{
QString pid=obj->proList.at(i)->pid;
int num = obj->proList.at(i)->productAmount;
for(PrinterMenu menu:vecMenu){
if(menu.strMenuId==pid){
//print
//...
//订单头 打印
if(0==i){
fd = open_port("/dev/ttyS1");
set_port(fd,19200);
print(menu.strOrderTitle.toLatin1().data(),3);
QString strDesk=" 台号:";
strDesk+=obj->channelName;
strDesk+=QString::number(obj->order_index);
std::string stdDesk=strDesk.toStdString();
print(charset_u2g(stdDesk).data(),4);
QString order="单据:";
order+=obj->order_id;
std::string stdOrder=order.toStdString();
print(charset_u2g(stdOrder).data(),2);
QString datetime =QDateTime::currentDateTime().toString("日期:yyyy-MM-dd 时间:hh:mm");
std::string stdDatetime=datetime.toStdString();
print(charset_u2g(stdDatetime).data(),2);
print("----------------------------------------",1);
}
QByteArray menuName=QString::number(num).toLatin1();
menuName.append(" ");
menuName.append(menu.strMenuName.toLatin1());
print(menuName.data(),3);
break;
}
}
}
if(-1!=fd){
//为空时换行
print("",1);
print("",1);
cut_paper();
}
//#endif
}
int WBillControl::charset_convert(const char* charset_from, const char* charset_to
, const std::string& string_from, std::string& string_to)
{
#ifdef Q_OS_LINUX
iconv_t cd = iconv_open(charset_to, charset_from);
if (!cd)
{
return -1;
}
string_to = "";
int ret = 0;
size_t inbytes = string_from.size();
char* inbuf = const_cast<char*>(string_from.c_str());
while (inbytes > 0)
{
char buffer[1024] = {0};
char *outbuf = buffer;
size_t outbytes = sizeof(buffer) - 1;
ret = iconv(cd, &inbuf, &inbytes, &outbuf, &outbytes);
if (ret == 0 || errno == E2BIG)
{
string_to.append(buffer);
}
else
{
break;
}
}
ret = iconv_close(cd);
if (inbytes != 0 || ret != 0)
{
return -1;
}
return 0;
#endif
return 0;
}
std::string WBillControl::charset_u2g(const std::string& utf8)
{
#ifdef Q_OS_LINUX
std::string gbk;
charset_convert("UTF-8", "GB18030//IGNORE", utf8, gbk);
//std::cout<<"convert res:"<<i<<std::endl;
return gbk;
#endif
return utf8;
}
......@@ -5,6 +5,7 @@
#include <QDateTime>
#include <QThread>
#include <QTcpServer>
#include <QMap>
#include "DataObject/OrderObject.h"
typedef struct {
......@@ -87,6 +88,29 @@ private:
void setMenu(const QByteArray &JsonMenu);
int charset_convert(const char* charset_from, const char* charset_to
, const std::string& string_from, std::string& string_to);
/* 功能:utf8转GB18030
* 参数:[1]utf8字符
* 返回:GB18030字符
* */
std::string charset_u2g(const std::string& utf8);
/* 功能:厨房单打印
* 参数:[1]订单信息
* 参数:[2]菜单id
* 参数:[3]数量
* 返回:
* */
void printCook(const OrderObject *obj,const QString& menuId,int num);
//外卖单打印
void printTakeout();
//上菜单打印
void printMenu(const OrderObject *obj);
public:
QVector<PrinterMenu> vecMenu;
QMap<QString,QVector<SetMenuItem>> mapSetMenu;
......
......@@ -74,7 +74,8 @@ HEADERS += MainForm.h \
Util/wbillcontrol.h \
sysTray.h \
DClasses/Database.h\
XCBOperate.h
XCBOperate.h \
pos_print.h
FORMS += MainForm.ui \
NotifyForm.ui \
......@@ -111,8 +112,9 @@ RCC_DIR = $$DESTDIR/Ui
unix {
QMAKE_LFLAGS += '-Wl,-rpath,\'\$$ORIGIN/lib\''
QMAKE_LFLAGS += -L. '-Wl,-rpath,\'\$$ORIGIN/lib\''
LIBS += -lxcb
LIBS += -lpos_print
}
DISTFILES +=
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.5.1, 2017-10-16T09:30:07. -->
<!-- Written by QtCreator 3.5.1, 2017-10-20T16:52:07. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......
......@@ -50,8 +50,8 @@ int main(int argc, char *argv[])
Logger& logger = Logger::instance();
logger.setLoggingLevel((Level)logInfo.level);
// 定向到日志文件
QDir().mkdir(appDir+"/Log");
QString logPath = appDir + "/Log/" + LOG_NAME;
QDir().mkdir(appDir+"/logs");
QString logPath = appDir + "/logs/" + LOG_NAME;
DestinationPtr fileDestination(DestinationFactory::MakeFileDestination(
logPath, EnableLogRotation, MaxSizeBytes(logInfo.size*1024*1024), MaxOldLogCount(logInfo.maxcount)));
logger.addDestination(fileDestination);
......
#ifndef _FMSPOSDLL_H_
#define _FMSPOSDLL_H_
int open_port(const char* szPort);
void close_port();
void set_port(int fd,int Baudrate);
void cut_paper();
//---------------------------------------------
// print函数说明
// data:要打印的数据
// type:打印类型(如下)
// 1、标准字体
// 2、标准加粗
// 3、两倍宽高
// 4、两倍宽高,加粗
// 5、三倍宽高
// 6、三倍宽高,加粗,红色
//---------------------------------------------
void print(const char *data,int type);
#endif
all:
g++ pos_print.c -fPIC -shared -o libpos_print.so
g++ main.c -lpos_print -L. -Wl,-rpath,'$$ORIGIN' -o test
#include "pos_print.h"
#include <stdio.h>
#include <string.h>
int main()
{
setbuf(stdout, NULL);
int fd, i;
fd = open_port("/dev/ttyS1");
set_port(fd,9600);
//char str[] = "EPSON (CHINA) CORP. \x0A\x0A\x0A\x0A\x0A\x0A\x1B\x69\x00";
//write(fd, &str, strlen(str));
print("\xC9\xCF\xB2\xCB\xB5\xA5 ",3);
print(" \xCC\xA8\xBA\xC5\xA3\xBA\x35\x30",5);
print("\xC8\xCB\xCA\xFD\xA3\xBA\x33 \xB5\xA5\xBE\xDD\xA3\xBA 12345678",1);
print("\xC8\xD5\xC6\xDA\xA3\xBA 20-10-20 \xCA\xB1\xBC\xE4\xA3\xBA 13:46",1);
print("--------------------------------------------",1);
print("1 \xBC\xE2\xBD\xB7\xD1\xBC\xCF\xC2\xB0\xCD",3);
print("2 \xB0\xD7\xD7\xC6\xBD\xE6\xC0\xBC",3);
print("3 \xB8\xC9\xB3\xB4\xC5\xA3\xC8\xE2\xBA\xD3\xB7\xDB",3);
cut_paper();
close_port();
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <string.h>
#include <iconv.h>
#include <stdlib.h>
#include "pos_print.h"
#define INVALID_HANDLE_VALUE -1
int g_Handle=INVALID_HANDLE_VALUE;
int g_uBaudrate=0;
int open_port(const char* szPort)
{
int fd; /* 端口文件描述符 */
if(g_Handle != INVALID_HANDLE_VALUE)
close(g_Handle);
g_Handle= open(szPort, O_RDWR | O_NOCTTY | O_NDELAY);
if (g_Handle == INVALID_HANDLE_VALUE)
printf("open_port failed\n");
else
printf("port is open ok \n");
return (g_Handle);
}
void set_port(int fd,int Baudrate)
{
struct termios options;
int flag;
tcgetattr(fd, &options);
fcntl(fd, F_SETFL, 0);
if(9600==Baudrate)
g_uBaudrate=B9600;
else if(19200==Baudrate)
g_uBaudrate=B19200;
cfsetispeed(&options, g_uBaudrate);
cfsetospeed(&options, g_uBaudrate);
options.c_cflag |= (CLOCAL | CREAD);
/* Set to 8N1 */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* HANDSHAKE 也称为 CRTSCTS */
options.c_cflag |= CRTSCTS;
/* RAW */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
/* For read data from printer */
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 1;
flag = tcsetattr(fd, TCSANOW, &options);
if (flag < 0)
{
perror("set_port: Setting port /dev/ttyS0 error - ");
}
}
void close_port()
{
if (g_Handle != INVALID_HANDLE_VALUE)
{
sleep(1);
int res = tcflush(g_Handle,TCIOFLUSH);
close(g_Handle);
g_Handle=INVALID_HANDLE_VALUE;
g_uBaudrate=0;
}
}
void cut_paper()
{
//char strCutPaper[] = "\x1D\x56\x42\x00";
char strCutPaper[] = "\x0A\x0A\x0A\x0A\x0A\x0A\x1B\x69\x00";
write(g_Handle, &strCutPaper, 9);
}
void print(char *data,int type)
{
printf("data:%s\n",data);
char *str = (char *)malloc(1024);
if(1==type){
strcpy(str,"\x1B\x40");
strcat(str,data);
}
else if(2==type){
strcpy(str,"\x1B\x40");
strcat(str,"\x1B\x21\x08");
strcat(str,data);
//strcat(str,"\x1B\x21\x00");
}
else if(3==type){
if(B9600==g_uBaudrate){
strcpy(str,"\x1B\x40\x1B\x21\x00\x1B\x33\x18\x1B\x52\x00\x1B\x63\x34\x05");
strcat(str," \x1C\x57\x01");
strcat(str,"\x1B\x21\x30");
strcat(str,data);
}
else{
strcpy(str,"\x1B\x40");
strcat(str,"\x1D\x21\x11");
//strcat(str,"\x1B\x21\x30");
strcat(str,data);
}
}
else if(4==type){
if(B9600==g_uBaudrate){
strcpy(str,"\x1B\x40\x1B\x21\x00\x1B\x33\x18\x1B\x52\x00\x1B\x63\x34\x05");
strcat(str," \x1C\x57\x01");
strcat(str,"\x1B\x21\x38");
strcat(str,data);
}
else{
strcpy(str,"\x1B\x40");
strcat(str,"\x1C\x57\x01");
strcat(str,"\x1B\x21\x38");
strcat(str,data);
}
}
else if(5==type){
if(B9600==g_uBaudrate){
strcpy(str,"\x1B\x40");
strcat(str," \x1C\x57\x01");
strcat(str,"\x1B\x21\x38");
strcat(str,data);
}
else{
strcpy(str,"\x1B\x40");
strcat(str,"\x1D\x21\x22");
strcat(str,data);
}
}
else if(6==type){
if(B9600==g_uBaudrate){
strcpy(str,"\x1B\x40");
strcat(str,"\x1C\x57\x01");
strcat(str,"\x1B\x21\x38");
strcat(str,"\x1B\x72\x01");
strcat(str,data);
}
else{
strcpy(str,"\x1B\x40");
strcat(str,"\x1D\x21\x22");
strcat(str,"\x1B\x72\x01");
strcat(str,data);
}
//strcat(str,"\x1B\x72\x00");
// strcat(str,"\x1B\x21\x00");
}
//char str[300];
//strcpy(str,data);
//strcat(str," thanks");
strcat(str," \x0A\x0D");
printf("str is:%s\n",str);
printf("str length:%d\n",strlen(str));
write(g_Handle, str, strlen(str));
free(str);
}
#ifndef _FMSPOSDLL_H_
#define _FMSPOSDLL_H_
int open_port(const char* szPort);
void close_port();
void set_port(int fd,int Baudrate);
void cut_paper();
//---------------------------------------------
// print函数说明
// data:要打印的数据
// type:打印类型(如下)
// 1、标准字体
// 2、标准加粗
// 3、两倍宽高
// 4、两倍宽高,加粗
// 5、三倍宽高
// 6、三倍宽高,加粗,红色
//---------------------------------------------
void print(char *data,int type);
#endif
#include <QCoreApplication>
#include <iostream>
#include <stdio.h>
#include <iconv.h>
#include <errno.h>
#include "pos_print.h"
int charset_convert(const char* charset_from, const char* charset_to
, const std::string& string_from, std::string& string_to)
{
iconv_t cd = iconv_open(charset_to, charset_from);
if (!cd)
{
return -1;
}
string_to = "";
int ret = 0;
size_t inbytes = string_from.size();
char* inbuf = const_cast<char*>(string_from.c_str());
while (inbytes > 0)
{
char buffer[1024] = {0};
char *outbuf = buffer;
size_t outbytes = sizeof(buffer) - 1;
ret = iconv(cd, &inbuf, &inbytes, &outbuf, &outbytes);
if (ret == 0 || errno == E2BIG)
{
string_to.append(buffer);
}
else
{
break;
}
}
ret = iconv_close(cd);
if (inbytes != 0 || ret != 0)
{
return -1;
}
return 0;
}
std::string charset_u2g(const std::string& utf8)
{
std::string gbk;
int i= charset_convert("UTF-8", "GB18030//IGNORE", utf8, gbk);
std::cout<<"convert res:"<<i<<std::endl;
return gbk;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout<<"hello world"<<std::endl;
// setbuf(stdout, NULL);
int fd;
fd = open_port("/dev/ttyS1");
set_port(fd,9600);
//QByteArray str=QString("例汤").toLocal8Bit();
//char lading[]="³¬ÖµÃÛÖ­²æÉÕ·¹/ÍâÂô";
//----中文----
//QByteArray zStr=QString("我的abc例汤").toUtf8();
std::string zz="上菜单";
print(const_cast<char*>(charset_u2g(zz).data()),3);
//----end-----
//----latin1------
std::string lading="1 ³¬ÖµÃÛÖ­²æÉÕ·¹/ÍâÂô";
QByteArray str=QString(lading.data()).toLatin1();
//----end--------
//print(str.data(),3);
std::string num=" 台号:50";
print(const_cast<char*>(charset_u2g(num).data()),5);
std::string str1="人数:3 单据:123456789";
print(const_cast<char*>(charset_u2g(str1).data()),1);
std::string str2="日期:20-10-24 时间:17:40";
print(const_cast<char*>(charset_u2g(str2).data()),1);
print("--------------------------------------------",1);
print(str.data(),3);
std::string menu2="2 Ôç²ÍÌØÖÆÔÆÍ̺ӷÛ";
print(QString(menu2.data()).toLatin1().data(),3);
std::string menu3="3 ÒøÌ©Ô±¹¤ÌײÍA1";
print(QString(menu3.data()).toLatin1().data(),3);
cut_paper();
close_port();
return 0;
}
#ifndef _FMSPOSDLL_H_
#define _FMSPOSDLL_H_
int open_port(const char* szPort);
void close_port();
void set_port(int fd,int Baudrate);
void cut_paper();
//---------------------------------------------
// print函数说明
// data:要打印的数据
// type:打印类型(如下)
// 1、标准字体
// 2、标准加粗
// 3、两倍宽高
// 4、两倍宽高,加粗
// 5、三倍宽高
// 6、三倍宽高,加粗,红色
//---------------------------------------------
void print(char *data,int type);
#endif
QT += core
QT -= gui
TARGET = printTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
unix {
QMAKE_LFLAGS += -L. '-Wl,-rpath,\'\$$ORIGIN\''
LIBS += -lpos_print
}
HEADERS += \
pos_print.h
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment