其他应用程序-用C语言控制打印机

内容分享2天前发布
0 0 0

 在软件开发中,经常需要对打印机进行一系列的控制,如高密度打印、黑体字打印,下划线打印,图像打印等。本节讨论控制打印机的不同方法及针对EPSON LQ-1600K打印机构造几个有用的函数,其它类型的打印机换以相应的控制码即可。

1.利用biosprint()函数控制打印机

 该函数原型为:int biosprint(int cmd,int byte,int port)。函数biosprint()控制port指定的打印机接口。若port为0,则使用LPT1;若port为1,则访问LPT2。Cmd的值控制具体的功能(可参考Turbo C手册)。如发送一字符串到与LPT2链接的打印机。

 char p[]=“Good morning”;

 while(*p) biosprint(0,*p++,1);

 

2.利用fprintf(stdprn,char* format,arg_List)函数控制打印机

 stdprn是标准打印机设备,向其输出控制代码的格式化即可控制连接LPT1的打印机。如LQ-1600K打印机中高密度打印的控制码序列为0x16,0x47,发送命令为:fprintf(std-prn,”%c%c”,0x16,0x47)。

 

3.利用流概念向打印机文件LPT1,LPT2,LPT3,…发送控制码

 Turbo C中,文件I/O系统把流与文件(具有I/O功能的外部设备)连接起来。使用打印机如同使用普通的磁盘文件,仅仅是打印机具有特定的文件名称。如在LQ-1600K 打印机中,下划线打印的控制码序列为:0x1c,0x2d,n,其中n=0时取消打印下划线;n=1时,打印1点下划线;n=2时,打印2点下划线。其相应的命令为:

 fprintf(fp,”%c%c%d”,0x1c,0x2d,n) 其中fp=fopen(“LPT1”,”W”)。 

 

4.选择几种常用打印方式的函数(文本模式下)

a.初始化打印机函数

void int_printer(void)

{

 fprintf(stdprn,”%c%c”,0x16,0x40);//复原打印机工作状态

}

 

b.设定汉字模式

 打印机设定为汉字打印模式有两种方法:其一是通过硬DIP 开关来选择;其二是通过软转换,即发送相应的控制码序列。如下面的函数:

void chinese_type(void)

{

 fprintf(stdprn,”%c%c”,0x1c,0x26); 

}

 

c.汉字下划线打印

 设定汉字下划线打印模式时,汉字和下划线需打印两遍。先打一行汉字,第二遍打印下划线。两遍打印之间均走纸4/180英寸。

void underlined_print(char* p)

{

 fprintf(stdprn,”%c%c%c”,0x1c,0x2d,0x01);

 fprintf(stdprn,”%s”p);

 fprintf(stdprn,”%c%c%c”,0x1c,0x2d,0x02);

fprintf(stdprn,”%s”p);

 fprintf(stdprn,”%c%c%c”,0x1c,0x2d,0x00);//解除下划线方式

}

 

d.旋转90°打印

 旋转90°打印函数可使所有汉字字模中的文字符号均转动90°打印。每个字符按逆时针方向转动90°打印。函数如下:

void whirling_print(char* p)

{

 fprintf(stdprn,”%c%c”,0x1c,0x4a);

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c”,0x1c,0x4b);

}

 

e.汉字放大4倍打印

 本函数设置的打印高度、宽度均增加一倍。放大4倍的汉字字符需打印两遍。在当前行距上再增加24/180英寸的行距。函数如下:

void superwide_print(char* p)

{

 fprintf(stdprn,”%c%c%c”,0x1c,0x57,0x01);

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c%c”,0x1c,0x57,0x00);

}

 

f.汉字倍宽打印

 本函数打印倍宽字符,在图像方式下无效。函数如下:

void wide_print(char* p)

{

 fprintf(stdprn,”%c%c”,0x1c,0x0e);

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c”,0x1c,0x14);

}

 

g.汉字特殊字体打印

 汉字特殊字体有三种方式:空心字体、阴影字体、空心阴影字体。着三种字体常用于汉字打印的修饰。函数如下:

void special_Print(char* p)

{

fprintf(stdprn,”%c%c%c”,0x1b,0x71,0x01);//空心

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c%c”,0x1b,0x71,0x02);//阴影

 fprintf(stdprn,”%s”,p);

