Ethernet POS Documentation - C# sample of named pipe usage

Notes

This code shows the contents of a C# application main file. It uses the bidirectionnal communication mode of Ethernet POS to request printer informations. The printer must be totaly compatible with Epson® ESC/POS commands, and it must be connected directly with Ethernet POS (using an USB port, a serial port or a parallel port).

To test this code, download the project ZIP archive, extract it, and open the solution file (EthPosSample.sln) with Microsoft Visual Studio.

Code using System;
using System.Threading;
using System.Text;
using System.Windows.Forms;
using System.IO.Pipes;

namespace EthPosSample
{
    public partial class Main : Form
    {
        const string ESC = "\x1b";
        const string FS = "\x1c";
        const string GS = "\x1d";

        private NamedPipeClientStream pipe;
        private string printerModel;
        private byte printerId;
        private byte printerTypeId;
        private byte printerRomVid;

        public Main()
        {
            InitializeComponent();
        }

        private void request_Click(object sender, EventArgs e)
        {
            request.Enabled = false;
            Cursor = Cursors.WaitCursor;

            try {
                RequestPrinterInfo();
            }
            catch (Exception ex) {
                MessageBox.Show("Error: " + ex.Message,
                                null,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }

            // Set control values to received or blank values.
            tbPrinterModel.Text = printerModel ?? string.Empty;
            tbPrinterId.Text = printerId.ToString();
            tbPrinterTypeId.Text = printerTypeId.ToString();
            tbPrinterRomVid.Text = printerRomVid.ToString();
    
            request.Enabled = true;
            Cursor = Cursors.Default;
        }

        private void quit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void RequestPrinterInfo()
        {
            byte[] read = new byte[1024];
            int success = 0;
            int res;

            // Reset printer informations before executing commands.
            printerModel = null;
            printerId = 0;
            printerTypeId = 0;
            printerRomVid = 0;

            // Printer ID request command.
            if (SendCmdAndGetReply(GS + "I" + (char)1, read) > 0) {
                printerId = read[0];
                success++;
            }

            // Printer Type ID request command.
            if (SendCmdAndGetReply(GS + "I" + (char)2, read) > 0) {
                printerTypeId = read[0];
                success++;
            }

            // Printer ROM Version ID request command.
            if (SendCmdAndGetReply(GS + "I" + (char)3, read) > 0) {
                printerRomVid = read[0];
                success++;
            }

            // Printer model request command.
            res = SendCmdAndGetReply(GS + "I" + (char)67, read);
            if (res > 1) {
                printerModel =
                    Encoding.Default.GetString(read, 1, res - 1).Trim();
                success++;
            }

            // Close pipe if opened once done.
            if (pipe != null) {
                pipe.Dispose();
                pipe = null;
            }

            // Throw an error if no reply has been received.
            if (success == 0)
                throw new Exception("No reply has been received from printer");
        }

        private int SendCmdAndGetReply(string cmd, byte[] read)
        {
            // Open pipe on demand.
            if (pipe == null) {
                pipe = new NamedPipeClientStream("ethpos_printer_0");
                pipe.Connect(5 * 1000);
            }

            // Send command as byte array.
            byte[] rawCmd = Encoding.Default.GetBytes(cmd);
            pipe.Write(rawCmd, 0, rawCmd.Length);

            // Read asynchronously.
            IAsyncResult ar = pipe.BeginRead(read, 0, read.Length, null, null);
    
            // Wait for at most 2s, and return read bytes count on success.
            if (ar.AsyncWaitHandle.WaitOne(2 * 1000))
                return pipe.EndRead(ar);

            // Timeout occured, in this framework version, the way to cancel
            // an asynchronous IO is to close and reopen the pipe.
            pipe.Dispose();
            pipe = null;

            return -1;
        }
    }
}

Download cs-named-pipe.zip