I am trying to simulate sending raw data to a bar code printer. Since I don't have one, I'm just sending some text to a Canon ink-jet printer on my network. Problem is when the code executes nothing happens. I got this code from a prior post (thank-you). Can you see anything that I might be missing or doing wrong? Thanks.
namespace ConsoleAppTest
{
class Program
{
static void Main(string[] args)
{
string text = "^XA" +
"^FO335,22,^CI0^A0,14,14^FR^FDConta^FS" +
"^FO368,22,^CI0^A0,14,14^FR^FDins^FS" +
"^PQ1" + "^XZ";
PrintToZebra zPrintToIP = new PrintToZebra("192.168.1.101",80, text);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace PMIConsoleAppTest
{
class PrintToZebra
{
public string printerIP { get; set; }
public int printerPort { get; set; }
public string myZPL { get; set; }
private EndPoint ep { get; set; }
private Socket sock { get; set; }
private NetworkStream ns { get; set; }
public PrintToZebra()
{
printerIP = "";
printerPort = 0;
myZPL = "";
}
public PrintToZebra(string anIP, int aPort, string aZPL)
{
printerIP = anIP;
printerPort = aPort;
myZPL = aZPL;
printToIP();
}
public void printToIP()
{
ep = new IPEndPoint(IPAddress.Parse(printerIP), printerPort);
sock = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
sock.Connect(ep);
ns = new NetworkStream(sock);
byte[] toSend = Encoding.ASCII.GetBytes(myZPL);
ns.BeginWrite(toSend, 0, toSend.Length, OnWriteComplete, null);
ns.Flush();
}
catch (Exception ex)
{
//Console.WriteLine(ex.ToString());
sock.Shutdown(SocketShutdown.Both);
sock.Close();
}
}
private void OnWriteComplete(IAsyncResult ar)
{
NetworkStream thisNS = ns;
thisNS.EndWrite(ar);
sock.Shutdown(SocketShutdown.Both);
sock.Close();
}
}
}