Skip to content

Instantly share code, notes, and snippets.

@eugeniop
Created November 22, 2013 05:29
Show Gist options
  • Save eugeniop/7595287 to your computer and use it in GitHub Desktop.
Save eugeniop/7595287 to your computer and use it in GitHub Desktop.
Zebra Printer
using System;
using System.Collections.Generic;
using System.Text;
using mOrders.Terminal.Infrastructure;
using mOrders.Terminal.Store;
namespace mOrders.Terminal.Services
{
public class CPCLLabelFormatter
{
private string labelMask;
private List<Action<PatientOrder,Sample, StringBuilder>> replaceFunctions;
public CPCLLabelFormatter( string labelMask )
{
this.labelMask = labelMask;
replaceFunctions = new List<Action<PatientOrder, Sample, StringBuilder>>();
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@location", o.Location));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@name", o.Name));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@gender", o.Gender));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@urgent", o.Urgent ? "Urgent" : ""));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@age", o.Age.ToString()));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@instructions", o.Instructions));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@comments", o.Comments));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@sampleCode", s.Code));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@sampleBarCode", s.Barcode));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@sampleDescription", s.Description));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@code", o.Code));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@date", SystemTime.Now.ToShortDateString()));
replaceFunctions.Add((o, s, sb) => sb = sb.Replace("@time", SystemTime.Now.ToShortTimeString()));
}
public string[] GetLabels(PatientOrder o)
{
List<string> labels = new List<string>();
foreach (var s in o.Samples)
{
StringBuilder sb = new StringBuilder(this.labelMask);
replaceFunctions.ForEach(ftr => ftr(o, s, sb));
labels.Add(sb.ToString());
}
return labels.ToArray();
}
}
}
namespace mOrders.Terminal.Infrastructure
{
public interface ILabelPrinter
{
void PrintLabels(string[] labels);
}
}
[TestMethod]
public void PrintsLabel()
{
string printerMask = "! 0 200 200 200 1\r\n" +
"LABEL\r\n" +
"CONTRAST 0\r\n" +
"TONE 0\r\n" +
"SPEED 5\r\n" +
"PAGE-WIDTH 580\r\n" +
"BAR-SENSE\r\n" +
";// PAGE 0000000005800200\r\n" +
"T 0 5 10 8 @date\r\n" +
"T 0 5 172 8 @time\r\n" +
"BT 0 0 0\r\n" +
"B 39 2 2 50 12 138 @sampleBarCode\r\n" +
"BT OFF\r\n" +
"T 0 5 49 51 @name\r\n" +
"T 0 5 12 51 @gender\r\n" +
"T 0 5 12 92 @location\r\n" +
"FORM\r\n" +
"PRINT\r\n";
Console.WriteLine(printerMask);
PatientOrder po = new PatientOrder() { Age = 1, Code = "O-Code", Gender = "M", Location = "Location", Name = "Name" };
po.Samples = new List<Sample>();
(po.Samples as List<Sample>).Add(new Sample() { Barcode = "12345", Code = "427", Description = "Sample 1" });
(po.Samples as List<Sample>).Add(new Sample() { Barcode = "87654", Code = "425", Description = "Sample 2" });
CPCLLabelFormatter f = new CPCLLabelFormatter(printerMask);
var labels = f.GetLabels(po);
var z = new ZebraLabelPrinter("192.168.1.18");
z.PrintLabels(labels);
}
using System.Text;
using mOrders.Terminal.Infrastructure;
using ZSDK_API.Comm;
namespace mOrders.Terminal.Services
{
public class ZebraLabelPrinter : ILabelPrinter
{
private string printerAddress;
private ZebraPrinterConnection connection;
public ZebraLabelPrinter(string printerAddress)
{
this.printerAddress = printerAddress;
}
public void PrintLabels(string[] labels)
{
connection = new TcpPrinterConnection(this.printerAddress, TcpPrinterConnection.DEFAULT_CPCL_TCP_PORT);
connection.Open();
foreach (var s in labels)
{
connection.Write(Encoding.Default.GetBytes(s));
}
connection.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment