CAESGenome.Services.BarcodeService.Print C# (CSharp) Method

Print() public method

public Print ( int id, string plateName ) : void
id int
plateName string
return void
        public void Print(int id, string plateName)
        {
            var address = IPAddress.Parse(_printer);
            var endPoint = new IPEndPoint(address, _printerPort);

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // all commands are for SATO CL412e Printer

            try
            {
                // open connection
                socket.Connect(endPoint);

                // escape command
                var cmd = Encoding.ASCII.GetBytes(Strings.Chr(27).ToString());

                // trigger the printer
                socket.Send(cmd);
                // start of printer command
                socket.Send(Encoding.ASCII.GetBytes("A"));
                socket.Send(cmd);
                // horizontal command
                socket.Send(Encoding.ASCII.GetBytes("H0210"));
                socket.Send(cmd);
                // vertical command
                socket.Send(Encoding.ASCII.GetBytes("V0010"));
                socket.Send(cmd);
                // BG is for printing barcode (code128)
                socket.Send(Encoding.ASCII.GetBytes(string.Format("BG03090 {0} . {1}", plateName, id)));
                socket.Send(cmd);
                socket.Send(Encoding.ASCII.GetBytes("H0250"));
                socket.Send(cmd);
                socket.Send(Encoding.ASCII.GetBytes("V0100"));
                // XM is font command for print CGF and data
                socket.Send(Encoding.ASCII.GetBytes(string.Format("XMCGF {0}", id)));
                socket.Send(cmd);
                // print 1 label
                socket.Send(Encoding.ASCII.GetBytes("Q1"));
                socket.Send(cmd);
                // end of command
                socket.Send(Encoding.ASCII.GetBytes("Z"));

                // release the socket
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            catch
            {
                throw;
            }
        }