Commit 4fe7e079 刘韬

1

1 个父辈 d0308049
此文件太大,无法显示。
......@@ -145,8 +145,13 @@ namespace Asa
while (true)
{
printer.Get();
int ret = Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
int ret = (int)PrinterStatus.Idle;
try
{
printer.Get();
ret = Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
}
catch { }
sta = (PrinterStatus)ret;
switch(sta)
{
......
......@@ -39,6 +39,8 @@ namespace PrintLabel_Test
this.label2 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label3 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button3
......@@ -106,19 +108,38 @@ namespace PrintLabel_Test
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Interval = 2000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(253, 246);
this.label3.Location = new System.Drawing.Point(251, 201);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 12);
this.label3.TabIndex = 9;
this.label3.Text = "label3";
//
// button4
//
this.button4.Location = new System.Drawing.Point(634, 12);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(123, 49);
this.button4.TabIndex = 5;
this.button4.Text = "USB直接连接";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(450, 304);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(123, 49);
this.button5.TabIndex = 5;
this.button5.Text = "设置IP地址";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
......@@ -129,6 +150,8 @@ namespace PrintLabel_Test
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button2);
this.Controls.Add(this.button3);
this.Controls.Add(this.button1);
......@@ -151,5 +174,7 @@ namespace PrintLabel_Test
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Zebra.Sdk.Printer.Discovery;
namespace PrintLabel_Test
{
......@@ -79,5 +80,39 @@ namespace PrintLabel_Test
});
});
}
private void button4_Click(object sender, EventArgs e)
{
var dl = ZebraPrinterHelper.ZebraManger.GetUsbDirectAddress();
textBox1.Text = dl.First().Key;
zebraManger = new ZebraPrinterHelper.ZebraManger(textBox1.Text, ZebraPrinterHelper.ConnectionType.UsbDirect);
if (!zebraManger.Connection(out string msg))
{
MessageBox.Show(msg);
}
else
MessageBox.Show("连接成功");
}
private void button5_Click(object sender, EventArgs e)
{
string cmd = @"^XA
^ND2,P,192.168.3.4,255.255.255.0,0.0.0.0
^NBC
^NC1
^NPP
^XZ
^XA
^JUS
^XZ
~PP
";
var cmd2 = "! U1 setvar \"media.printmode\" \"P\"";//p=剥纸,T=撕纸
var cmd3 = "~TA048";//剥纸距离调整 value=cm*80, 048=0.6cm
}
}
}
......@@ -38,6 +38,10 @@
<HintPath>..\..\ConfigHelper\ConfigHelper\bin\Debug\ConfigHelper.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="SdkApi.Desktop, Version=2.15.2634.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\ZebraDemoSource\Sdk\SdkApi.Desktop.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
......
......@@ -29,6 +29,7 @@ namespace ZebraPrinterHelper
ConnectionType = connectionType;
}
Connection printerConnection = null;
ZebraPrinter printer;
Connection GetConnection()
{
if (ConnectionType == ConnectionType.Network)
......@@ -82,6 +83,7 @@ namespace ZebraPrinterHelper
printerConnection = GetConnection();
printerConnection.Open();
PrinterDPI = GetPrinterDPI();
printer = ZebraPrinterFactory.GetInstance(printerConnection);
return true;
}
catch (ConnectionException e)
......@@ -143,8 +145,8 @@ namespace ZebraPrinterHelper
{
if (!CheckAndGetStatus(out PrinterStatus status, out msg))
return false;
CancelAll();
if (status.labelsRemainingInBatch>0)
CancelAll();
if (!status.isReadyToPrint) {
string[] printerStatusString = new PrinterStatusMessages(status).GetStatusMessage();
......@@ -159,7 +161,9 @@ namespace ZebraPrinterHelper
try
{
ZebraImageI image = ZebraImageFactory.GetImage(bmp);
ZebraPrinterFactory.GetInstance(printerConnection).PrintImage(image, 0, 0, 0, 0, false);
var width = image.Width * (PrinterDPI/203f);
var height = image.Height * (PrinterDPI / 203f);
printer.PrintImage(image, 0, 0, (int)width, (int)height, false);
return true;
}
catch (ConnectionException e)
......@@ -245,23 +249,19 @@ namespace ZebraPrinterHelper
public int GetPrinterDPI()
{
try
string d = "! U1 getvar \"head.resolution.in_dpi\"\n";
var b = Encoding.ASCII.GetBytes(d);
var rd = printerConnection.SendAndWaitForResponse(b, 5000, 500, null);
var rs = Encoding.ASCII.GetString(rd);
if (rs.Length > 0)
{
string d = "! U1 getvar \"head.resolution.in_dpi\"\n";
var b = Encoding.ASCII.GetBytes(d);
var rd = printerConnection.SendAndWaitForResponse(b, 5000, 500, null);
var rs = Encoding.ASCII.GetString(rd);
if (rs.Length > 0)
{
rs = rs.Trim('\"');
return int.Parse(rs);
}
return 203;
rs = rs.Trim('\"');
return int.Parse(rs);
}
catch
{
return 203;
else {
throw new ZebraPrinterLanguageUnknownException("Get DPI Error!");
}
return 203;
}
/// <summary>
......@@ -334,15 +334,22 @@ namespace ZebraPrinterHelper
}
try
{
ZebraPrinter printer = ZebraPrinterFactory.GetInstance(printerConnection);
ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);
return (linkOsPrinter != null) ? linkOsPrinter.GetCurrentStatus() : printer.GetCurrentStatus();
return printer.GetCurrentStatus();
//ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);
//return (linkOsPrinter != null) ? linkOsPrinter.GetCurrentStatus() : printer.GetCurrentStatus();
}
catch {
return null;
}
}
public static Dictionary<string, Dictionary<string,string>> GetUsbDirectAddress() {
var devicelist = new Dictionary<string, Dictionary<string, string>>();
foreach (DiscoveredUsbPrinter printer in UsbDiscoverer.GetZebraUsbPrinters(new ZebraPrinterFilter()))
{
devicelist[printer.Address] = printer.DiscoveryDataMap;
}
return devicelist;
}
}
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!