首先,必须了解所使用的打印机的波特率等信息。(否则有些功能无法实现)
1、通用串口设备文件 SerialDev.h
#ifndef SERIAL_DEV_H
#define SERIAL_DEV_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#ifndef RET_OK
#define RET_OK 0
#endif
#ifndef RET_ERR
#define RET_ERR -1
#endif
class SerialDev {
private:
#ifndef CMSPAR
static const int CMSPAR = 010000000000;
#endif
int mFd;
int setBaudRate(struct termios &opt, int speed)
{
speed_t s;
switch (speed) {
case 0 : s = B0; break;
case 50 : s = B50; break;
case 75 : s = B75; break;
case 110 : s = B110; break;
case 134 : s = B134; break;
case 150 : s = B150; break;
case 200 : s = B200; break;
case 300 : s = B300; break;
case 600 : s = B600; break;
case 1200 : s = B1200; break;
case 1800 : s = B1800; break;
case 2400 : s = B2400; break;
case 4800 : s = B4800; break;
case 9600 : s = B9600; break;
case 19200 : s = B19200; break;
case 38400 : s = B38400; break;
case 57600 : s = B57600; break;
case 115200 : s = B115200; break;
case 230400 : s = B230400; break;
default:
perror("invalid baud rate\n");
return RET_ERR;
}
cfsetispeed(&opt, s);
cfsetospeed(&opt, s);
return RET_OK;
}
int setDataBits(struct termios &opt, int data_bits)
{
opt.c_cflag &= ~CSIZE;
switch (data_bits) {
case 5: opt.c_cflag |= CS5; break;
case 6: opt.c_cflag |= CS6; break;
case 7: opt.c_cflag |= CS7; break;
case 8: opt.c_cflag |= CS8; break;
default:
perror("invalid data bits\n");
return RET_ERR;
}
opt.c_iflag &= ~ (IXON | IXOFF | IXANY);
opt.c_lflag &= ~ (ICANON | ECHO | ECHOE | ISIG);
opt.c_iflag &= ~ (INLCR | ICRNL | IGNCR);
opt.c_oflag &= ~(ONLCR | OCRNL);
return RET_OK;
}
int setParityChecking(struct termios &opt, char parity)
{
switch (parity) {
case 'n':
case 'N':
opt.c_cflag &= ~PARENB;
break;
case 'o':
case 'O':
opt.c_cflag |= PARENB;
opt.c_cflag |= PARODD;
break;
case 'e':
case 'E':
opt.c_cflag |= PARENB;
opt.c_cflag &= ~PARODD;
break;
case 'm':
case 'M':
opt.c_cflag |= PARENB | CS8 | PARODD | CMSPAR;
break;
case 's':
case 'S':
opt.c_cflag |= PARENB | CS8 | CMSPAR;
break;
default:
perror("invalid parity\n");
return RET_ERR;
}
return RET_OK;
}
int setStopBits(struct termios &opt, int stop_bits)
{
switch (stop_bits) {
case 1:
opt.c_cflag &= ~CSTOPB;
break;
case 2:
opt.c_cflag |= CSTOPB;
break;
default:
perror("invalid stop bits\n");
return RET_ERR;
}
return RET_OK;
}
int setCanonicalMode(struct termios &opt, bool icanon)
{
if (icanon) {
opt.c_lflag |= ICANON;
} else {
opt.c_lflag &= ~ICANON;
}
return RET_OK;
}
int setNoncanonicalOpt(struct termios &opt, unsigned char vmin, unsigned char vtime)
{
opt.c_cc[VMIN] = vmin;
opt.c_cc[VTIME] = vtime;
return RET_OK;
}
bool opened() { return mFd != -1; }
public:
SerialDev() : mFd(-1) { }
~SerialDev() { close(); }
int open(const char *dev, int flags = O_RDWR)
{
if ((mFd = ::open(dev, flags)) == RET_ERR) {
perror("open file failed\n");
return RET_ER