在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 操作TSC条码打印机

操作TSC条码打印机

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.07M
  • 下载次数:74
  • 浏览次数:1264
  • 发布时间:2018-01-26
  • 实例类别:C#语言基础
  • 发 布 人:339238137
  • 文件格式:.zip
  • 所需积分:5
 相关标签: 操作 c 打印机 条码 打印

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using System.Runtime.InteropServices;
using System.Drawing.Printing;
using System.Management;




namespace PrintTSCG310
{
    public partial class Form1 : Form
    {


        private Label lblPrinteres, lblStart, lblEnd, lblMsg, lblFenshu;
        private NumericUpDown nudStart, nudEnd, nudFenshu;

        private Button btnPrint;

        public Form1()
        {
            InitializeComponent();
            Initial();
            this.Load  = new EventHandler(Form1_Load);
            this.FormClosed  = new FormClosedEventHandler(Form1_FormClosed);
        }
        void Initial()
        {

            this.Text = "批量打印条形码";
            lblPrinteres = label1;
            lblPrinteres.Text = "打印机:";

            btnPrint =button1;
            btnPrint.Text = "打印";


            nudStart=numericUpDown1;
            nudStart.Value=1;
            nudStart.ValueChanged  = new EventHandler(nudStart_ValueChanged);
            nudEnd=numericUpDown2;
            nudEnd.Value=19;
            nudEnd.ValueChanged  = new EventHandler(nudEnd_ValueChanged);

            nudFenshu = numericUpDown3;
            nudFenshu.Value = 1;

            lblStart = label2;
            lblStart.Text = "起始编号";

            lblEnd = label3;
            lblEnd.Text = "结束编号";


            lblMsg = label4;
            lblMsg.Text = "从CCD-" nudStart.Value.ToString("00") "到CCD-" nudEnd.Value.ToString("00");

            lblFenshu = label5;
            lblFenshu.Text = "份数";

        }

        void nudEnd_ValueChanged(object sender, EventArgs e)
        {
           
            if (nudStart.Value > nudEnd.Value)
            {
                MessageBox.Show("起始值不能大于结束值");
                nudEnd.Value = nudStart.Value;
            }

            lblMsg.Text = "从CCD-"   nudStart.Value.ToString("00")   "到CCD-"   nudEnd.Value.ToString("00");

        }

        void nudStart_ValueChanged(object sender, EventArgs e)
        {
            if (nudStart.Value > nudEnd.Value)
            {
                MessageBox.Show("起始值不能大于结束值");
                nudStart.Value = nudEnd.Value;
            }
            lblMsg.Text = "从CCD-"   nudStart.Value.ToString("00")   "到CCD-"   nudEnd.Value.ToString("00");

        }

        void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            // 关闭 打印机 端口
            TSCLIB_DLL.closeport();
        }

        void Form1_Load(object sender, EventArgs e)
        {
            //遍历本台电脑所加载的打印机
            foreach (String printer in PrinterSettings.InstalledPrinters)
            {
                comboBox1.Items.Add(printer.ToString());
            }
            comboBox1.SelectedIndex = 0;
            
        }

        #region 打开TSC打印机
        public static void TSCOpen(string p_port)
        {
            // 打开 打印机 端口.
            TSCLIB_DLL.openport(p_port);
            // 设置标签 宽度、高度 等信息.
            // 宽 64mm  高 10mm
            // 速度为4
            // 字体浓度为8
            // 使用垂直間距感測器(gap sensor)
            // 两个标签之间的  间距为 3.5mm
            TSCLIB_DLL.setup("64", "10", "4", "15", "0", "1.5", "0");
            // 清除缓冲信息
            TSCLIB_DLL.clearbuffer();

            // 发送 TSPL 指令.
            // 设置 打印的方向.
            TSCLIB_DLL.sendcommand("DIRECTION 1");
        }


        #endregion

