为什么打印出错误的条码?

Why is the wrong barcode being printed?

怎么可能读了一个条码,打印了另一个条码?

我的代码读取扫描的值并将其放入文本框中;例如,诸如“76145513”

之类的值

然而,当我为该条形码打印标签时,它会打印“57056548”

最后一个是有效的条形码,但为什么打印的是 ("57056548") 而不是扫描的值 ("76145513")?

我正在读取一张纸上的条码,上面有许多 "outsized" 个条码。我正在使用的 sheet 包含正在扫描的值和正在打印的值,但我什至在扫描时甚至掩盖了流氓条形码以确保激光束不是墙眼和 "seeing" 打印值,尽管其 "cyclops beam" 确实只点亮了正确的条形码。

我输入了一些 debug/sanity 检查代码以确保发送打印的内容是正确的,而且是正确的;我看到,第一个 MessageBox.Show() 中的“PrintUtils 中的条码。ZebraQLn220Printer.PrintLabel 是 76145513” 和“ 带有校验和的条码是 761455132" 与第二个。

那么怎么可能扫描了正确的值并将其发送到 Print 方法,却打印了不同的(但有效!)代码?

这里是核心打印代码:

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);
            }
        }

这是日志文件显示的内容(忽略了不相关的条目):

。 . .

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

顺便说一句,代码中的任何地方都不存在“57056548”(我想也许我将它用作临时测试值而忘记将其删除)。这似乎几乎不可能发生,但它确实发生了。

更新

根据 user3025177 的建议,我在打印代码中添加了调试消息:

foreach (string line in linesToSend)
{
    MessageBox.Show(String.Format("About to be sent to th
serial port: {0}", line));
    serialPort.Write(line);
}

这是我看到的:

! 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

而标签上印的是:

0.00 [right-aligned]
ITEM NOT FOUND [left-aligned]
[the barcode zebra stripes, centered]
57056548 [centered]

所以发送的是正确的条形码(“761455132”),但标签上打印的是“57056548”。

奇怪!

更新 2

Sour Mash Archie 的评论很有趣,不过 - 这是有道理的,因为: (a) 我用于测试的打印机生锈且布满蜘蛛网 (b) 正在打印的 "bogus/rogue" 条形码 过去曾在该设备上打印过 - 所以它必须是 "stuck in memory" 左右...

希望不大,但我记得几年前 Zebra 打印机的一个问题,它们会卡在条形码上,并且每次打印时总是打印相同的条形码。这只有在您使用 EPL/ZPL 代码打印时才会发生,如果您使用 Windows 图形命令则不会,但如果您关闭打印机然后再次打开,或者在另一台机器上尝试,它会持续存在。

我们通过在打印机上进行硬件重置来修复它;在我们的模型上,您必须打开外壳并按下电路板上的按钮。

在 Zebra 网站上查看适用于您的打印机型号的固件更新。