TSC打印机使用教程终极版(转)

最近公司做一个资产采集的项目,之前做过此类项目,不过没有整理资料,借这次机会写一下,做个记录。

本教程使用的打印机型号:TSC TTP-244 Plus     官方文档

一、TSC打印机安装

1.机器安装

根据官方快速安装指南安装打印机,此处不详细说明,也可以看视频教程,唯一需要注意的地方就是碳带的方向不要装错

产品说明书   

打印机初始化、感测器校正方法 

a) 两个手指同时按住PAUSE、FEED键,不要松手,同时开机。  

b) 待三个灯轮流闪时,只松开FEED键。待走纸,可松开PAUSE键。正常出纸是出大概2-3张标签纸。  

c) 按下FEED键,正常出纸为一张标签纸高度。并停在正常撕纸位置。  

2.安装驱动

驱动下载  

安装完驱动后,在页面设置修改下纸张大小,打印测试页。

二、程序调用

1.准备

相关文件:TSPL2指令集(中文版)  dll 

注册dll:新建bat文件,复制对应系统版本的命令,把下载的dll和bat命令文件放到同一目录,执行bat命令。

1 set source=.
2 set target=%windir%system32
3 echo 'Copy Files...'
4 copy %source%TSCActiveX.dll %target%
5 copy %source%TSCLIB.dll %target%
6 echo 'Regist Service'
7 regsvr32 %target%TSCActiveX.dll
32位
1 set source=.
2 set target=%windir%sysWOW64
3 echo 'Copy Files...'
4 copy %source%TSCActiveX.dll %target%
5 copy %source%TSCLIB.dll %target%
6 echo 'Regist Service'
7 regsvr32 %target%TSCActiveX.dll
64位系统

2.JavaScript方式调用

 1 <script type='text/javascript' language='javascript'>
 2 var d = new Date();
 3 var time = d.toLocaleString();
 4 var TSCObj;
 5 TSCObj = new ActiveXObject("TSCActiveX.TSCLIB");//引入插件
 6 //TSCObj.ActiveXabout();
 7 TSCObj.ActiveXopenport ("TSC TTP-244 Plus");//打开打印机端口
 8 TSCObj.ActiveXsetup ("99.5","70","5","8","0","2","0");//设置初始参数
 9 //TSCObj.ActiveXformfeed();
10 //TSCObj.ActiveXnobackfeed();
11 TSCObj.ActiveXsendcommand ("SET TEAR ON");
12 TSCObj.ActiveXclearbuffer();
13 TSCObj.ActiveXwindowsfont (260, 100, 36, 0, 0, 0, "arial", "办公耗材-标签纸");//打印文本
14 TSCObj.ActiveXwindowsfont (450, 170, 32, 0, 0, 0, "arial", time);//打印时间
15 //BARCODE X,Y,”code type”,height,human readable,rotation,narrow,wide,[alignment,]”content“
16 TSCObj.ActiveXbarcode ("100", "300", "128", "100", "2", "0", "2", "2", "PD102011");//打印条码
17 TSCObj.ActiveXprintlabel ("1","1");
18 TSCObj.ActiveXcloseport();//关闭端口
19 </script>
20 
21 Javascript代码
JavaScript

