打印测试,如果启动报错,则要考虑在项目属性中,生成–>目标平台,选择x86试试。TSCLIB.dll这个文件需要放置执行根目录下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace TSCLIB_DLL_IN_C_Sharp { public class PrintHelpter { /// <summary> /// 给打印机发送打印数据 /// </summary> /// <param name="json">要打印的数据</param> /// <returns>返回1=表示打印成功,0=失败</returns> public static int PrintMsg(string json, out OrderInfo orderInfoOut) { //开发文档,官网 https://www.chinatsc.cn/SC/support/support_download/TTP-244_Pro/Datasheet //驱动下载,客户端测试工具下载,开发文档/DLL下载 https://www.chinatsc.cn/SC/support/support_download/TTP-244_Pro //驱动下载,说明书,开发文档/DLL/参考代码下载 http://www.tsc-china.com/info/46592.html //打印二维码参考 https://www.chinatsc.cn/Image/Ckeditor/FAQ/DLL打印二维码.pdf //tspl2指令集中文版 https://wenku.baidu.com/view/9ab4af374531b90d6c85ec3a87c24028915f8585.html //TSC打印机使用教程终极版 https://www.cnblogs.com/codeyou/p/tsc.html //TSCLIB.DLL函数库使用说明 https://www.cnblogs.com/liujianshe1990-/p/12602914.html //TTP-244 Pro 系列打印机说明书 https://www.chinatsc.cn/SC/DownloadFile/readpdf/support/5712/TTP-244pro_user_manual_2017_2_1_SC.pdf?file_type=0 //Diagnostic Tool工具,说明书 http://www.tsc-china.com/u/0ee79b3e-d8a9-4f39-9073-78484040ddd3/file/636362389924448747.pdf /* * 打印机状态值 0 =待機中 1 =印字頭開啟 2 =卡紙 3 =卡紙並開啟印字頭 4 =缺紙 5 =缺紙並開啟印字頭 8 =缺碳帶 9 =缺碳帶並開啟印字頭 A =缺碳帶並卡紙 B =缺碳帶並卡紙及開啟印字頭 C =缺碳帶並同時缺紙 D =缺碳帶、缺紙並開啟印字頭 10 =暫停 20 =列印中 * */ //连接打印机 TSCLIB_DLL.openport("TSC TTP-244 Pro"); //读取打印机状态,0=待机中,2=卡纸,4 =缺纸 int state = TSCLIB_DLL.usbportqueryprinter(); Console.WriteLine("打印机状态=" + state); if (state != 0) { Console.WriteLine("打印机状态异常,不能打印"); return 0; } //设置内容显示范围 // 设置标签 宽度、高度 等信息. // 宽 79mm 高40mm(内容高度) // 速度为4 // density字体浓度为15,0~15,数字愈大列印结果愈黑 // 使用垂直間距感測器(gap sensor) // 两个标签之间的 间距为 3.5mm //setup(int width, int height, int speed, int density, int sensor, int sensor_distance, int sensor_offset) //宽度、高度、速度、浓度 //sensor为0:sensor_distance 垂直间距距离 sensor_offset垂直间距的偏移 //sensor为1:sensor_distance 定义黑标高度和额外送出长度 sensor_offset黑标偏移量 // int height = 110 + printRows * 7 + countRemark * 7;//内容总高度,单位毫米 //int height = 100 + addressList.Count * 6 + remarkList.Count * 7 + footList.Count * 5; int height =100; TSCLIB_DLL.setup("79", height.ToString(),"6", "15", "0", "0", "0"); //设置不回吃纸 TSCLIB_DLL.nobackfeed(); //<ESC>!Q 重新开机,测试无效果 //TSCLIB_DLL.sendcommand("<ESC>!Q"); TSCLIB_DLL.clearbuffer(); // 设置 打印的方向. TSCLIB_DLL.sendcommand("DIRECTION 1"); // 纸回拉指定长度 //TSCLIB_DLL.sendcommand("BACKFEED 60"); //二维码 //需要清除上一次的打印记忆 TSCLIB_DLL.sendcommand("CLS"); //語法:BAR x, y, width, height。画横线, //width線條寬度,單位 dot //200 DPI: 1 mm = 8 dots //TSCLIB_DLL.sendcommand("BAR 50,0,500,2"); //QRCODE x,y,ECC Level,cell width,mode,rotation,[justification,]model,]mask,]area] "content" //ECC level容错率,H=30%,Q=25%,M=15% //cell width 1~10,二维码大小,尺寸 //画分隔线290,不画270 //打印内容距离打印机出口值 int startY = 200; //“测试ABC123”为要生成的二维码字符串 string command = "QRCODE 180," + startY + ",H,6,A,0,M2,S7,\"测试ABC123\""; TSCLIB_DLL.sendcommand(command); //设置墨汁浓度 //TSCLIB_DLL.sendcommand("DENSITY 15"); // TSCLIB_DLL.windowsfont(100, 50, 30, 0, 0, 0, "ARIAL", "收货区域:北京(BJ)北京(010)"); //TSS24.BF2简体中文24*24 //TSCLIB_DLL.printerfont("100", "0", "FONT001", "0", "1", "1", "收货区域:北京(BJ)北京(010)"); // TSCLIB_DLL.setup("79", "40", "4", "16", "40", "1", "0"); int fontSize = 30; //离左侧边距 int x = 20; //画分隔线290,不画270 int y = startY + 170; //行高 int addNum = 40; TSCLIB_DLL.windowsfont(x, y, fontSize, 0, 0, 0, "ARIAL", "收货区域:成都武侯区" ); TSCLIB_DLL.printlabel("1", "1"); TSCLIB_DLL.closeport(); return 1; } /// <summary> /// 获取打印机状态,其他地方可以调用 /// </summary> public static void GetPrintState() { try { //读取打印机状态,0=待机中,2=卡纸,4 =缺纸 int state = TSCLIB_DLL.usbportqueryprinter(); Console.WriteLine("打印机状态" + state); } catch (Exception ex) { Console.WriteLine("获取打印机状态异常," + ex.ToString()); } Thread.Sleep(1000); } } }调用的TSCLIB.dll组件方法:
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Runtime.InteropServices; public class TSCLIB_DLL { [DllImport("TSCLIB.dll", EntryPoint = "about")] public static extern int about(); [DllImport("TSCLIB.dll", EntryPoint = "openport")] public static extern int openport(string printername); [DllImport("TSCLIB.dll", EntryPoint = "barcode")] public static extern int barcode(string x, string y, string type, string height, string readable, string rotation, string narrow, string wide, string code); [DllImport("TSCLIB.dll", EntryPoint = "clearbuffer")] public static extern int clearbuffer(); [DllImport("TSCLIB.dll", EntryPoint = "closeport")] public static extern int closeport(); [DllImport("TSCLIB.dll", EntryPoint = "downloadpcx")] public static extern int downloadpcx(string filename, string image_name); [DllImport("TSCLIB.dll", EntryPoint = "formfeed")] public static extern int formfeed(); [DllImport("TSCLIB.dll", EntryPoint = "nobackfeed")] public static extern int nobackfeed(); [DllImport("TSCLIB.dll", EntryPoint = "printerfont")] public static extern int printerfont(string x, string y, string fonttype, string rotation, string xmul, string ymul, string text); [DllImport("TSCLIB.dll", EntryPoint = "printlabel")] public static extern int printlabel(string set, string copy); //功能:繪製QRCODE二維條碼 //語法: //QRCODE X, Y, ECC Level, cell width, mode, rotation, [model, mask,]"Data string” //參數說明 //X QRCODE條碼左上角X座標 //Y QRCODE條碼左上角Y座標 //ECC level 錯誤糾正能力等級 //L 7% //M 15% //Q 25% //H 30% //cell width 1~10 //mode 自動生成編碼/手動生成編碼 //A Auto //M Manual //rotation 順時針旋轉角度 //0 不旋轉 //90 順時針旋轉90度 //180 順時針旋轉180度 //270 順時針旋轉270度 //model 條碼生成樣式 //1 (預設), 原始版本 //2 擴大版本 //mask 範圍:0~8,預設7 //Data string 條碼資料內容 //string printercommand = "QRCODE 176,8,Q,8,A,0,M2,S7,\"" + barCode + "\""; [DllImport("TSCLIB.dll", EntryPoint = "sendcommand")] public static extern int sendcommand(string printercommand); /// <summary> /// 宽度、高度、速度、浓度 // sensor为0:vertical 垂直间距距离 offset垂直间距的偏移 // sensor为1:vertical定义黑标高度和额外送出长度 offset黑标偏移量 /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <param name="speed">列印速度,1~6,6为最快速度</param> /// <param name="density">打印浓度,1-15,数字越大越黑</param> /// <param name="sensor"></param> /// <param name="vertical"></param> /// <param name="offset"></param> /// <returns></returns> [DllImport("TSCLIB.dll", EntryPoint = "setup")] public static extern int setup(string width, string height,string speed, string density,string sensor, string vertical,string offset); /// <summary> /// windowsfont(a,b,c,d,e,f,g,h) /// 说明:使用Windows TTF字型列印文字 /// 参数: /// a:整数型别,文字X方向起始点,以点(point)表示。 /// b:整数型别,文字Y方向起始点,以点(point)表示。 /// c:整数型别,字体高度,以点(point)表示。 /// d:整数型别,旋转角度,逆时钟方向旋转 /// 0 -> 0 degree /// 90-> 90 degree /// 180-> 180 degree /// 270-> 270 degree /// e:整数型别,字体外形 /// 0->标准(Normal) /// 1->斜体(Italic) /// 2->粗体(Bold) /// 3->粗斜体(Bold and Italic) /// f:整数型别,底线 /// 0->无底线 /// 1->加底线 /// g:字串型别,字体名称。如: Arial, Times new Roman,细名体,标楷体 /// h:字串型别,列印文字内容。 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="fontheight"></param> /// <param name="rotation"></param> /// <param name="fontstyle"></param> /// <param name="fontunderline"></param> /// <param name="szFaceName"></param> /// <param name="content"></param> /// <returns></returns> [DllImport("TSCLIB.dll", EntryPoint = "windowsfont")] public static extern int windowsfont(int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, string szFaceName, string content); /// <summary> /// 获取打印机状态, /// </summary> /// <returns></returns> /// 0=待机中 /// 1=印字头开启 /// 2=卡纸 /// 3 /// 4 =缺纸 /// 10= 暂停中 /// 20 =列印中 [DllImport("TSCLIB.dll", EntryPoint = "usbportqueryprinter")] public static extern int usbportqueryprinter(); }我的码云项目参考 C#代码调用TSC打印机参考项目源码码云地址: https://gitee.com/wanghaoli/windows-service-tsc-print.git