最近公司做一個資產采集的項目,之前做過此類項目,不過沒有整理資料,借這次機會寫一下,做個記錄。
本教程使用的打印機型號: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命令。

set source=. set target=%windir%\system32 echo 'Copy Files...' copy %source%\TSCActiveX.dll %target% copy %source%\TSCLIB.dll %target% echo 'Regist Service' regsvr32 %target%\TSCActiveX.dll

set source=. set target=%windir%\sysWOW64 echo 'Copy Files...' copy %source%\TSCActiveX.dll %target% copy %source%\TSCLIB.dll %target% echo 'Regist Service' regsvr32 %target%\TSCActiveX.dll
2.JavaScript方式調用

<script type='text/javascript' language='javascript'>
var d = new Date(); var time = d.toLocaleString(); var TSCObj; TSCObj = new ActiveXObject("TSCActiveX.TSCLIB");//引入插件 //TSCObj.ActiveXabout();
TSCObj.ActiveXopenport ("TSC TTP-244 Plus");//打開打印機端口
TSCObj.ActiveXsetup ("99.5","70","5","8","0","2","0");//設置初始參數 //TSCObj.ActiveXformfeed(); //TSCObj.ActiveXnobackfeed();
TSCObj.ActiveXsendcommand ("SET TEAR ON"); TSCObj.ActiveXclearbuffer(); TSCObj.ActiveXwindowsfont (260, 100, 36, 0, 0, 0, "arial", "辦公耗材-標簽紙");//打印文本
TSCObj.ActiveXwindowsfont (450, 170, 32, 0, 0, 0, "arial", time);//打印時間 //BARCODE X,Y,”code type”,height,human readable,rotation,narrow,wide,[alignment,]”content“
TSCObj.ActiveXbarcode ("100", "300", "128", "100", "2", "0", "2", "2", "PD102011");//打印條碼
TSCObj.ActiveXprintlabel ("1","1"); TSCObj.ActiveXcloseport();//關閉端口
</script>
3.C#調用

#region 調用TSC打印機打印條碼
/// <summary>
/// 調用TSC打印機打印條碼 /// </summary>
/// <param name="title">打印的標題</param>
/// <param name="barCode">打印的條碼編號</param>
public static void TSC(string title, string barCode) { // 打開 打印機 端口.
TSCLIB_DLL.openport(p_port); // 設置標簽 寬度、高度 等信息. // 寬 94mm 高 25mm // 速度為4 // 字體濃度為8 // 使用垂直間距感測器(gap sensor) // 兩個標簽之間的 間距為 3.5mm
TSCLIB_DLL.setup("94", "25", "4", "8", "0", "3.5", "0"); // 清除緩沖信息
TSCLIB_DLL.clearbuffer(); // 發送 TSPL 指令. // 設置 打印的方向.
TSCLIB_DLL.sendcommand("DIRECTION 1"); // 打印文本信息. // 在 (176, 16) 的坐標上 // 字體高度為34 // 旋轉的角度為 0 度 // 2 表示 粗體. // 文字沒有下划線. // 字體為 黑體. // 打印的內容為:title
TSCLIB_DLL.windowsfont(176, 16, 34, 0, 2, 0, "宋體", title); // 打印條碼. // 在 (176, 66) 的坐標上 // 以 Code39 的條碼方式 // 條碼高度 130 // 打印條碼的同時,還打印條碼的文本信息. // 旋轉的角度為 0 度 // 條碼 寬 窄 比例因子為 7:12 // 條碼內容為:barCode
TSCLIB_DLL.barcode("176", "66", "39", "130", "1", "0", "7", "12", barCode); // 打印.
TSCLIB_DLL.printlabel("1", "1"); // 關閉 打印機 端口
TSCLIB_DLL.closeport(); } #endregion
4.Java調用
解壓文件,將jna.jar包添加到項目 下載地址
本示例打印的是二維碼,由於官方文檔中沒有重寫打印二維碼的方法,我也懶得寫了,直接使用的發送命令的方式打印。

package com.zmkj.momo.admin; import com.sun.jna.Library; import com.sun.jna.Native; import java.text.SimpleDateFormat; import java.util.Date; /** * TSC打印機測試 */
public class TscPrint { public interface TscLibDll extends Library { TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary("TSCLIB", TscLibDll.class); int about(); int openport(String pirnterName); int closeport(); int sendcommand(String printerCommand); int setup(String width, String height, String speed, String density, String sensor, String vertical, String offset); int downloadpcx(String filename, String image_name); int barcode(String x, String y, String type, String height, String readable, String rotation, String narrow, String wide, String code); int printerfont(String x, String y, String fonttype, String rotation, String xmul, String ymul, String text); int clearbuffer(); int printlabel(String set, String copy); int formfeed(); int nobackfeed(); int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content); } public static void main(String[] args) { System.setProperty("jna.encoding", "GBK");// 支持中文
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = df.format(new Date()); String qrCode = "PD102011"; //TscLibDll.INSTANCE.about();
TscLibDll.INSTANCE.openport("TSC TTP-244 Plus"); //TscLibDll.INSTANCE.downloadpcx("C:\\UL.PCX", "UL.PCX");
TscLibDll.INSTANCE.setup("99.5","70","5","8","0","2","0"); TscLibDll.INSTANCE.clearbuffer(); //TscLibDll.INSTANCE.sendcommand("PUTPCX 550,10,\"UL.PCX\"");
String command = "QRCODE 300,250,Q,8,A,0,M2,S7,\"" + qrCode+"\""; //打印二維碼的參數和內容
TscLibDll.INSTANCE.sendcommand(command); //傳送指令
TscLibDll.INSTANCE.windowsfont(260, 100, 36, 0, 0, 0, "arial", "辦公耗材-標簽紙"); TscLibDll.INSTANCE.windowsfont(450, 150, 32, 0, 0, 0, "arial", time); TscLibDll.INSTANCE.printlabel("1", "1"); TscLibDll.INSTANCE.closeport(); } }
如果運行報錯UnsatisfiedLinkError: Unable to load library “TSCLIB”...可以嘗試把JDK換成32位版本。
在調用過程中有不明白的地方看TSPL2說明書,上面有詳細的指令用法以及參數說明!!!
參考: