C#使用USB接口进行Barcode Printer(读取非C#的TSCLib.dll方法)


前言

就在上一篇博客有写如何使用C#系统方法进行串口通信,但是否能进行USB控制打印机尚未测试,但原本就有一种方式去控制USB连接的打印机,而且以这种方式并不是输入指令,可能更容易被理解。

其中还有一个很重要的知识点,就是如何在C#中引用非C#的DLL文件,这才是本文的精华。

一、引用非C#的DLL文件

直接在References中引用DLL的方法对于非C#的DLL文件是不可行的,反而会出现以下问题。

 1、导包

using System.Runtime.InteropServices;

2、如何使用

需要将DLL放在Bin路径下,在bin路径下如果再包一个文件夹也是可以的(如Resources/……),但指明的位置要准确,DLL文件名不区分大小写,此DLL可在网上搜寻下载。

    public class TSCLibDLL
    {
        [DllImport("Resources/TSCLIB.dll", EntryPoint = "about")]
        public static extern int about();

        [DllImport("Resources/TSCLIB.dll", EntryPoint = "openport")]
        public static extern int openport(string printername);

        [DllImport("Resources/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("Resources/TSCLIB.dll", EntryPoint = "clearbuffer")]
        public static extern int clearbuffer();

        [DllImport("Resources/TSCLIB.dll", EntryPoint = "closeport")]
        public static extern int closeport();

        [DllImport("Resources/TSCLIB.dll", EntryPoint = "downloadpcx")]
        public static extern int downloadpcx(string filename, string image_name);

        [DllImport("Resources/TSCLIB.dll", EntryPoint = "formfeed")]
        public static extern int formfeed();

        [DllImport("Resources/TSCLIB.dll", EntryPoint = "nobackfeed")]
        public static extern int nobackfeed();

        [DllImport("Resources/TSCLIB.dll", EntryPoint = "printerfont")]
        public static extern int printerfont(string x, string y, string fonttype,
        string rotation, string xmul, string ymul,string text);

        [DllImport("Resources/TSCLIB.dll", EntryPoint = "printlabel")]
        public static extern int printlabel(string set, string copy);

        [DllImport("Resources/TSCLIB.dll", EntryPoint = "sendcommand")]
        public static extern int sendcommand(string printercommand);

        [DllImport("Resources/TSCLIB.dll", EntryPoint = "setup")]
        public static extern int setup(string width, string height,string speed, 
                     string density,string sensor, string vertical,string offset);

        [DllImport("Resources/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);
  
    }

二、使用USB接口控制打印机

前面一部分将DLL中的方法声明,后面正常调用即可

private void usbPrint()
{
    var res = TSCLibDLL.openport("USB");
    if (res == 0) return;//接口未打开
    TSCLibDLL.setup("30", "14.4", "2", "10", "0", "3.2", "0");//设置长宽等要素,可自行实验
    TSCLibDLL.clearbuffer();//Clear image buffer
    TSCLibDLL.windowsfont(40, 5, 25, 0, 2, 0, "Arial", "Printer Header");//first row  
    TSCLibDLL.barcode("30", "35", "EAN13", "60", "1", "0", "3", "6", "Barcode12345"); //Drawing barcode
    TSCLibDLL.windowsfont(30, 130, 20, 0, 2, 0, "Arial", "Printer Footer"); //third row
    TSCLibDLL.printlabel("1", "1");
    TSCLibDLL.closeport(); //Close specified printer driver
}