20 Replies Latest reply on Apr 16, 2018 6:14 AM by James Sack

    We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.

    James Sack

      We use the following to print:

       

                   // Zebra KR403 Receipt Printer

                  var enumDevices = Zebra.Printing.UsbPrinterConnector.EnumDevices();

                  if (enumDevices.Keys.Count > 0)

                  {

                      string key = enumDevices.Keys[0];

                      Zebra.Printing.UsbPrinterConnector connector = new Zebra.Printing.UsbPrinterConnector(key);

       

                      string command1 = @"^XA^PW464~SD15" +

                      @"^FO05,100^A0N,32,32^FB450,1,0,C^FDTest Line^FS ";

       

                      command1 = command1 +

                      @"^CN1

                      ^PN0        

                      ^XZ";

       

                      byte[] buffer1 = ASCIIEncoding.ASCII.GetBytes(command1);

                      connector.IsConnected = true;

                      connector.Send(buffer1);

       

                      // Read paper status

                      string spaperout = "^XA~HQES^XZ\n";

                      byte[] buffer2 = new byte[];

                      buffer2 = ASCIIEncoding.ASCII.GetBytes(spaperout);

                      connector.Send(buffer2);

                      byte[] buffer3 = new byte[];

                      int bytesread = connector.Read(buffer3, 0, 512);

              }

       

      We encounter errors while trying to read the low paper status.

       

      Jim

        • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
          Robin West

          Hi Jim,

          What errors are you getting?

          Also not all Zebra printers support a low paper sensor.  Most will only give a paper out error.  Which printer are you using?

          Robin

            • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
              James Sack

              Robin,

               

              The printers we are using in all of our kiosks is the KR403 unit with the paper sensor option.

               

              The errors we are getting occur in the Zebra.Printing.UsbPrinterConnector.Read  function.  It is telling us the buffer is not large enough.  I have tried making buffer size as large as 8192 when I call this function.  Is my approach even correct?  I am really looking for a proper example how to read the status in C# using this.

               

              The code from the UsbPrinterConnector.Read function is below.

               

                      public override int Read(byte[] buffer, int offset, int count)

                      {

                          // USB 1.1 ReadFile in block chunks of 64 bytes

                          // USB 2.0 ReadFile in block chunks of 512 bytes

                          uint read;

               

               

                          if (readBuffer == null)

                              readBuffer = new byte[ReadBufferSize];

               

               

                          AutoResetEvent sg = new AutoResetEvent(false);

                          NativeOverlapped ov = new NativeOverlapped();

                          ov.OffsetLow = 0;

                          ov.OffsetHigh = 0;

                          ov.EventHandle = sg.Handle;

               

               

                          if (!FileIO.ReadFile(usbHandle, readBuffer, ReadBufferSize, out read, ref ov))

                          {

                              if (Marshal.GetLastWin32Error() == FileIO.ERROR_IO_PENDING)

                                  sg.WaitOne(ReadTimeout, false);

                              else

                                  throw new Win32Exception(Marshal.GetLastWin32Error());

                          }

                          FileIO.GetOverlappedResult(usbHandle, ref ov, out read, true);

               

               

                          Array.Copy(readBuffer, 0, buffer, offset, read);

                          return (int)read;

                      }

            • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
              James Sack

              Robin,

               

              Have you been able to identify the correct Zebra.Printing.UsbPrinterConnector that has the SendAndWaitFor Response method?

               

              Jim

                • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                  James Sack

                  Robin,

                   

                  Has a Zebra.Printing.UsbPrinterConnector been located so we can use the SendAndWaitForResponse method?

                   

                  Jim

                    • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                      James Sack

                      Hi Robin,

                       

                      Is there an update to locating a Zebra.Printing.UsbPrinterConnector with the SendAnd WaitForResponse method?

                       

                      Jim

                        • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                          Robin West

                          Hi James, Sorry again for the delay. 

                          The C# SDK that you are using - Do you mean the one for the Windows Mobile/Embedded devices, or the newest one for PC's?

                          If the Windows Embedded, you will have to essentially create your own SendAndWaitForResponse - see below for some basic code.  With the PC SDK, there are several methods for this in the Connection class.

                           

                          public byte[] SendAndWaitForResponse(byte[] dataToSend, int initialResponseTimeout, int responseCompletionTimeout, string terminator = null)

                                  {

                                      connection.Write(dataToSend);

                                      connection.WaitForData(initialResponseTimeout);

                                      if (connection.BytesAvailable() == 0)

                                          throw new ConnectionException("Initial Response Timeout.  No data found.");

                                      string response = "";

                           

                           

                                      while ((connection.BytesAvailable() > 0) && (responseCompletionTimeout > 0))

                                      {

                                          bool found = false;

                           

                           

                                          // try reading for more data

                                          byte[] temp = connection.Read();

                                          response += Encoding.Default.GetString(temp);

                           

                           

                                          // look for the terminator string

                                          if ((terminator != null) && (response.Contains(terminator)))

                                          {

                                              found = true;

                                              response = response.Substring(0, response.IndexOf(terminator));

                                          }

                           

                           

                                          responseCompletionTimeout--;

                                          if (found)

                                              responseCompletionTimeout = 0;

                                      }

                                      return Encoding.UTF8.GetBytes(response);

                                  }

                            • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                              James Sack

                              Robin,

                               

                              We now understand we have been using a different code source to communicate with the Zebra printer KR403.

                               

                              We would like to use the Zebra SDK for PC and follow these steps:

                               

                              Steps to add the C# SDK to your Visual Studio project from NuGet

                              1. Open your Visual Studio project.
                              2. Click Project > Manage NuGet Packages....
                              3. Select the Browse tab.
                              4. Set your Package source to nuget.org.
                              5. Search for Zebra.Printer.SDK.
                              6. Select Zebra.Printer.SDK from the list.
                              7. Click Install.

                              We cannot locate the Zebra.Printer.SDK at nuget.org in order to install it in our Visual Studio project.  Are there newer instructions?

                               

                              Jim

                                • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                                  Robin West

                                  Hi James, Sorry, the SDK is not on Nuget yet.  It has to be downloaded from Zebra.com.  The manual install instructions on the page you are looking at are also not clear.  Please follow the instructions below:

                                  I highly recommend coping all of the files from the SDK install location "\link_os_sdk\PC-.NET\v2.13.898\lib" folder into a folder in your project then add references to the three SdkApi libraries in the copied folder. I go through this in more depth in the webinar below.https://youtu.be/UEKxdy224JA?t=253

                                  SdkApi_Core.dll

                                  SdkApi_Desktop.dll

                                  SdkApi_Desktop_Usb.dll

                                  1 of 1 people found this helpful
                                    • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                                      James Sack

                                      Robin,

                                       

                                      We are attempting to install the Windows Installer for SDK as described.

                                       

                                      The mpsdk-installer-2-13.exe file was downloaded.  Early in the extraction the LaunchAnywhere Error "Could not find a valid Java virtual machine to load.  You may need to reinstall a supported Java virtual machine."

                                       

                                      The installer is then closed.

                                       

                                       

                                       

                                      Jim

                                      • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                                        James Sack

                                         

                                         

                                         

                                        We are attempting to install the Windows Installer for SDK as described.

                                         

                                         

                                         

                                        The mpsdk-installer-2-13.exe file was downloaded.  Early in the extraction

                                        the LaunchAnywhere Error "Could not find a valid Java virtual machine to

                                        load.  You may need to reinstall a supported Java virtual machine."

                                         

                                         

                                         

                                        The installer is then closed.

                                         

                                         

                                        We cannot install the package as directed.

                                         

                                         

                                         

                                        On Mon, Feb 12, 2018 at 2:29 AM, Samyak Upadhyay <

                                          • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                                            Robin West

                                            Hi James, you either need to install Java v6, v7, or v8, or contact tech support on this.  Our team can't do anything about installer issues.

                                              • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                                                James Sack

                                                Robin,

                                                 

                                                I have finally been able to install the link_os_sdk.

                                                 

                                                I have added the SdkApi_Core, SdkApi_Desktop and SdkApi_Desktop_USB references to my ASP.NET project.

                                                 

                                                I have reviewed your code and have several questions.

                                                 

                                                                // Read paper status

                                                                string spaperout = "~HQES";

                                                                string terminator = 0x03;  // hex ETX

                                                                byte[] buffer2 = ASCIIEncoding.ASCII.GetBytes(spaperout);

                                                                byte[] buffer3 = connector.SendAndWaitForResponse(buffer2,3000,500,terminator);

                                                             

                                                                if ((0 == buffer3.length) || (2 != buffer3[0]))

                                                                    // read failed or read in extra data from some other command.

                                                                 

                                                                // location of paper low bit I believe is 134, check if this byte is ASCII '8'

                                                                if ( 56 <= buffer3[134])

                                                                {

                                                                    // show paper low message

                                                                }             

                                                 

                                                 

                                                How do I connect to the USB Zebra printer?

                                                 

                                                In order to connect to the printer via USB I added

                                                               Zebra.Sdk.Comm.UsbConnection connector = new UsbConnection(key);

                                                 

                                                What should the string key be?

                                                 

                                                I also changed the line of code for the terminator to

                                                               string terminator = "\x03";

                                                 

                                                We appreciate your continued help.

                                                 

                                                Jim

                                                  • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                                                    Robin West

                                                    Hi Jim,

                                                    We recommend using the USBDiscoverer to enumerate valid connections because USB on PC allows for changing virtual port numbers and so the connection string may only be valid for a short time.

                                                    Some very basic sample code for how to do this:

                                                     

                                                            public List<DiscoveredPrinter> GetUSBPrinters()
                                                            {
                                                                List<DiscoveredPrinter> printerList = new List<DiscoveredPrinter>();
                                                                try
                                                                {
                                                                    foreach (DiscoveredUsbPrinter usbPrinter in UsbDiscoverer.GetZebraUsbPrinters())
                                                                    {
                                                                        printerList.Add(usbPrinter);
                                                                        Console.WriteLine(usbPrinter);
                                                                    }
                                                                }
                                                                catch (ConnectionException e)
                                                                {
                                                                    Console.WriteLine($"Error discovering local printers: {e.Message}");
                                                                }
                                                    
                                                    
                                                                Console.WriteLine("Done discovering local printers.");
                                                                return printerList;
                                                            }
                                                            public void basicPrint(string zpl)
                                                            {
                                                                List<DiscoveredPrinter> printerList = GetUSBPrinters();
                                                                string ZPL_STRING = "^XA^FO50,50^A0N,50,50^FDHello World^FS^XZ";
                                                               
                                                                if (printerList.Count > 0)
                                                                {
                                                                    // in this case, we arbitrarily are printing to the first found printer
                                                                    DiscoveredPrinter discoveredPrinter = printerList[0];
                                                                    Connection connection = discoveredPrinter.GetConnection();
                                                                    connection.Open();
                                                                    connection.Write(Encoding.UTF8.GetBytes(ZPL_STRING));
                                                                }
                                                                else Console.WriteLine("No Printers found to print to.");
                                                    
                                                    
                                                            }
                                                    

                                                    Edited lines 30 and 31

                                                    • Re: We are in need of a working code example in C# that uses the Zebra.Printing.UsbPrinterConnector to query the printer status for the low paper condition.
                                                      James Sack

                                                      Robin,

                                                       

                                                      Successful code was developed to read the Zebra kr403 printer status.

                                                       

                                                      We were publishing our ASP.NET project to the web when one of the references from the SDK  PC-.NET caused an issue.

                                                       

                                                      The package ID in 'SdkApi_Core' contains invalid characters.

                                                       

                                                      The publishing process stops at this point.

                                                      We have not seen this type of issue before. How can this issue be handled?

                                                       

                                                      The exact text from the dnu publish command follows:

                                                       

                                                      //////////////////////////////////////////////////////////////////////////////////////

                                                      Using Project dependency SdkApi_Core 1.0.0 for DNX,Version=v4.5.1

                                                        Packing nupkg from Project dependency SdkApi_Core

                                                          Source C:\inetpub\wwwroot\SCKiosk\wrap\SdkApi_Core\project.json

                                                          Target C:\inetpub\wwwroot\SCKiosk\src\SCKiosk\bin\output\approot\packages\SdkApi_Core\1.0.0

                                                       

                                                       

                                                      Building SdkApi_Core for DNX,Version=v4.5.1

                                                        Using Project dependency SdkApi_Core 1.0.0

                                                          Source: C:\inetpub\wwwroot\SCKiosk\wrap\SdkApi_Core\project.json

                                                       

                                                       

                                                        Using Project dependency BouncyCastle.Crypto 1.0.0

                                                          Source: C:\inetpub\wwwroot\SCKiosk\wrap\BouncyCastle.Crypto\project.json

                                                       

                                                       

                                                        Using Assembly dependency fx/mscorlib 4.0.0

                                                          Source: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\mscorlib.dll

                                                       

                                                       

                                                        Using Assembly dependency fx/System 4.0.0

                                                          Source: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.dll

                                                       

                                                       

                                                        Using Assembly dependency fx/System.Core 4.0.0

                                                          Source: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\System.Core.dll

                                                       

                                                       

                                                        Using Assembly dependency fx/Microsoft.CSharp 4.0.0

                                                          Source: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Microsoft.CSharp.dll

                                                       

                                                       

                                                      Error: The package ID 'SdkApi_Core' contains invalid characters. Examples of valid package IDs include 'MyPackage' and

                                                      MyPackage.Sample'.

                                                      //////////////////////////////////////////////////////////////////////////////////////

                                                       

                                                      Has there been any discussion regarding changing the package ID to remove the underscore character?  It is preventing the latest dnx package (dnx-clr-win-x86 1.0.0-rc1-update2) from publishing the C# solution.

                                                       

                                                      Jim