Epson TM-T88IV 热敏票据打印机
(或 M-T532热敏打印机芯 + BA-T500USB 控制板)
#include /* 标准输入、输出定义 */
#include /* UNIX 标准函数定义 */
#include /* 文件控制定义 */
#include /* 错误编号定义 */
#include /* POSIX 终端控制定义 */
#include /* 字符串函数定义 */
#define SERIAL_PORT "/dev/ttyS0" //COM 1
#define BAUDRATE B38400 //9600bps
#define MAX_SIZE 255
/** \brief 打开串行端口一
* \brief 成功后返回文件描述符,或是失败后返回-1
*/
int open_port()
{
int fd; /* 端口文件描述符 */
fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
return (fd);
}
void set_port(int fd)
{
struct termios options;
int flag;
tcgetattr(fd, &options);
fcntl(fd, F_SETFL, 0);
cfsetispeed(&options, BAUDRATE);
cfsetospeed(&options, BAUDRATE);
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(int fd)
{
close(fd);
}
int main(int argc, char *argv[])
{
int fd, i;
/* Open COM1 (/dev/ttyS0) and set for write mode! */
fd = open_port();
set_port(fd);
/* String data to be written */
char strOutput[] = "EPSON (CHINA) CORP.\x0A";
size_t sizeOutput = strlen(strOutput);
char strOutputCh[] = "爱普生(中国)有限公司\x0A";
size_t sizeOutputCh = strlen(strOutputCh);
/* Feed 4 lines */
char strFeed4Lines[] = "\x0A\x0A\x0A\x0A";
/* Set Font size */
char strSetFontSize[] = "\x1D\x21\x11";
/* Feed and cut paper */
char strCutPaper[] = "\x1D\x56\x42\x00";
/* 1.Normal size */
for (i = 0; i < 3; i++)
{
write(fd, &strOutput, sizeOutput);
write(fd, &strOutputCh, sizeOutputCh);
}
write(fd, &strFeed4Lines, 4);
/* 2.Double width */
strSetFontSize[2] = '\x10';
write(fd, &strSetFontSize, 3);
for (i = 0; i < 3; i++)
{
write(fd, &strOutput, sizeOutput);
write(fd, &strOutputCh, sizeOutputCh);
}
write(fd, &strFeed4Lines, 4);
/* 3.Double height */
strSetFontSize[2] = '\x01';
write(fd, &strSetFontSize, 3);
for (i = 0; i < 3; i++)
{
write(fd, &strOutput, sizeOutput);
write(fd, &strOutputCh, sizeOutputCh);
}
write(fd, &strFeed4Lines, 4);
/* 4.Set font to be 3x3 */
strSetFontSize[2] = '\x22';
write(fd, &strSetFontSize, 3);
for (i = 0; i < 3; i++)
{
write(fd, &strOutput, sizeOutput);
write(fd, &strOutputCh, sizeOutputCh);
}
write(fd, &strFeed4Lines, 4);
/* 5.Set font back to be default */
strSetFontSize[2] = '\x00';
write(fd, &strSetFontSize, 3);
for (i = 0; i < 3; i++)
{
write(fd, &strOutput, sizeOutput);
write(fd, &strOutputCh, sizeOutputCh);
}
write(fd, &strFeed4Lines, 4);
/* Feed and cut paper */
write(fd, &strCutPaper, 4);
close_port(fd);
return 0;
}