本文主要是介绍基于QT实现串口热敏打印机(ECS/POS)打印文本、波形(曲线),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
基于QT实现串口热敏打印机(ECS/POS)打印文本、波形(曲线)
QT 版本 5.6.2
适用打印机型号 : GY-Q586P GY-EP204X
打印文本的操作都很简单,
打印波形时要注意几个参数, 打印机默认精度 203ppi, 8dots/mm,速度70mm/s
打印机操作手册中有描述每个命令的用法,这里我加入一点自己的理解。
每个曲线可以视为一个个小线段组成,每个小线段与上一行和下一行的线段就组成了曲线,
每个小线段由首尾两个点组成(start point, end point)
要打印波形,我们首先要把波形数据转换位 点 的坐标位置
如果原始数据太小或太大就要进行相应的缩放。
如果原始数据的点数不可调, 但打印标准有要求,比如这些点必须打多长,
我们可以通过删除部分数据或者插入空白点(由切纸命令实现 1B 4A xx 走纸xx个点 */)来实现 缩短或者增加长度,但可能造成波形失真的情况,
如果有其他方式,还请告知。
点的坐标 是指一行中,点所在的位置,参考下图
坐标点转换为要发送的数据时,有几点注意事项,
-
发送的数据中一个点由两个字节组成分别表示该点的低阶和高阶(sl, sh 或 el, eh)
-
低阶 的取值范围 0到255, 高阶的取值范围 0到1, 就类似是256进制, 满256 高阶进1,
如果
start point 坐标是200(DEC) 不满256, 则发送的数据为 C8 00(HEX)
end point 坐标 是300 (DEC) 满256 ,则发送数据为 2C 01(HEX)
那么画这一行的小线段的命令就是 1D 27 01 C8 00 2C 01
其中 1D 27 01 是画线段的标识命令 -
start point 的坐要小于等于 end point的坐标
-
这里的代码和示例 是画一条曲线, 如果同时画n条曲线画, 也就是 一行会有n个 小线段
则命令形式为
1D 27 n sl1 sh1 el1 eh1 sl2 sh2 el2 eh2 … sln shn eln ehn
附代码如下, 仅供参考,如果发现问题还望指出。
(注意根据打印机型号修改波特率)
/** tlfs_printer.h** Created on: 2021-11-30* Author: Bruce Su*/#ifndef TLFS_PRINTER_H
#define TLFS_PRINTER_H#include <QObject>
#include <QtSerialPort>
#include <QString>
#include <QVector>class TLFS_Printer : public QObject
{Q_OBJECT
public:explicit TLFS_Printer(QObject *parent = 0);enum Text_Alignment {default_alignment,ALIGN_LEFT = 0x00611B,ALIGN_CENTER = 0x01611B,ALIGN_RIGHT = 0x02611B,UnknownAlignment = 0xFFFFFFFF};enum Text_Size {default_text_size,TEXT_SZIE_1 = 0x00211D,TEXT_SZIE_2 = 0x11211D,/** Do not SupportTEXT_SZIE_3 = 0x22211D,TEXT_SZIE_4 = 0x33211D,TEXT_SZIE_5 = 0x44211D,TEXT_SZIE_6 = 0x55211D,TEXT_SZIE_7 = 0x66211D,TEXT_SZIE_8 = 0x77211D,*/UnknownTextSize = 0xFFFFFFFF};void clear(void);void set_text(QString text);void set_font_size(TLFS_Printer::Text_Size text_size);void set_alignment(TLFS_Printer::Text_Alignment text_alignment);void start_print(void);void start_print(QString text, TLFS_Printer::Text_Alignment text_alignment, TLFS_Printer::Text_Size text_size);void print_segment(unsigned short pointa, unsigned short pointb);void print_ecg_wave(const QVector<double> &ecg_data);void print_ap_wave(const QVector<double> &ap_data);void print_ball_wave(const QVector<double> &ball_data);bool isNoPaper(void);bool isLidOpen(void);bool isTempVolOutOfRange(void);private:QSerialPort *serial;QByteArray buf;TLFS_Printer::Text_Size font_size;TLFS_Printer::Text_Alignment alignment;QString text_str;
};#endif // TLFS_PRINTER_H
/** tlfs_printer.cpp** Created on: 2021-11-30* Author: Bruce Su* Note: Compatiable with GY-Q586P GY-EP204X*/#include <QByteArray>
#include "tlfs_printer.h"
#include <QDebug>
#define SERIAL_PORT "ttymxc1"
#define CMD_CLEAR_BUFFER (0x401B)
#define CMD_CHECK_PRINTER_STATE (0x010410)
#define CMD_CHECK_OFFLINE_STATE (0x020410)
#define CMD_CHECK_ERROR_STATE (0x030410)
#define CMD_CHECK_PAPERSENSOR_STATE (0x040410)
#define CMD_PRINT_SEGMENT (0x01271D)
#define POINT_STAGE (256) //大小大于256 的点高阶置1, 低阶为减去256的值TLFS_Printer::TLFS_Printer(QObject *parent) : QObject(parent)
{serial = new QSerialPort;//设置串口名serial->setPortName(SERIAL_PORT);//打开串口serial->open(QIODevice::ReadWrite);//设置波特率(请修改为打印机要求的波特率)serial->setBaudRate(QSerialPort::Baud115200);//设置数据位数serial->setDataBits(QSerialPort::Data8);//设置奇偶校验serial->setParity(QSerialPort::NoParity);//设置停止位serial->setStopBits(QSerialPort::OneStop);//设置流控制serial->setFlowControl(QSerialPort::NoFlowControl);text_str = Q_NULLPTR;font_size = default_text_size;alignment = default_alignment;unsigned int tmp = CMD_CLEAR_BUFFER;serial->write((const char*)&tmp, 2);
}void TLFS_Printer::clear(void)
{unsigned int tmp = CMD_CLEAR_BUFFER;serial->write((const char*)&tmp, 2);
}void TLFS_Printer::start_print(QString text, TLFS_Printer::Text_Alignment text_alignment, TLFS_Printer::Text_Size text_size)
{QByteArray buf;unsigned int tmp = 0;if(text == Q_NULLPTR) return;buf.clear();if(text_size != UnknownTextSize && text_size != default_text_size){tmp = text_size;buf.append((const char*)&tmp, 3);}if(text_alignment != UnknownAlignment && text_alignment != default_alignment){tmp = text_alignment;buf.append((const char*)&tmp, 3);}buf.append(text.toLocal8Bit()); //防止中文乱码serial->write(buf);
}void TLFS_Printer::set_text(QString text)
{text_str = text;
}void TLFS_Printer::set_font_size(TLFS_Printer::Text_Size text_size)
{font_size = text_size;
}void TLFS_Printer::set_alignment(TLFS_Printer::Text_Alignment text_alignment)
{alignment = text_alignment;
}void TLFS_Printer::start_print(void)
{start_print(text_str, alignment, font_size);
}bool TLFS_Printer::isNoPaper(void)
{unsigned int tmp = CMD_CHECK_PAPERSENSOR_STATE;serial->write((const char*)&tmp, 3);tmp = 0;serial->waitForReadyRead(100);serial->getChar((char*)&tmp);if(tmp & 0x60) return true;return false;
}bool TLFS_Printer::isLidOpen(void)
{unsigned int tmp = CMD_CHECK_OFFLINE_STATE;serial->write((const char*)&tmp, 3);tmp = 0;serial->waitForReadyRead(100);serial->getChar((char*)&tmp);if(tmp & 0x04) return true;return false;
}bool TLFS_Printer::isTempVolOutOfRange(void)
{unsigned int tmp = CMD_CHECK_ERROR_STATE;serial->write((const char*)&tmp, 3);tmp = 0;serial->waitForReadyRead(100);serial->getChar((char*)&tmp);if(tmp & 0x40) return true;return false;
}/*打印从 点a到点b的线段*/
void TLFS_Printer::print_segment(unsigned short pointa, unsigned short pointb)
{char sl = 0, sh = 0, el = 0, eh = 0;QByteArray buf;buf.clear();//把原始坐标转化为要发送的数据if(pointa > pointb){sl = pointb % POINT_STAGE;sh = pointb / POINT_STAGE;el = pointa % POINT_STAGE;eh = pointa / POINT_STAGE;}else{sl = pointa % POINT_STAGE;sh = pointa / POINT_STAGE;el = pointb % POINT_STAGE;eh = pointb / POINT_STAGE;}unsigned int tmp = CMD_PRINT_SEGMENT;buf.append((const char*)&tmp, 3);buf.append(sl);buf.append(sh);buf.append(el);buf.append(eh);serial->write(buf);
}//画 心电波形
/* 高度满足要求 10mm/mv, 宽度需要结合信号发生器微调
* 打印机精度 8个点/mm 每行最多383个点
*/
void TLFS_Printer::print_ecg_wave(const QVector<double> &ecg_data)
{unsigned short pointa = 0;unsigned short pointb = 0;for(int i = 0; i < ecg_data.size(); ++i){if((ecg_data.at(i) > 3.0) || (ecg_data.at(i) < -1.0)){if(i == 0)pointa = 80 + 32;elsepointa = (ecg_data.at(i-1)) * 80 + 80 + 32; //放大数据}else{pointa = (ecg_data.at(i)) * 80 + 80 + 32;//放大数据}if(i == 0)pointa = pointb;print_segment(pointa, pointb);pointb = pointa; //记录本次的点,用于下一行时首尾相连}
}
//画 主动脉压力波形
void TLFS_Printer::print_ap_wave(const QVector<double> &ap_data)
{unsigned short pointa = 0;unsigned short pointb = 0;for(int i = 0; i < ap_data.size(); ++i){pointa = ap_data.at(i) * 2; //放大数据if(i == 0)pointa = pointb;print_segment(pointa, pointb);pointb = pointa;}
}//画 球囊压力波形
void TLFS_Printer::print_ball_wave(const QVector<double> &ball_data)
{unsigned short pointa = 0;unsigned short pointb = 0;for(int i = 0; i < ball_data.size(); ++i){pointa = ball_data.at(i);if(i == 0)pointa = pointb;print_segment(pointa, pointb);pointb = pointa;}
}
这篇关于基于QT实现串口热敏打印机(ECS/POS)打印文本、波形(曲线)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!