fprintf(stdprn,”%c%c%c”,0x1b,0x71,0x03);//空心阴影

 fprintf(stdprn,”%s”,p);

fprintf(stdprn,”%c%c%c”,0x1b,0x71,0x00);//普通字体

}

 

h.汉字加网络打印

 网纹的规格有五种:白字、黑子、点网、斜纹网、交叉纹网。加网区既可加网字区,也可以加网空心字区。汉字加网格打印用与修饰打印的汉字。加网格打印的控制码序列为:

 十六进制码:1b 28 58 n1 n2 a1 a2 a3

 其中n1 = 3;n2 = 0;a1 = 0.1;0≤a2≤4;a3= 0

 a1 = 0时指定加网字区;a1=1时指定加网空心字内区。

 a2 = 0时,网纹为白底;a2=1时,网文为黑体;a2=2 时,网文为点纹;a2=3时,网纹为斜纹网;a2=4时,网纹为交叉纹网。函数如下:

void plusnet_print(char* p)

{

 fprintf(stdprn,”%c%c%c”,0x1b,0x33,0x86);//调整换行量宽度

 fprintf(stdprn,”%c%c%c”,0x1c,0x57,0x01);//汉字放大四倍

 fprintf(stdprn,”%c%c%c”,0x1b,0x33,0x86);//设置空心字

 //黑体网点

 fprintf(stdprn,”%c%c%c%c%c%c%c%c”,0x1b,0x28,0x58,0x03,0x00,0x00,0x01,0x00);//调整换行量宽度

 fprintf(stdprn,”%s”,p);

 //点纹网点

 fprintf(stdprn,”%c%c%c%c%c%c%c%c”,0x1b,0x28,0x58,0x03,0x00,0x00,0x02,0x00);//调整换行量宽度

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c%c%c%c%c%c%c”,0x1b,0x28,0x58,0x03,0x00,0x00,0x03,0x00);

 fprintf(stdprn,”%s”,p);

 //交叉纹网点

 fprintf(stdprn,”%c%c%c%c%c%c%c%c”,0x1b,0x28,0x58,0x03,0x00,0x00,0x04,0x00);

 fprintf(stdprn,”%s”,p);

 //白底

 fprintf(stdprn,”%c%c%c%c%c%c%c%c”,0x1b,0x28,0x58,0x03,0x00,0x00,0x00,0x00);

 fprintf(stdprn,”%s”,p);

 //空心字加内网

 fprintf(stdprn,”%c%c%c%c%c%c%c%c”,0x1b,0x28,0x58,0x03,0x01,0x00,0x03,0x00);

 fprintf(stdprn,”%s”,p);

}

 

i.设定全角汉字字间距

 该函数设置中文字符的左右间距。n1,n2为特定的点数。字左为n1点数,字右为n2点数。n1和n2的单位都为1/180英寸。打印机接通时的默认值为n1=0,n2=3。

函数如下:

void cworddistance_print(char* p)

{

 int i;

 for(i=3;i<=24;i+=3)

{

  fprintf(stdprn,”%c%c%c%d”,0x1b,0x53,0x00,i);

 fprintf(stdprn,”%s”,p);

}

 

fprintf(stdprn,”%c%c%c%c”,0x1c,0x53,0x00,0x03);

}

 

5.文本模式下控制打印机的实例程序

//打印机文本状态的控制

#include <stdio.h>

#include <stdlib.h>

void int_printer(void);//初始化打印机

void chinese_type(void);//设置打印机为汉字方式

void emphasized_printer(char* p);//强调打印

void double_printer(char* p);//双重打印

void underlined_print(char* p);//汉字下划线打印

void italic_print(char* p);//斜体打印

void super_sub_print(char* p);//汉字上、下标打印

void cworddistance_print(char* p);//设置汉字字间距

void whirling_print(char* p);//旋转打印 

void wide_print(char* p);//加宽打印

void superwide_print(char* p);//汉字放大4倍打印

void special_print(char* p);//特殊字体打印

void plusnet_print(char* p);//加网格打印

 

void main()