3.C#调用

 1 #region 调用TSC打印机打印条码
 2         /// <summary>
 3         /// 调用TSC打印机打印条码
 4         /// </summary>
 5         /// <param name="title">打印的标题</param>
 6         /// <param name="barCode">打印的条码编号</param>
 7         public static void TSC(string title, string barCode)
 8         {
 9             // 打开 打印机 端口.
10             TSCLIB_DLL.openport(p_port);
11             // 设置标签 宽度、高度 等信息.
12             // 宽 94mm  高 25mm
13             // 速度为4
14             // 字体浓度为8
15             // 使用垂直間距感測器(gap sensor)
16             // 两个标签之间的  间距为 3.5mm
17             TSCLIB_DLL.setup("94", "25", "4", "8", "0", "3.5", "0");
18             // 清除缓冲信息
19             TSCLIB_DLL.clearbuffer();
20             // 发送 TSPL 指令.
21             // 设置 打印的方向.
22             TSCLIB_DLL.sendcommand("DIRECTION 1");
23             // 打印文本信息.
24             // 在 (176, 16) 的坐标上
25             // 字体高度为34
26             // 旋转的角度为 0 度
27             // 2 表示 粗体.
28             // 文字没有下划线.
29             // 字体为 黑体.
30             // 打印的内容为:title
31             TSCLIB_DLL.windowsfont(176, 16, 34, 0, 2, 0, "宋体", title);
32             // 打印条码.
33             // 在 (176, 66) 的坐标上
34             // 以 Code39 的条码方式
35             // 条码高度 130
36             // 打印条码的同时,还打印条码的文本信息.
37             // 旋转的角度为 0 度
38             // 条码 宽 窄 比例因子为 7:12
39             // 条码内容为:barCode
40             TSCLIB_DLL.barcode("176", "66", "39", "130", "1", "0", "7", "12", barCode);
41             // 打印.
42             TSCLIB_DLL.printlabel("1", "1");
43             // 关闭 打印机 端口
44             TSCLIB_DLL.closeport();
45         }
46 #endregion
47 
48 C#代码
C#代码

4.Java调用

解压文件,将jna.jar包添加到项目  下载地址  

本示例打印的是二维码,由于官方文档中没有重写打印二维码的方法,我也懒得写了,直接使用的发送命令的方式打印。

 1 package com.zmkj.momo.admin;
 2 
 3 import com.sun.jna.Library;
 4 import com.sun.jna.Native;
 5 
 6 import java.text.SimpleDateFormat;
 7 import java.util.Date;
 8 
 9 /**
10  * TSC打印机测试
11  */
12 public class TscPrint {
13     public interface TscLibDll extends Library {
14         TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary("TSCLIB", TscLibDll.class);
15         int about();
16         int openport(String pirnterName);
17         int closeport();
18         int sendcommand(String printerCommand);
19         int setup(String width, String height, String speed, String density, String sensor, String vertical, String offset);
20         int downloadpcx(String filename, String image_name);
21         int barcode(String x, String y, String type, String height, String readable, String rotation, String narrow, String wide, String code);
22         int printerfont(String x, String y, String fonttype, String rotation, String xmul, String ymul, String text);
23         int clearbuffer();
24         int printlabel(String set, String copy);
25         int formfeed();
26         int nobackfeed();
27         int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);
28     }
29 
30 
31     public static void main(String[] args) {
32         System.setProperty("jna.encoding", "GBK");// 支持中文
33         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
34         String time = df.format(new Date());
35         String qrCode = "PD102011";
36         //TscLibDll.INSTANCE.about();
37         TscLibDll.INSTANCE.openport("TSC TTP-244 Plus");
38         //TscLibDll.INSTANCE.downloadpcx("C:\UL.PCX", "UL.PCX");
39         TscLibDll.INSTANCE.setup("99.5","70","5","8","0","2","0");
40         TscLibDll.INSTANCE.clearbuffer();
41         //TscLibDll.INSTANCE.sendcommand("PUTPCX 550,10,"UL.PCX"");
42         String command = "QRCODE 300,250,Q,8,A,0,M2,S7,"" + qrCode+"""; //打印二维码的参数和内容
43         TscLibDll.INSTANCE.sendcommand(command); //传送指令
44         TscLibDll.INSTANCE.windowsfont(260, 100, 36, 0, 0, 0, "arial", "办公耗材-标签纸");
45         TscLibDll.INSTANCE.windowsfont(450, 150, 32, 0, 0, 0, "arial", time);
46         TscLibDll.INSTANCE.printlabel("1", "1");
47         TscLibDll.INSTANCE.closeport();
48     }
49 }
50 
51 Java代码
Java代码

如果运行报错UnsatisfiedLinkError: Unable to load library “TSCLIB”...可以尝试把JDK换成32位版本。

 在调用过程中有不明白的地方看TSPL2说明书,上面有详细的指令用法以及参数说明!!!

原文地址:https://www.cnblogs.com/NetPig/p/9948132.html