Here are the guts of it - I need to sanitize before I share the whole project but these are the most important bits:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
public static class Zebra
{
public static ZebraStatus GetStatus(string name)
{
// Printer IP Address and communication port
string ipAddress = name;
int port = 9100; //the default, did you change it?
// ZPL Command(s)
string ZPLString = "~HS";
/*"^XA" +
"^FO50,50" +
"^A0N50,50" +
"^FDHello, World!^FS" +
"^XZ";*/
try
{
// Open connection
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
client.SendTimeout = 3000;
client.ReceiveTimeout = 5000;
var success = client.ConnectAsync(ipAddress, port).Wait(1800); //conn timeout
if (!success)
{
// throw new Exception("Failed to connect.");
var resp2 = new ZebraStatus();
resp2.Name = name;
return resp2;
}
// Write ZPL String to connection
System.IO.StreamWriter writer =
new System.IO.StreamWriter(client.GetStream());
writer.Write(ZPLString);
writer.Flush();
var ns = client.GetStream();
var buflen = client.Available;
var attempts = 0;
while (buflen == 0 && attempts++ < 20)
{
System.Threading.Thread.Sleep(10);
buflen = client.Available;
}
var mybuf = new byte[buflen];
//ns.Read(mybuf, 0, (int)ns.Length);
var cnt = 0;
var abort = false;
while (cnt < buflen && !abort)
{
int val = ns.ReadByte();
if (val == -1)
{
abort = true;
}
mybuf[cnt++] = (byte)val;
}
var time = stopWatch.ElapsedMilliseconds;
stopWatch.Stop();
// Close Connection
ns.Close();
writer.Close();
client.Close();
//client.EndConnect(clientConn);
var resp = System.Text.Encoding.ASCII.GetString(mybuf);
var status = new ZebraStatus();
status.LoadFromString(resp);
status.Name = ipAddress;
status.ResponseTime = time;
//PrinterResponse.Text = status.ToString();
return status;
}
catch (Exception ex)
{
var resp = new ZebraStatus();
resp.Name = name;
//PrinterResponse.Text = resp.ToString();
return resp;
//throw;
// Catch Exception
}
}
}
public class ZebraStatus
{
//030,0,0,1244,000,0,0,0,000,0,0,0
//comm, paperout,pause, labellen, formats, BUFF FULL, Diagnostics, PartialFormat, 3 0's, crpt ram, temp under, temp over
//001,0,0,0,1,2,6,0,00000000,1,000
//Func, unused, head open, ribbon out, thermal mode, print mode, print width, label waiting, labels remaining (8), 1, num of images(3)
//0000,0
//ignore
public string Name;
public string IP;
public bool PaperOut;
public bool Paused;
public bool BufferFull;
public bool Diagnostics;
public bool PartialFormat;
public bool CorruptRam;
public bool OverTemp;
public bool UnderTemp;
public bool HeadOpen;
public bool RibbonOut;
public bool ThermalTransfer;
public int PrintMode;
public bool LabelWaiting;
public bool Connected = false;
public long ResponseTime = 0;
public bool IsOnline = false;
public DateTime LastUpdate = DateTime.Now;
public DateTime? LastResponse = null;
public bool IsQuerying = true;//false;
public bool LoadFromString(string input)
{
var statuslist = input.Split(',');
try
{
PaperOut = statuslist[1] == "1";
Paused = statuslist[2] == "1";
BufferFull = statuslist[5] == "1";
Diagnostics = statuslist[6] == "1";
PartialFormat = statuslist[7] == "1";
CorruptRam = statuslist[8] == "1";
OverTemp = statuslist[9] == "1";
UnderTemp = statuslist[10] == "1";
HeadOpen = statuslist[13] == "1";
RibbonOut = statuslist[14] == "1";
ThermalTransfer = statuslist[15] == "1";
PrintMode = Convert.ToInt32(statuslist[16]);
LabelWaiting = statuslist[17] == "1";
IsOnline = true;
IsQuerying = false;
LastResponse = DateTime.Now;
}
catch(Exception e)
{
Connected = false;
return false;
}
Connected = true;
return true;
}
public override string ToString()
{
string n = Environment.NewLine;
string response = this.Connected ?
$"Name: {this.Name}{n}ResponseTime(ms):{this.ResponseTime}{n}Paused:{this.Paused}{n}PaperOut:{this.PaperOut}{n}BufferFull:{this.BufferFull}{n}HeadOpen:{this.HeadOpen}{n}RibbonOut:{this.RibbonOut}{n}LabelWaiting:{this.LabelWaiting}{n}" :
$"Name: { this.Name}{n}Not Connected!";
return response;
}
}
----- USAGE-----
ZebraStatus status = Zebra.GetStatus(PrinterIPorHostName);
Give me a bit and I’ll share the project (with the disclaimer I am not a webdev)