创作背景:在工控行业中,打印二维码是一个很常用的功能,而斑马设备又是其中的佼佼者。有幸在一个项目中使用过斑马打印机。
程序的源码在文章末尾有连接,这里直接展示关键代码。代码不是终版,且均由我一个人完成,所以不存在泄密和侵权的情况,各位伙伴可以放心使用。
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调用斑马打印机