热敏打印机常用指令集
初始化打印机:Write(0x1b 0x40)
打印机实时状态:
/*
功能:返回打印机状态
参数:无
返回:
0 正常
1 线路不通
2 打印机故障
3 打印机缺纸
8 打印机纸将尽
*/
int GetPrintComStatus()
{
int ret = -1;
unsigned char str[3];
unsigned char res[50];
bool flag1=false;
bool flag2=false;
bool flag3=false;
memset(str,0x00,sizeof(str));
/*
* DLE EOT n 返回实时状态
* n=1:传输打印机状态
* n=2:传输离线状态
* n=3:传输错误状态
* n=4:传输纸卷传感器状态
*/
str[0] = 0x10;
str[1] = 0x04;
str[2] = 2;
ret = WritePort(str,3);
if(ret != 3)
return 1;
memset(res,0x00,sizeof(res));
if(ReadPort(res,1)<=0)
return 1;
if(res[0]&0x40||res[0]&0x04)
return 2;
if(res[0]==18)
flag2=true;
memset(str,0x00,sizeof(str));
str[0] = 0x10;
str[1] = 0x04;
str[2] = 3;
ret = WritePort(str,3);
if(ret != 3)
return 1;
memset( res,0x00,sizeof(res) );
if ( ReadPort(res,1)<=0 )
return 1;
if ( res[0]&0x80||res[0]&0x40||res[0]&0x08||res[0]&0x04 )
return 2;
if ( res[0]==18 )
flag1=true;
memset( str,0x00, sizeof(str));
str[0] = 0x10;
str[1] = 0x04;
str[2] = 4;
ret = WritePort(str,3);
if(ret != 3)
return 1;
memset(res,0x00,sizeof(res));
if ( ReadPort(res,1)<=0 )
return 1;
if(res[0]&0x60)
{
return 3; //缺纸
}
if(res[0]&0x0c)
{
return 8;//纸将尽
}
if(res[0]==18)
flag3=true;
if(flag1&&flag2&&flag3)
return 0;
else
return 1;
}
选择标准模式:Write( 0x1b, 0x53)
打印条形码:
int PrintBarCode(char *str)
{
int ret = -1;
int nlength;
unsigned char tmp[7];
memset(tmp,0x00,sizeof(tmp));
/*
* GS h n 选择条码高度
* n指定垂直方向点数,默认高度点数为n=162(十进制)
*/
tmp[0]=0x1d;
tmp[1]=0x68;
tmp[2]=0x3c;
WritePort(tmp,3);
memset(tmp,0x00,sizeof(tmp));
/*
* GS H n 选择可识别字符的打印位置
* 在打印条码时选择打印可识别字符的位置
*/
tmp[0]=0x1d;
tmp[1]=0x48;
tmp[2]=0x0;
WritePort(tmp,3);
memset(tmp,0x00,sizeof(tmp));
/*
* GS w n 选择条码宽度
* 默认值n=3;n=2、3、4点宽度
*/
tmp[0]=0x1d;
tmp[1]=0x77;
tmp[2]=0x2;
WritePort(tmp,3);
memset(tmp,0x00,sizeof(tmp));
/*
* GS k m d1...dk/GS k m n d1...dn打印条码
*/
tmp[0]=0x1d;
tmp[1]=0x6b;
tmp[2]=0x49;
tmp[3]=0x12;
tmp[4]=0x7b;
tmp[5]=0x41;
WritePort(tmp,6);
nlength = strlen(str);
ret = WritePort((unsigned char *)str,strlen(str));
if(ret == nlength)
return -1;
return 0;
}
切纸:Write(0x1b,0x69) 或 Write(0x1d,0x76,0x49) 或 Write(0x1d,0x56,0x42,0x00)