条码打印机智能脱机打印
使用BCC中国定制条码打印机
BCC打印机 PM42 PM43打印机
打印机指令
打印机指令罗列一下:
- Honeywell 打印机 ,FP,DP,DPL指令;
- ZEBRA打印机 ZPL||,EPL,CPCL指令;
- ESPON打印机 ESC-POS指令 ;
- TSC打印机 TSPL指令 ;
各厂家打印机指令各有千秋。
打印机底层是运行在Unix系统上的
操作步骤
-
环境搭建
Windows开启程序功能—>Telnet Client 功能 -
打开DOS系统 Win+R输入命令cmd
telnet 192.168.0.120 网线连接打印机 打印机IP地址192.168.0.120
login输入:user
$ 输入:busybox
再次输入:Mono apps/HelloWorld.exe -
在VS2017中生成事件—后期生成事件命令行(C:\SmartPrinting\Utils\FtpPut.exe ( T a r g e t F i l e N a m e ) f t p : / / 192.168.1.10 / a p p s / (TargetFileName) ftp://192.168.1.10/apps/ (TargetFileName)ftp://192.168.1.10/apps/(TargetFileName) user pass)。
-
- FileZilla ftp文件软件可以查看打印机的系统文件 字体Font C#程序apps等等。
图片
连接打印机的图片:
VS2017生成事件的图片
:
代码Demo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.IO;
using System.Xml.Serialization;
using Intermec.Printer;
namespace HoneywellPrinterDemo
{
class Program
{
static UI.Canvas canvas;
static UI.Canvas.Timer timer;
static UI.Keypad keypad;
static Communication.Autohunt autohunt;
static string ReadData = "";
static int num = 0;
static Color white = new Color(255, 255, 255, 255);
static Color black = new Color(0, 0, 0, 255);
static UI.Canvas.Text varTextL1; //UI 第一行
static UI.Canvas.Text varTextL2; //UI 第二行
static UI.Canvas.Text varTextL3; //UI 第三行
static void Main(string[] args)
{
Console.WriteLine("HelloWorld Application is running V1.1.4!"); //Telnet 上显示
canvas = new UI.Canvas();
canvas.Clear();
UI.Canvas.Rectangle bg = new UI.Canvas.Rectangle(0, 0, canvas.Width,
canvas.Height, black); //画背景图
canvas += bg;
UI.Canvas.Text titleText = new UI.Canvas.Text(5, 0, "Honeywell 中国", "MHeiGB18030C-Medium", 30, white);//UI第一行标题
canvas += titleText;
varTextL1 = new UI.Canvas.Text(5, 40, "请扫描条码!", "MHeiGB18030C-Medium", 26, white);//UI 第一行文字
canvas += varTextL1;
varTextL2 = new UI.Canvas.Text(5, 70, "", "MHeiGB18030C-Medium", 26, white);//UI 第二行文字
canvas += varTextL2;
varTextL3 = new UI.Canvas.Text(5, 100, "", "MHeiGB18030C-Medium", 26, white);//UI 第三行文字
canvas += varTextL3;
//140
UI.Canvas.Text varTextFeed = new UI.Canvas.Text(5, 170, "按确认键测试认纸", "MHeiGB18030C-Medium", 26, white);
canvas += varTextFeed;
//170
UI.Canvas.Text varTextExit = new UI.Canvas.Text(5, 200, "按返回键退出智能打印程序", "MHeiGB18030C-Medium", 26, white);
canvas += varTextExit;
//Autohunt 自动绑定USB和串口
autohunt = new Communication.Autohunt();
string[] usbPortNames = Communication.USBHost.GetPortNames();
foreach (string usbPortName in usbPortNames)
{
Communication.USBHost usbHost =new Communication.USBHost(usbPortName);
usbHost.Open();
if (usbHost.IsOpen)
{
autohunt.AddStream(usbHost.GetStream());
Console.WriteLine("Added: " + usbPortName);
}
}
// Add Serial ports to autohunt
string[] serialPortNames = Communication.SerialPort.GetPortNames();
foreach (string serialPortName in serialPortNames)
{
Communication.SerialPort serialPort =
new Communication.SerialPort(serialPortName);
serialPort.Open();
serialPort.BaudRate = 115200; //串口参数,可以调整
serialPort.Encoding = System.Text.Encoding.UTF8;
serialPort.Parity = System.IO.Ports.Parity.None;
serialPort.StopBits = System.IO.Ports.StopBits.One;
serialPort.DataBits = 8;
serialPort.Handshake = System.IO.Ports.Handshake.None;
autohunt.AddStream(serialPort.BaseStream);
Console.WriteLine("Added: " + serialPortName); //Telnet上显示
}
keypad = new UI.Keypad(); //键盘事件
keypad.KeyUp += new UI.Keypad.KeyEventHandler(KeyHandlerUp); //键盘事件
timer = new UI.Canvas.Timer(); //定时器
timer.Interval = 500; //刷新间隔500ms
timer.Tick += new UI.Canvas.TimerEventHandler(GetData);
timer.Start();
canvas.Run();
timer.Enabled = false;
keypad.Dispose();
autohunt.Dispose();
canvas.Refresh();
canvas.Exit();
canvas.Dispose();
}
public static void KeyHandlerUp(Object o, UI.Keypad.KeyEventArgs eventArgs) //按键事件
{
int keycode;
keycode = eventArgs.KeyChar;
// Output on console
Console.WriteLine("Key code: " + keycode.ToString());
if (keycode == 9) //返回键
{
timer.Enabled = false;
timer.Stop();
varTextL1.Data = "程序正在退出"; //更新UI第一行
varTextL2.Data = "请耐心等待!"; //更新UI第二行
varTextL3.Data = ""; //更新UI第三行
canvas.Exit();
canvas.Dispose();
}
if (keycode == 11) //走纸
{
Feed(1);
}
if (keycode == 10)
{
Feed(2);
}
}
public static void GetData(Object obj, UI.Canvas.TimerEventArgs eventArgs) //获取USB/串口数据
{
timer.Stop();
string tempL1, tempL2, tempL3, tempL4;
int i;
Byte[] bytes = new Byte[256];
if ((i = autohunt.Read(bytes, 0, bytes.Length)) > 0)
{
string data = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
// Console.WriteLine("ReadData Value : {0}", data);
Console.WriteLine("Begin read {0} bytes: {1}", data.Length, data);
//data.IndexOf("\n") > 0 || data.IndexOf("\r") > 0 ||
if (data.IndexOf("\n") > 0 || data.IndexOf("\r") > 0)
{
data = ReadData + data; //将USB串口数据存储为data
// data = data.Replace("\n", "");//删除回车
//data =data.Replace("\r", "");//删除换行 chinaautoid.com
ReadData = "";
Console.WriteLine("read {0} bytes: {1}", data.Length, data);
varTextL1.Data = "扫描到数据:"; //更新UI第一行
if (data.Length > 20) //如果扫描数据> 20
{
varTextL2.Data = data.Substring(0, 20); //分2行显示,截取前20位
varTextL3.Data = data.Substring(20, data.Length - 20);
}
else
{ //如果扫描数据< 20
varTextL2.Data = data;
varTextL3.Data = "";
}
Console.WriteLine("Print Function!");
if (data.Length == 14)
{
PrintLabelType1(data); //打印第一种标签
}
else
{
PrintLabelType2();
}
canvas.Refresh(); //刷新UI
}
else
{
Console.WriteLine("NOCR read {0} bytes: {1}", data.Length, data);
ReadData += data;
}
}
timer.Start();
}
private static void Feed(int FeedType)
{
PrintControl printControl = new PrintControl();
State state = State.NoError;
if (FeedType == 1)
{
state = printControl.FormFeed();
}
if (FeedType == 2)
{
state = printControl.TestFeed();
}
if (state == State.NoError)
{
Console.WriteLine("Feed 1 label");
}
printControl.Dispose();
}
private static void PrintLabelType1(string data)
{
Console.WriteLine("In Print 1 Function");
PrintControl printControl = new PrintControl();
Drawing drawing = new Drawing();
drawing.Clear();//清空画图
Console.WriteLine(data);
// Status status = new Status();
// State state = status.GetState();
State state = State.NoError;
num++;
Drawing.Text text = new Drawing.Text();
text.CharacterEncoding = CharacterEncoding.UTF8;
text.Point = new Point(100, 150); //打印文字的X,Y
text.Data = "脱机打印SmartSystem"+num.ToString(); //文字内容
text.Height = 12; //文字高度
text.FontName = "MHeiGB18030C-Medium"; //字体高度
drawing += text;
Drawing.Barcode barc = new Drawing.Barcode(120, 50, "CODE128", data); //Code128,X,Y位置,条码内容
barc.Height = 40; //条码高度
barc.Text.Enabled = true; //人眼可识别文字开启
barc.Text.FontName = "Univers"; //字体名
barc.BarWidthWide = 3;
barc.BarWidthNarrow = 1;
barc.WidthMagnification = 1;
drawing += barc;
state = printControl.PrintFeed(drawing, 1); //打印一张
if (state != State.NoError)
{
Console.WriteLine("PrintFeed failed: {0}", state.ToString());
}
Console.WriteLine("Exit");
printControl.Dispose();
drawing.Dispose();
}
private static void PrintLabelType2()
{
Console.WriteLine("In Print 1 Function");
PrintControl printControl = new PrintControl();
Drawing drawing = new Drawing();
drawing.Clear();//清空画图
// Status status = new Status();
// State state = status.GetState();
State state = State.NoError;
Drawing.Text text = new Drawing.Text();
text.CharacterEncoding = CharacterEncoding.UTF8;
text.Point = new Point(100, 150); //打印文字的X,Y
text.Data = "Error"; //文字内容
text.Height = 12; //文字高度
text.FontName = "MHeiGB18030C-Medium"; //字体高度
drawing += text;
Drawing.Text text1 = new Drawing.Text();
text1.CharacterEncoding = CharacterEncoding.UTF8;
text1.Point = new Point(100, 170); //打印文字的X,Y
text1.Data = "Honeyewll"; //文字内容
text1.Height = 12; //文字高度
text1.FontName = "MHeiGB18030C-Medium"; //字体高度
drawing += text1;
state = printControl.PrintFeed(drawing, 1); //打印一张
if (state != State.NoError)
{
Console.WriteLine("PrintFeed failed: {0}", state.ToString());
}
Console.WriteLine("Exit");
printControl.Dispose();
drawing.Dispose();
}
private static void PrintLabelType3(string data)
{
Console.WriteLine("In Print 2 Function");
PrintControl printControl = new PrintControl();
Drawing drawing = new Drawing();
drawing.Clear();//清空画图
//Status status = new Status();
// State state = status.GetState();
State state = State.NoError;
//Console.WriteLine("Printer State: {0}", state.ToString());
Drawing.Text text = new Drawing.Text();
text.Point = new Point(150, 30); //打印文字的X,Y
text.Data = data; //文字内容
text.Height = 12; //文字高度
text.FontName = "MHeiGB18030C-Medium"; //字体高度
drawing += text;
state = printControl.PrintFeed(drawing, 1);
if (state != State.NoError)
{
Console.WriteLine("PrintFeed failed: {0}", state.ToString());
}
Console.WriteLine("Exit");
printControl.Dispose();
drawing.Dispose();
}
}
}