C#Winform调用tcp/ip调用斑马打标机示例

创作背景:在工控行业中,打印二维码是一个很常用的功能,而斑马设备又是其中的佼佼者。有幸在一个项目中使用过斑马打印机。

程序的源码在文章末尾有连接,这里直接展示关键代码。代码不是终版,且均由我一个人完成,所以不存在泄密和侵权的情况,各位伙伴可以放心使用。

1、打标帮助的类。因为项目是给墨西哥的一个公司做的,所以大部分注释都是采用的英文。

cs 复制代码
    public class PrintHandle
    {
        public static AsyncTcpSession Client = new AsyncTcpSession();
        static string IP = "192.168.12.175";
        static int Port = 9100;
        public static string StationID = "OP";

        /// <summary>
        /// Initialize Printing
        /// </summary>
        public static void InitializePrint()
        {
            Client.Closed += Client_Closed;
            Client.DataReceived += Client_DataReceived;
            Client.Connected += Client_Connected;
            ConnectingPrinter();
        }

        /// <summary>
        /// Link Printer
        /// </summary>
        public static void ConnectingPrinter()
        {
            if (!Client.IsConnected)
            {
                Connect(Client, IP, Convert.ToInt32(Port));
            }
        }

        public static void ClosePrinter()
        {
            if (Client.IsConnected)
            {
                Client.Close();
            }
        }

        /// <summary>
        /// Close
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Client_Closed(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// receive data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Client_DataReceived(object sender, DataEventArgs e)
        {
            string msg = Encoding.Default.GetString(e.Data).Replace("\0", "");
            for (int i = 0; i < e.Data.Length; i++)
            {
                e.Data[i] = 0;
            }
        }

        /// <summary>
        /// Connect
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Client_Connected(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Print large bottle code
        /// </summary>
        public static void PrintOriginalBottleCodeAction(PrintAttribute pa)
        {
            if (Client.IsConnected)
            {
                byte[] arr;
                arr = Encoding.Default.GetBytes(string.Format("{0}", GetPrintAssemblyText(pa)));
                Client.Send(arr, 0, arr.Length);
            }
        }

        /// <summary>
        /// Print small bottle code
        /// </summary>
        public static void PrintLittleBottleCodeAction(PrintAttribute pa)
        {
            byte[] arr;
            arr = Encoding.Default.GetBytes(string.Format("{0}", GetPrintAssemblyText(pa)));
            Client.Send(arr, 0, arr.Length);

        }

        public static void Connect(AsyncTcpSession c, string ip, int port)
        {
            int i = 0;
            c.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
            while (!c.IsConnected)
            {
                Thread.Sleep(20);
                i++;
                if (i == 50)
                    break;
            }
        }

        /// <summary>
        /// Assemble Print Template
        /// </summary>
        /// <returns></returns>
        public static string GetPrintAssemblyText(PrintAttribute pa)
        {
            try
            {
                string text = System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "\\Data\\" + StationID + "\\" + "Assembly" + ".prn");
                text = text.Replace("{BigOrSmall}", pa.BottleTye);
                text = text.Replace("{SolutionTypeAndNumber}", pa.BottleNo);
                text = text.Replace("{OpeningDateInformation}", pa.OpeningDate);
                text = text.Replace("{ExpiryDateInformation}", pa.ExpiryDate);
                text = text.Replace("FDInformationCodeErWeiMa", pa.CodeInformation);
                text = text.Replace("{InformationCode}", pa.CodeInformation);
                text = text.Replace("{BatchNoInformation}", pa.BatchNo);
                return text;
            }
            catch (Exception ex)
            {
                Log.WriteLog("GetPrintAssemblyText", "PrintHandle", ex.ToString());
                return string.Empty;
            }

            return string.Empty;
        }
    }

2、调用示例。

cs 复制代码
        /// <summary>
        /// Scan the original bottle code of the primer, verify whether it is available, etc
        /// </summary>
        public static PrintAttribute PROriginalCodeAction(string pROriginalCode)
        {
            PrintAttribute pa = new PrintAttribute();
            pa.DataIniti();
            string originalCodeBC = "2";
            if (!string.IsNullOrEmpty(pROriginalCode))
            {
                pa = DatabaseOperation.ScanPROriginalBottleCode(pROriginalCode);
                if (pa.IfUse)
                {
                    PrintHandle.PrintOriginalBottleCodeAction(pa);
                    originalCodeBC = "1";
                }
            }
            else
            {
                originalCodeBC = "2";
            }
            PLCLibrary.PLCWrite.PRReplyOriginalCodeBC(originalCodeBC);

            return pa;
        }

源码链接:C#winform调用斑马打印机

相关推荐
2401_857439691 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
SoraLuna1 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
xlsw_2 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
Dream_Snowar3 小时前
速通Python 第三节
开发语言·python
高山我梦口香糖3 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
信号处理学渣4 小时前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客4 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
jasmine s4 小时前
Pandas
开发语言·python
biomooc4 小时前
R 语言 | 绘图的文字格式(绘制上标、下标、斜体、文字标注等)
开发语言·r语言
骇客野人4 小时前
【JAVA】JAVA接口公共返回体ResponseData封装
java·开发语言