        #region 调用TSC打印机打印条码

       
     /// <summary>
     /// 调用TSC打印机打印条码
     /// </summary>
     /// <param name="str">打印的内容</param>
        /// <param name="a">设定打印卷标式数</param>
        /// <param name="b">设定打印卷标份数</param>
        public static void TSC(string str,string a,string b)
        {
            #region  打印条码的方法一
           
//            printerfont(a,b,c,d,e,f,g)
//说明: 使用条形码机内建文字打印
//参数: 
//a: 字符串型别,文字X方向起始点,以点(point)表示。
//(200 DPI,1点=1/8 mm, 300 DPI,1点=1/12 mm)
//b: 字符串型别,文字Y方向起始点,以点(point)表示。
//(200 DPI,1点=1/8 mm, 300 DPI,1点=1/12 mm)
//    c: 字符串型别,内建字型名称,共12种。
//    1: 8*/12 dots
//    2: 12*20 dots
//    3: 16*24 dots
//    4: 24*32 dots
//    5: 32*48 dots
//    TST24.BF2: 繁体中文24*24 
//    TST16.BF2: 繁体中文16*16
//    TTT24.BF2: 繁体中文24*24 (电信码)
//    TSS24.BF2: 简体中文24*24
//    TSS16.BF2: 简体中文16*16
//    K: 韩文 24*24
//    L: 韩文 16*16
//d: 字符串型别,设定文字旋转角度
//        0: 旋转0度
//        90: 旋转90度
//        180: 旋转180度
//        270: 旋转270度
//e: 字符串型别,设定文字X方向放大倍率,1~8
//f: 字符串型别,设定文字X方向放大倍率,1~8
//g: 字符串型别,打印文字内容

            #endregion
            #region 打印条码的方法二

            /*
             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: 字符串型别,打印文字内容。
             */

            TSCLIB_DLL.windowsfont(250, 70, 40, 0, 2, 0, "宋体", str);
            #endregion
           
            
           
    //       printlabel(a,b)
    //        说明: 打印卷标内容
    //参数: 
    //a: 字符串型别,设定打印卷标式数(set)
    //b: 字符串型别,设定打印卷标份数(copy)
            
             TSCLIB_DLL.printlabel(a, b);

          
        }
#endregion

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Items.Count != 0)
            {
                TSCOpen(comboBox1.SelectedItem.ToString());


               // TSC("CCD-13 CCD-13", "1", nudFenshu.Value.ToString());




                for (int i = Convert.ToInt32(nudStart.Value); i <= Convert.ToInt32(nudEnd.Value); i  )
                {
                    TSC("CCD-"   i.ToString("00")   " CCD-"   i.ToString("00"), "1", nudFenshu.Value.ToString());
                    // 清除缓冲信息
                    TSCLIB_DLL.clearbuffer(); 
                }
                // 关闭 打印机 端口
                TSCLIB_DLL.closeport();
            }
                

      }
        /// <summary>
        /// 调用tsclib.dll
        /// </summary>
        public class TSCLIB_DLL
        {

            //            about()
            //说明: 显示DLL 版本号码
            //参数: 无
            [DllImport("TSCLIB.dll", EntryPoint = "about")]
            public static extern int about();

           //openport(a)
           // 说明: 指定计算机端的输出端口
           // 参数: 
           //     a: 单机打印时,请指定打印机驱动程序名称,例如: TSC CLEVER TTP-243
           //     若连接打印机服务器,请指定服务器路径及共享打印机名称,例如: \\SERVER\TTP243
            [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);
            //clearbuffer()
            //说明: 清除
            //参数: 无
            [DllImport("TSCLIB.dll", EntryPoint = "clearbuffer")]
            public static extern int clearbuffer();

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

            //downloadpcx(a,b)
            //说明:下载单色PCX 格式图文件至打印机
            //参数: 
            //    a: 字符串型别,文件名(可包含路径)
            //    b: 字符串型别,下载至打印机内存内之文件名(请使用大写档名)

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


//            formfeed()
//说明: 跳页,该函式需在setup后使用
//参数: 无
            [DllImport("TSCLIB.dll", EntryPoint = "formfeed")]
            public static extern int formfeed();

//           nobackfeed()
//说明: 设定纸张不回吐
//参数: 无

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

            //sendcommand(command)
            //说明: 送内建指令到条形码打印机
            //参数: 详细指令请参考TSPL
            [DllImport("TSCLIB.dll", EntryPoint = "sendcommand")]
            public static extern int sendcommand(string printercommand);

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

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

        }

        #region 调用TSC1打印机打印一维码
        /// <summary>
        /// 调用TSC打印机打印条码
        /// </summary>
        /// <param name="title">打印的标题</param>
        /// <param name="barCode">打印的条码编号</param>
        public static void TSC1(string title, string barCode)
        {
           /*
            barcode(a,b,c,d,e,f,g,h,I)
说明: 使用条形码机内建条形码打印
参数: 
a: 字符串型别,条形码X方向起始点,以点(point)表示。
(200 DPI,1点=1/8 mm, 300 DPI,1点=1/12 mm)
b: 字符串型别,条形码Y方向起始点,以点(point)表示。
(200 DPI,1点=1/8 mm, 300 DPI,1点=1/12 mm)
c: 字符串型别,
128				Code 128, switching code subset A, B, C 
automatically 
128M			Code 128, switching code subset A, B, C 
manually.
EAN128			Code 128, switching code subset A, B, C 
automatically
			25				Interleaved 2 of 5
			25C				Interleaved 2 of 5 with check digits
			39				Code 39
			39C				Code 39 with check digits
			93				Code 93 
			EAN13			EAN 13
			EAN13 2		EAN 13 with 2 digits add-on
			EAN13 5		EAN 13 with 5 digits add-on
			EAN8			EAN 8 
	EAN8 2			EAN 8 with 2 digits add-on
			EAN8 5			EAN 8 with 5 digits add-on
			CODA			Codabar
			POST			Postnet
			UPCA			UPC-A
			UPCA 2			UPC-A with 2 digits add-on
			UPCA 5			UPC-A with 5 digits add-on
			UPCE			UPC-E 
			UPCE 2			UPC-E with 2 digits add-on
			UPCE 5			UPC-E with 5 digits add-on

	d: 字符串型别,设定条形码高度,高度以点来表示
	e: 字符串型别,设定是否打印条形码码文
		0: 不打印码文
		1: 打印码文
	f: 字符串型别,设定条形码旋转角度
		0: 旋转0度
		90: 旋转90度
		180: 旋转180度
		270: 旋转270度
	g: 字符串型别,设定条形码窄bar 比例因子,请参考TSPL使用手册
	h: 字符串型别,设定条形码窄bar 比例因子,请参考TSPL使用手册
	I: 字符串型别,条形码内容
*/
            TSCLIB_DLL.barcode("176", "66", "39", "130", "1", "0", "7", "12", barCode);


            // 打印.
            TSCLIB_DLL.printlabel("1", "1");


        }
        #endregion


        #region 调用TSC2打印机打印二维码
        /// <summary>
        /// 调用TSC打印机打印二维码
        /// </summary>
        /// <param name="title">打印的标题</param>
        /// <param name="barCode">打印的条码编号</param>
        public static void TSC2(string title, string barCode)
        {
            
            #region 请求sendcommand指令,打印二维码
            //功能:繪製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   條碼資料內容
            #endregion
            string command = "QRCODE 176,8,Q,8,A,0,M2,S7,\""   barCode   "\"";
            TSCLIB_DLL.sendcommand(command);
            // 打印文本信息.
            // 在 (176, 8) 的坐标上
            // 字体高度为34
            // 旋转的角度为 0 度
            // 2 表示 粗体.
            // 文字没有下划线.
            // 字体为 黑体.
            // 打印的内容为:title
            //---------------------------------根据需要再添加
            //TSCLIB_DLL.windowsfont(176, 8, 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");
            
        }
        #endregion
    }
}

实例下载地址

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警