C# 控制TSC打印机功能

核心代码

    public class PrinterHelper
    {
        /// 
        /// 更新线程
        /// 
        Thread m_Thread;

        /// 
        /// 线程阻塞
        /// 
        AutoResetEvent m_ResetEvent = new AutoResetEvent(true);

        /// 
        /// 是否工作
        /// 
        bool m_ThreadIsWork = false;

        /// 
        /// 是否阻塞
        /// 
        bool m_ThreadIsPause = true;

        /// 
        /// 服务器状态
        /// 
        public delegate void PrinterStatusEventDelegate(int Status, string Message);
        /// 
        /// 服务器状态
        /// 
        public event PrinterStatusEventDelegate PrinterStatus;

        /// 
        /// 打印机名称
        /// 
        private static string m_PrinterName = string.Empty;

        public PrinterHelper(string PrinterName)
        {
            m_PrinterName = PrinterName;

            this.m_Thread = new Thread(new ThreadStart(this.WorkThreedFun));
            this.m_Thread.IsBackground = true;
            this.m_Thread.Start();

            this.m_ThreadIsWork = true;
            this.m_ThreadIsPause = false;
            this.m_ResetEvent.Set();
        }


        [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);

        [DllImport("TSCLIB.dll", EntryPoint = "sendBinaryData")]
        public static extern int sendBinaryData(byte[] content, int length);

        //功能:繪製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);

        /// 
        ///   宽度、高度、速度、浓度
        //    sensor为0:vertical 垂直间距距离 offset垂直间距的偏移
        //    sensor为1:vertical定义黑标高度和额外送出长度 offset黑标偏移量
        /// 
        /// 
        /// 
        /// 列印速度,1~6,6为最快速度
        /// 打印浓度,1-15,数字越大越黑
        /// 
        /// 
        /// 
        /// 
        [DllImport("TSCLIB.dll", EntryPoint = "setup")]
        public static extern int setup(string width, string height, string speed, string density, string sensor, string vertical, string offset);

        /// 
        /// 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:字串型别,列印文字内容。 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        [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);

        /// 
        /// 获取打印机状态,
        /// 
        /// 
        /// 0=待机中
        /// 1=印字头开启
        /// 2=卡纸
        /// 3
        /// 4 =缺纸
        /// 10= 暂停中
        /// 20 =列印中
        [DllImport("TSCLIB.dll", EntryPoint = "usbportqueryprinter")]
        public static extern int usbportqueryprinter();

        /// 
        /// 执行线程函数
        /// 
        private void WorkThreedFun()
        {
            while (true)
            {
                if (this.m_ThreadIsPause || !this.m_ThreadIsWork)
                {
                    this.m_ResetEvent.WaitOne();
                }
                System.Threading.Thread.Sleep(10);
                this.DoWork();
            }
        }

        private void DoWork()
        {
            //int i = openport(m_PrinterName);
            int status = usbportqueryprinter();
            string Hex = status.ToString("X6");
            int rStatus = 14;
            string strStatus = string.Empty;
            switch (Hex)
            {
                case "000000":
                    rStatus = 0;
                    strStatus = "准备就绪";
                    break;
                case "000001":
                    rStatus = 1;
                    strStatus = "打印头开启";
                    break;
                case "000002":
                    rStatus = 2;
                    strStatus = "纸张卡纸";
                    break;
                case "000003":
                    rStatus = 3;
                    strStatus = "打印头开启并且纸张卡纸";
                    break;
                case "000004":
                    rStatus = 4;
                    strStatus = "纸张缺纸";
                    break;
                case "000005":
                    rStatus = 5;
                    strStatus = "打印头开启并且纸张缺纸";
                    break;
                case "000008":
                    rStatus = 6;
                    strStatus = "无碳带";
                    break;
                case "000009":
                    rStatus = 7;
                    strStatus = "打印头开启并且无碳带";
                    break;
                case "00000A":
                    rStatus = 8;
                    strStatus = "纸张卡纸并且无碳带";
                    break;
                case "00000B":
                    rStatus = 9;
                    strStatus = "打印头开启、纸张卡纸并且无碳带";
                    break;
                case "00000C":
                    rStatus = 10;
                    strStatus = "纸张缺纸并且无碳带";
                    break;
                case "000010":
                    rStatus = 11;
                    strStatus = "暂停";
                    break;
                case "000020":
                    rStatus = 12;
                    strStatus = "打印中";
                    break;
                case "000080":
                    rStatus = 13;
                    strStatus = "其它错误";
                    break;
                default:
                    rStatus = 14;
                    strStatus = "未知";
                    break;
            }
            this.PrinterStatus?.Invoke(rStatus, strStatus);
            //System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + ":" + status);
        }

        public static int SetCommand(string HexString)
        {
            int dvopenPort = openport(m_PrinterName);

            byte[] bytes = HexStrToByte(HexString);
            var r = sendBinaryData(bytes, bytes.Length);

            closeport();

            return r;
        }

        private static byte[] HexStrToByte(string HexString)
        {
            HexString = HexString.Replace(" ", "");
            if (HexString.Length % 2 != 0)
            {
                HexString += " ";
            }

            byte[] returnBytes = new byte[HexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(HexString.Substring(i * 2, 2).Trim(), 16);
            }

            return returnBytes;
        }

        public static void TestPrint()
        {
            int dvopenPort = openport(m_PrinterName);

            sendcommand("DIRECTION 1");
            //设置标签大小  mm
            sendcommand("SIZE 76mm" + ",130mm");
            //设置打印速度  英寸每秒 1-10
            sendcommand("SPEED 10");
            //清除打印机缓存
            clearbuffer();
            //此条指令用来定义不同的国码编码。

            string BarCode = "123456";
            sendcommand("CODEPAGE UTF-8");
            sendcommand("OFFSET 5mm");
            sendcommand(string.Format("TEXT 140,50,\"SourceHa.TTF\",0,10,10,\"当前操作:打印测试页\""));
            sendcommand("TEXT 80,100,\"SourceHa.TTF\",0,14,14,\"----------------------------------\"");
            sendcommand("TEXT 120,160,\"SourceHa.TTF\",0,10,10,\"*********************\"");
            sendcommand("TEXT 80,200,\"SourceHa.TTF\",0,14,14,\"----------------------------------\"");
            sendcommand("TEXT 220,250,\"SourceHa.TTF\",0,13,13,\"菩萨蛮 温庭筠\"");
            sendcommand("TEXT 200,300,\"SourceHa.TTF\",0,13,13,\"玉楼明月长相忆,\"");
            sendcommand("TEXT 200,350,\"SourceHa.TTF\",0,13,13,\"柳丝袅娜春无力。\"");
            sendcommand("TEXT 200,400,\"SourceHa.TTF\",0,13,13,\"门外草萋萋,\"");
            sendcommand("TEXT 200,450,\"SourceHa.TTF\",0,13,13,\"送君闻马嘶。\"");
            sendcommand("TEXT 200,500,\"SourceHa.TTF\",0,13,13,\"画罗金翡翠,\"");
            sendcommand("TEXT 200,550,\"SourceHa.TTF\",0,13,13,\"香烛销成泪。\"");
            sendcommand("TEXT 200,600,\"SourceHa.TTF\",0,13,13,\"花落子规啼,\"");
            sendcommand("TEXT 200,650,\"SourceHa.TTF\",0,13,13,\"绿窗残梦迷。\"");
            sendcommand("TEXT 120,700,\"SourceHa.TTF\",0,13,13,\"0001-0000-0010-0100\"");
            sendcommand("TEXT 80,750,\"SourceHa.TTF\",0,14,14,\"----------------------------------\"");
            barcode("180", "800", "128", "128", "1", "0", "2", "2", BarCode);
            sendcommand("TEXT 80,900,\"SourceHa.TTF\",0,14,14,\"----------------------------------\"");
            windowsfont(110, 950, 32, 0, 0, 0, "SourceHa.TTF", "测试打印时间" + DateTime.Now.ToString());

            printlabel("1", "1");
            closeport();
        }

        public static void AddJob(int index)
        {
            int dvopenPort = openport(m_PrinterName);

            sendcommand("DIRECTION 1");
            //设置标签大小  mm
            sendcommand("SIZE 76mm" + ",130mm");
            //设置打印速度  英寸每秒 1-10
            sendcommand("SPEED 10");
            //清除打印机缓存
            clearbuffer();
            //此条指令用来定义不同的国码编码。

            string BarCode = "123456";
            sendcommand("CODEPAGE UTF-8");
            sendcommand("OFFSET 5mm");
            sendcommand(string.Format("TEXT 140,50,\"SourceHa.TTF\",0,10,10,\"当前操作:打印测试页{0}\"", index));
            sendcommand("TEXT 80,100,\"SourceHa.TTF\",0,14,14,\"----------------------------------\"");
            sendcommand("TEXT 120,160,\"SourceHa.TTF\",0,10,10,\"*********************\"");
            sendcommand("TEXT 80,200,\"SourceHa.TTF\",0,14,14,\"----------------------------------\"");
            sendcommand("TEXT 220,250,\"SourceHa.TTF\",0,13,13,\"菩萨蛮 温庭筠\"");
            sendcommand("TEXT 200,300,\"SourceHa.TTF\",0,13,13,\"玉楼明月长相忆,\"");
            sendcommand("TEXT 200,350,\"SourceHa.TTF\",0,13,13,\"柳丝袅娜春无力。\"");
            sendcommand("TEXT 200,400,\"SourceHa.TTF\",0,13,13,\"门外草萋萋,\"");
            sendcommand("TEXT 200,450,\"SourceHa.TTF\",0,13,13,\"送君闻马嘶。\"");
            sendcommand("TEXT 200,500,\"SourceHa.TTF\",0,13,13,\"画罗金翡翠,\"");
            sendcommand("TEXT 200,550,\"SourceHa.TTF\",0,13,13,\"香烛销成泪。\"");
            sendcommand("TEXT 200,600,\"SourceHa.TTF\",0,13,13,\"花落子规啼,\"");
            sendcommand("TEXT 200,650,\"SourceHa.TTF\",0,13,13,\"绿窗残梦迷。\"");
            sendcommand("TEXT 120,700,\"SourceHa.TTF\",0,13,13,\"0001-0000-0010-0100\"");
            sendcommand("TEXT 80,750,\"SourceHa.TTF\",0,14,14,\"----------------------------------\"");
            barcode("180", "800", "128", "128", "1", "0", "2", "2", BarCode);
            sendcommand("TEXT 80,900,\"SourceHa.TTF\",0,14,14,\"----------------------------------\"");
            windowsfont(110, 950, 32, 0, 0, 0, "SourceHa.TTF", "测试打印时间" + DateTime.Now.ToString());

            printlabel("1", "1");
            closeport();
        }
    }

完成打印机指令控制打印机函数:

        [DllImport("TSCLIB.dll", EntryPoint = "sendBinaryData")]
        public static extern int sendBinaryData(byte[] content, int length);

发送打印机指令

i.e !P和!O,分别暂停打印、取消暂停

注意为跳脱符号,使用sendBinaryData发送指令接受的为byte[]数据类型

代码参考:

string Hex = "1B 21 50"; //!P暂停
string Hex = "1B 21 4F"; //!O解除暂停
byte[] sendBytes = HexStrToByte(Hex);
var r = sendBinaryData(sendBytes, sendBytes.Length);

许多朋友使用的是sendcommand函数,是只能发送命令,而不能发送指令内容!!!

sendcommand 支持TSC文档:系统指令、标签内容打印命令、设备配置指令等

sendBinaryData支持TSC文档:状态获取及立即指令、信息传送、驱动指令、文件管理指令、基础指令及函数等

TSC包含命令与指令,文档有很明确的说明。

C# 控制TSC打印机功能_第1张图片

C# 控制TSC打印机功能_第2张图片

TSCLIB 下载地址  

你可能感兴趣的:(打印机,c#,开发语言,TSC,打印机)