title: Why is the wrong barcode being printed?
body: <p>How could it be that one barcode is being read, yet another one is printed?</p>
  
  <p>My code reads the value scanned and puts it in a textbox; e.g., a value such as "76145513"</p>
  
  <p>Yet, when I print a label for that barcode, it prints "57056548"</p>
  
  <p>This last is a valid barcode, but why is that ("57056548") printed instead of the value scanned ("76145513")?</p>
  
  <p>I am reading the barcodes from a shhet of paper that has many "outsized" barcodes on it. The sheet I'm using contains both the value being scanned AND the one being printed, but I even went so far as to cover up the rogue barcode when scanning to be certain that the laser beam wasn't wall-eyed and "seeing" the printed value, although its "cyclops beam" does light up the correct barcode only.</p>
  
  <p>I put some debug/sanity check code in to make sure what was being sent to print was correct, and it is; I see, "<em>Barcode  in PrintUtils.ZebraQLn220Printer.PrintLabel is 76145513</em>" in the first MessageBox.Show() and "<em>barcode with checksum is 761455132</em>" with the second one.</p>
  
  <p>So how could it be that the correct value is being scanned, sent to the Print method, and yet a different (but valid!) code is being printed?</p>
  
  <p>Here is the core print code:</p>
  
  <pre><code>private void PrepareAndPrintLabel()
  {
      . . .
      string barcode = textBoxUPC.Text.Trim();
      if (String.IsNullOrEmpty(barcode))
      {
          MessageBox.Show("Cannot print with no barcode provided");
          return;
      }
      PrintUtils.IBeltPrinterFactory factory = new 
  PrintUtils.BeltPrinterFactory();
      PrintUtils.IBeltPrinter printer = factory.NewBeltPrinter();
      printer.PrintLabel(listPrice, description, barcode);
  }
  
      public void PrintLabel(string price, string description, string
  barcode)
      {
          MessageBox.Show(String.Format("Barcode in
  PrintUtils.ZebraQLn220Printer.PrintLabel is {0}", barcode));
          try
          {
              ArrayList elementsToPrint = new ArrayList();
  
              // Create one etp for each line to be printed on the label
              . . .
              ElementToPrint etp3 = new ElementToPrint
                                        {
                                            DisplayVal = barcode,
                                            elementAlignment =
  ElementToPrintAlignment.Center, elementType =
  elementToPrintType.BarcodeText, RelativeFontSize =
  FontSizeType.BarcodeTextMedium, XPos = 0
                                        };
              elementsToPrint.Add(etp3);
              ElementToPrint etp4 = new ElementToPrint
                                        {
                                            DisplayVal = barcode,
                                            elementAlignment =
  ElementToPrintAlignment.Center, elementType = ElementToPrintType.Barcode,
  RelativeFontSize = FontSizeType.Large, XPos = 0
                                        };
              elementsToPrint.Add(etp4);
              ArrayList linesToSend =
  ConvertElementsForZebraQLn220(elementsToPrint, 1.25);
  
              using (SerialPort serialPort = new SerialPort())
              {
                  serialPort.BaudRate = 19200;
                  serialPort.Handshake = Handshake.XOnXOff;
                  if (!(serialPort.IsOpen))
                  {
                      serialPort.Open();
                  }
  
                  Thread.Sleep(500);
                  foreach (string line in linesToSend)
                  {
                      serialPort.Write(line);
                  }
              }
          }
          catch (Exception ex)
          {
              String msgInnerExAndStackTrace = String.Format("{0}; Inner
  Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace)
  ExceptionLoggingService.Instance.WriteLog(String.Format("From
  ZebraQLn220Printer,PrintUtils.PrintLabel: {0}", msgInnerExAndStackTrace));
          }
      } // PrintLabel
  
      public static ArrayList ConvertElementsForZebraQLn220(ArrayList
  elementsToPrint, double labelHeight)
      {
          const int BARCODE_TEXT_DELTA = 5;
          const int BARCODE_HEIGHT = 50;
          ArrayList linesToSend = new ArrayList();
          int _labelHeight = ConvertInchesToZebraQLn220DPI(labelHeight);
          int YPos = 0;
          ElementToPrintAlignment lastAlignmentSet =
  ElementToPrintAlignment.Left;
  
          // Always start with this line for label mode (as opposed to
  line mode) CPCL:
          linesToSend.Add(string.Format("! 0 200 200 {0} 1\r\n",
  _labelHeight)); //at 1.25, labelHeight is 254; more accurately, it would b
  241, as the labels are really 1.1875" in height, not 1.25
  
          foreach (ElementToPrint etp in elementsToPrint)
          {
              // POSITION THE TEXT
              . . .
              // SIZE THE TEXT - These are based on the device-specific
  parameters 
              if (etp.elementType.Equals(ElementToPrintType.BarcodeText))
              {
                  linesToSend.Add(string.Format("BARCODE-TEXT {0} {1}
  {2}\r\n", fontNum, fontSizeId, BARCODE_TEXT_DELTA));
                  YPos = YPos + GetHeightForFontNumAndSizeID(fontNum,
  fontSizeId) + BARCODE_TEXT_DELTA;
              }
              else if (etp.elementType.Equals(ElementToPrintType.Barcode))
              {
                  string displayValTrimmed = etp.DisplayVal.Trim();
                  string barcodeType = GetBarcodeType(displayValTrimmed);
                  string checkSum = GetBarcodeChecksum(displayValTrimmed);
                  string barcodeWithCheckSum = string.Format("{0}{1}",
  displayValTrimmed, checkSum);
                  MessageBox.Show(string.Format("barcodeWithCheckSum is
  {0}", barcodeWithCheckSum));
                  linesToSend.Add(string.Format("BARCODE {0} 1 1 {1} 0 {2}
  {3}\r\n", barcodeType, BARCODE_HEIGHT, YPos, barcodeWithCheckSum));
                  YPos = YPos + BARCODE_HEIGHT;
              }
              else if (etp.elementType.Equals(ElementToPrintType.Text))
              {
                  linesToSend.Add(string.Format("TEXT {0} {1} 0 {2}
  {3}\r\n", fontNum, fontSizeId, YPos, etp.DisplayVal));
                  YPos = YPos + GetHeightForFontNumAndSizeID(fontNum,
  fontSizeId);
              }
          }
  </code></pre>
  
  <p>This is what the log file shows (irrelevent entries elided):</p>
  
  <p>. . .</p>
  
  <pre><code>Date: 2/3/2015 2:31:26 PM
  Message: Reached frmVerify.InitReader
  
  Date: 2/3/2015 2:31:26 PM
  Message: Reached frmVerify.StartRead
  
  Date: 2/3/2015 2:31:29 PM
  Message: Reached frmVerify.BarcodeReader_ReadNotify
  
  Date: 2/3/2015 2:31:29 PM
  Message: Reached frmVerify.HandleData
  
  Date: 2/3/2015 2:31:29 PM
  Message: Reached frmVerify.textBoxUPC_LostFocus
  
  . . .
  
  Date: 2/3/2015 2:31:43 PM
  Message: Reached frmVerify.buttonPrintLabel_Click
  
  Date: 2/3/2015 2:31:43 PM
  Message: Reached frmVerify.PrepareAndPrintLabel
  
  Date: 2/3/2015 2:31:43 PM
  Message: Reached PrintUtils.PrintLabel
  
  Date: 2/3/2015 2:31:43 PM
  Message: Reached PrintUtils.ConvertElementsForZebraQLn220
  
  . . .
  
  Date: 2/3/2015 2:31:43 PM
  Message: Reached PrintUtils.GetBarcodeType
  
  Date: 2/3/2015 2:31:43 PM
  Message: Reached PrintUtils.GetBarcodeChecksum
  </code></pre>
  
  <p>BTW, "<em>57056548</em>" does not exist anywhere in the code (I thought maybe I had used it as a temporary test value and forgotten to remove it). It seems virtually impossible that this could be happening, yet it is.</p>
  
  <h2>UPDATE</h2>
  
  <p>Following user3025177's suggestion, I added a debug msg to my printing code:</p>
  
  <pre><code>foreach (string line in linesToSend)
  {
      MessageBox.Show(String.Format("About to be sent to th
  serial port: {0}", line));
      serialPort.Write(line);
  }
  </code></pre>
  
  <p>Here is what I saw:</p>
  
  <pre><code>! 0 200 200 241 1
  RIGHT
  TEXT 4 3 0 0 24.77
  LEFT
  TEXT 5 0 0 90 No description found
  CENTER
  BARCODE-TEXT 0 2 5
  BARCODE 128 1 1 50 0 137 761455132
  FORM
  PRINT
  </code></pre>
  
  <p>And what is printed on the label is:</p>
  
  <pre><code>0.00 [right-aligned]
  ITEM NOT FOUND [left-aligned]
  [the barcode zebra stripes, centered]
  57056548 [centered]
  </code></pre>
  
  <p>So the correct barcode ("761455132") is being sent, but "57056548" is being printed on the label.</p>
  
  <p>Bizarro!</p>
  
  <h2>UPDATE 2</h2>
  
  <p>Sour Mash Archie's comment is intriguing, though - that would make sense, as:
  (a) The printer I am using for testing is rusty and cobweb-infested
  (b) The "bogus/rogue" barcode being printed <em>has</em> been printed on that device in the past - so it must be "stuck in memory" or so...</p>