Search code examples
c#socketspdfposnetwork-printers

How to print a bill from POS printer in .net using socket?


I have an ASP Web API and network printer(Epson TM-U220). I need to select the printer by code and print a bill. I just try as bellow. But not work fine. I want to print this direct using pos printer

var server = "192.168.1.164";
var nome = "www.pdf";

Socket clientSocket = new Socket(AddressFamily.InterNetwork, 
SocketType.Stream, ProtocolType.Tcp);
clientSocket.NoDelay = true;

IPAddress ip = IPAddress.Parse(server);
IPEndPoint ipep = new IPEndPoint(ip, 9100);
clientSocket.Connect(ipep);

clientSocket.Send(File.ReadAllBytes(nome));
clientSocket.Close();

Solution

  • I try this and it works fine. But still, I cannot get printer status.

    //--printer script model
    public class DirectPrintingScript
    {
        public string Value { get; set; }
        public int PrintMethodID { get; set; }
        public bool IsNeedPrint { get; set; }
    }
    
    //--print method
    public void print(List<DirectPrintingScript> result, string IP, int Port)
    {
        var job = new DirectPrinterProcess();
        Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        clientSocket.NoDelay = true;
    
        IPAddress ip = IPAddress.Parse(IP);
        IPEndPoint ipep = new IPEndPoint(ip, Port);
        clientSocket.Connect(ipep);
        Encoding enc = Encoding.ASCII;
    
        foreach (DirectPrintingScript item in result)
        {
            var command = job.SelectMethod(item.PrintMethodID);
            byte[] commandBytes = Encoding.ASCII.GetBytes(command);
            byte[] contentBytes = Encoding.ASCII.GetBytes(item.Value);
            clientSocket.Send(commandBytes);
    
            if (item.IsNeedPrint)
            {
               clientSocket.Send(contentBytes);
               var n = job.NewLine();
               byte[] nBytes = Encoding.ASCII.GetBytes(n);
               clientSocket.Send(nBytes);
            }
         }
    
         // Line feed hexadecimal values
         byte[] bEsc = new byte[4];
         bEsc[0] = 0x0A;
         bEsc[1] = 0x0A;
         bEsc[2] = 0x0A;
         bEsc[3] = 0x0A;
    
         // Send the bytes over 
         clientSocket.Send(bEsc);
    
         clientSocket.Close();
    }
    
    //--print method process
        public class DirectPrinterProcess
        {
            public string SelectMethod(int MethodID)
            {
                switch (MethodID)
                {
                    case 1:
                        return JustificationCenter();
                    case 2:
                        return JustificationLeft();
                    case 3:
                        return DoubleHeight();
                    case 4:
                        return DoubleWidth();
                    case 5:
                        return CancelDoubleHeightWidth();
                    case 6:
                        return SetColorRed();
                    case 7:
                        return SetColorBlack();
                    default:
                        return CancelDoubleHeightWidth();
                }
            }
    
            private string JustificationCenter()
            {
                return "" + (char)27 + (char)97 + (char)1;
            }
    
            private string JustificationLeft()
            {
                return "" + (char)27 + (char)97 + (char)0;
            }
    
            private string DoubleHeight()
            {
                return "" + (char)27 + (char)33 + (char)16;
            }
    
            private string DoubleWidth()
            {
                return "" + (char)27 + (char)33 + (char)32;
            }
    
            private string CancelDoubleHeightWidth()
            {
                return "" + (char)27 + (char)33 + (char)0;
            }
    
            private string SetColorRed()
            {
                return "" + (char)27 + (char)114 + (char)1;
            }
    
            private string SetColorBlack()
            {
                return "" + (char)27 + (char)114 + (char)0;
            }
    
            public string NewLine()
            {
                return "" + "\n";
            }
        }