0

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();
    }

}
}
  • Well you are sending bar code commands to a normal printer... this doesn't surprise me that much if it doesn't work... – Tommaso Belluzzo Jan 15 '13 at 23:50
  • look at this http://stackoverflow.com/questions/8540468/emulate-zebra-printer – Antonio Bakula Jan 16 '13 at 00:15
  • I changed the bar code commands to normal text and it still doesn't work and I don't get any exceptions. I moved the two lines above the Try to ensure that wasn't causing an exception I didn't see. – Darrell Mulder Jan 16 '13 at 14:18
  • Can I use the Socket class to print to my local wireless printer? Maybe that is causing the issue. I do not have a server that can 'listen' for incoming requests. If this is required, what kind of 'server' would I need? – Darrell Mulder Jan 16 '13 at 15:45

0 Answers0