{

 init_printer();

 chinese_type();

 empasized_printer(“计算机是信息时代的产物
“);

 double_printer(“计算机是信息时代的产物
“);

 underlined_print(“计算机是信息时代的产物
“);

 italic_print(“计算机是信息时代的产物
“);

 super_sub_print(“计算机是信息时代的产物
“);

 cworddistance_print(“计算机是信息时代的产物
“);

 whirling_print(“计算机是信息时代的产物
“);

 wide_print(“计算机是信息时代的产物
“);

 superwide_print(“计算机是信息时代的产物
“);

 special_print(“计算机是信息时代的产物
“);

 plusnet_print(“计算机是信息时代的产物
“);

 init_printer();

}

 

void int_printer(void)

{

 fprintf(stdprn,”%c%c”,0x16,0x40);//复原打印机工作状态

}

 

void chinese_type(void)

{

 fprintf(stdprn,”%c%c”,0x1c,0x26); 

}

 

void emphasized_printer(char* p)

{

 fprintf(stdprn,”%c%c”,0x1b,0x45);

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c”,0x1b,0x46);

}

 

void double_printer(char* p)

{

 fprintf(stdprn,”%c%c”,0x1b,0x47);

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c”,0x1b,0x48);

}

 

void underlined_print(char* p)

{

 fprintf(stdprn,”%c%c%c”,0x1c,0x2d,0x01);

 fprintf(stdprn,”%c%c%c”,0x1c,0x2d,0x02);

 fprintf(stdprn,”%c%c%c”,0x1c,0x2d,0x00);//解除下划线方式

}

 

void italic_print(char* p)

{

 fprintf(stdprn,”%c%c%c”,0x1c,0x2d,0x01);

 fprintf(stdprn,”%c%c%c”,0x1c,0x2d,0x02);

 fprintf(stdprn,”%c%c%c”,0x1c,0x2d,0x00);//解除下划线方式

}

 

void super_sub_print(char* p)

{

 fprintf(stdprn,”%c%c%c”,0x1c,0x72,0x00);

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c%c”,0x1c,0x72,0x01);

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c”,0x1c,0x12);

 

}

 

void cworddistance_print(char* p)

{

 register int i;

 

 for(i=3;i<=24;i+=3)

 {

  fprintf(stdprn,”%c%c%c%d”,0x1c,0x53,0x00,i);

  fprintf(stdprn,”%s”,p);

 }

 

 fprintf(stdprn,”%c%c%c%c”,0x1c,0x53,0x00,0x03);

}

 

void whirling_print(char* p)

{

 fprintf(stdprn,”%c%c”,0x1c,0x4a);

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c”,0x1c,0x4b);

}

 

void wide_print(char* p)

{

 fprintf(stdprn,”%c%c”,0x1c,0x0e);

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c”,0x1c,0x14);

}

 

void superwide_print(char* p)

{

 fprintf(stdprn,”%c%c%c”,0x1c,0x57,0x01);

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c%c”,0x1c,0x57,0x00);

}

 

void special_Print(char* p)

{

 fprintf(stdprn,”%c%c%c”,0x1b,0x71,0x01);//空心

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c%c”,0x1b,0x71,0x02);//阴影

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c%c”,0x1b,0x71,0x03);//空心阴影

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c%c”,0x1b,0x71,0x00);//普通字体

}

 

void plusnet_print(char* p)

{

 fprintf(stdprn,”%c%c%c”,0x1b,0x33,0x86);//调整换行量宽度

 fprintf(stdprn,”%c%c%c”,0x1c,0x57,0x01);//汉字放大四倍

 fprintf(stdprn,”%c%c%c”,0x1b,0x33,0x86);//设置空心字

 //黑体网点

 fprintf(stdprn,”%c%c%c%c%c%c%c%c”,0x1b,0x28,0x58,0x03,0x00,0x00,0x01,0x00);//调整换行量宽度

 fprintf(stdprn,”%s”,p);

 //点纹网点

 fprintf(stdprn,”%c%c%c%c%c%c%c%c”,0x1b,0x28,0x58,0x03,0x00,0x00,0x02,0x00);//调整换行量宽度

 fprintf(stdprn,”%s”,p);

 fprintf(stdprn,”%c%c%c%c%c%c%c%c”,0x1b,0x28,0x58,0x03,0x00,0x00,0x03,0x00);

© 版权声明

相关文章

暂无评论

none
暂无评论...