#include "commcontrol.h"
enum Excep
{
EXCEP_ONE,
EXCEP_TWO
};
CommControl::CommControl()
{
serialPort=new QSerialPort;
stopped=false;
if(stopped&&com_opened)
{
serialPort->close();
com_opened=false;
}
if(!com_opened)
{
//open com
// qDebug() << "Brush:" <<"---open com port------";
com_opened=true;
serialPort->setPortName("COM1");
serialPort->open(QIODevice::ReadWrite);
serialPort->setBaudRate(19200);
serialPort->setDataBits(QSerialPort::Data8);
serialPort->setParity(QSerialPort::NoParity);
serialPort->setStopBits(QSerialPort::OneStop);
// my_serialport->setStopBits(QSerialPort::TwoStop);
serialPort->setFlowControl(QSerialPort::NoFlowControl);
com_opened=true;
SetNormalFont();
}
}
bool CommControl::Dispose()
{\
bool Result = true;
if (serialPort->isOpen())
{
try
{
serialPort->close();
delete serialPort;
//serialPort->
}
catch(Excep ex)
{ Result = false; }
}
return Result;
}
bool CommControl::IsOpen()
{
bool Result = false;
// serialPort.Encoding = System.Text.Encoding.GetEncoding("GB2312");
if (serialPort->isOpen()) Result = true;
return Result;
}
bool CommControl::Write(QByteArray bdata)
{
try
{
if (IsOpen())
{
serialPort->write(bdata);
serialPort->waitForBytesWritten(-1);
qDebug()<<"send success";
return true;
}
else
{
return false;
}
}
catch(Excep ex)
{ return false; }
}
bool CommControl::Write(QString Data)
{
try
{
if (IsOpen())
{
QByteArray bData = Data.toLocal8Bit();
Write(bData);
return true;
}
else
{
return false;
}
}
catch(Excep ex) { return false; }
}
bool CommControl::WriteLine(QString Data)
{
bool Result = Write(Data);
if (Result) Result = NewRow();
return Result;
}
bool CommControl::PrintLine()
{
return WriteLine("=============================================");
}
bool CommControl::NewRow()
{
QByteArray temp;
temp.resize(1);
temp[0]=0x0A;
return Write(temp);
}
bool CommControl::NewRow(int iRow)
{
bool Result = false;
for (int i = 0; i < iRow; i++)
{
Result = NewRow();
if (!Result) break;
}
return Result;
}
bool CommControl::CutPaper()
{
QByteArray temp;
temp.resize(3);
temp[0]=0x1D;
temp[1]=0x56;
temp[2]=0x00;
qDebug()<<temp;
return Write(temp);
}
bool CommControl::SetNormalFont()
{
if (!IsOpen()) return false;
QByteArray temp;
try
{
//1D, 50 设置横向和纵向移动单位
temp.resize(4);
temp[0]=0x1D;
temp[1]=0x50;
temp[2]=0xB4;
temp[3]=0xB4;
serialPort->write(temp);
//1B, 53 选择标准模式
temp.resize(2);
temp[0]=0x1B;
temp[1]=0x53;
serialPort->write(temp);
//1B, 20 设置字符右间距
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x20;
temp[2]=0x00;
serialPort->write(temp);
//设置汉字字符左右间距
temp.resize(4);
temp[0]=0x1C;
temp[1]=0x53;
temp[2]=0x00;
temp[3]=0x00;
serialPort->write(temp);
//1D 42 是否反选打印 01反选/00取消
temp.resize(3);
temp[0]=0x1D;
temp[1]=0x42;
temp[2]=0x00;
serialPort->write(temp);
//1B 45 选择/取消加粗模式 01选择/00取消////////////
//temp = { 0x1B, 0x45, 0x00 };
//serialPort->write(temp);
//1B 7B 选择/取消倒置打印模式 01选择/00取消
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x7B;
temp[2]=0x00;
serialPort->write(temp);
//1B 2D 设置/取消下划线 01设置/00取消
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x2D;
temp[2]=0x00;
serialPort->write(temp);
//1B 2D 设置/取消汉字下划线 01设置/00取消
temp.resize(3);
temp[0]=0x1C;
temp[1]=0x2D;
temp[2]=0x00;
serialPort->write(temp);
//选择取消顺时针旋转90度 01选择 00取消
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x56;
temp[2]=0x00;
serialPort->write(temp);
//1B 45 选择/取消加粗模式 01选择/00取消
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x45;
temp[2]=0x00;
serialPort->write(temp);
//1B 24 设置绝对打印位置
temp.resize(4);
temp[0]=0x1B;
temp[1]=0x24;
temp[2]=0x00;
temp[3]=0x00;
serialPort->write(temp);
//1B, 33 设置行高, 18个像素
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x33;
temp[2]=0x20;
serialPort->write(temp);
//1B 4D 选择字体 03为汉字字体/////////
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x4D;
temp[2]=0x00;
serialPort->write(temp);
//1D 21 选择字体大小,默认
temp.resize(3);
temp[0]=0x1D;
temp[1]=0x21;
temp[2]=0x00;
serialPort->write(temp);
return true;
}
catch(Excep ex) { return false; }
}
bool CommControl::WriteBigLine(QString Data)
{
bool Result = true;
//Result = SetNormalFont();
if (!Result) return Result;
try
{
QByteArray temp;
//1B, 33 设置行高, 54个像素
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x33;
temp[2]=0x48;
serialPort->write(temp);
serialPort->waitForBytesWritten(-1);
//1B 4D 选择字体 03为汉字字体
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x4D;
temp[2]=0x03;
serialPort->write(temp);
serialPort->waitForBytesWritten(-1);
//横向放大和纵向放大不可同时作用
//1D 21 选择字体大小,横向放大1倍
//temp = { 0x1D, 0x21, 0x10 };
//serialPort->write(temp);
//1D 21 选择字体大小,纵向放大1倍
temp.resize(3);
temp[0]=0x1D;
temp[1]=0x21;
temp[2]=0x33;
serialPort->write(temp);
serialPort->waitForBytesWritten(-1);
//1B 45 选择/取消加粗模式 01选择/00取消
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x45;
temp[2]=0x01;
serialPort->write(temp);
serialPort->waitForBytesWritten(-1);
Write(Data);
Result = true;
}
catch(Excep ex) { Result = false; }
Result = SetNormalFont();
if (Result) Result = NewRow();
return Result;
}
bool CommControl::WriteLittleBigLine(QString Data)
{
bool Result = true;
//Result = SetNormalFont();
if (!Result) return Result;
try
{
QByteArray temp;
//1B, 33 设置行高, 54个像素
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x33;
temp[2]=0x48;
serialPort->write(temp);
serialPort->waitForBytesWritten(-1);
//1B 4D 选择字体 03为汉字字体
temp.resize(3);
temp[0]=0x1B;
temp[1]=0x4D;
temp[2]=0x03;
serialPort->write(temp);
serialPort->waitForBytesWritten
QT下无驱串口开发ESC-POS热敏打印机
4星 · 超过85%的资源 需积分: 33 34 浏览量
2016-07-24
19:35:52
上传
评论 7
收藏 2.63MB ZIP 举报
audiencewu
- 粉丝: 4
- 资源: 11
最新资源
- 基于yolov5识别算法实现的DNF自动脚本源码.zip
- 基于Python实现的自动化办公项目.zip
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页