.net c#为什么打印了错误的条形码?

Why is the wrong barcode being printed?
2020-11-21
  •  译文(汉语)
  •  原文(英语)

如何读取一个条形码,再打印另一个条形码?

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

但是,当我为该条形码打印标签时,它会打印"57056548"

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

我正在从一张纸上读取条形码,上面有很多"超大"条形码.我正在使用的工作表既包含正在扫描的值,也包含正在打印的值,但我什至甚至在扫描时掩盖了流氓条形码,以确保激光束不会被视线和"看见"打印的值,尽管其"独眼巨人"确实仅点亮正确的条形码.

我放入了一些调试/健全性检查代码,以确保发送给打印的内容正确无误;我在第一个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);
            }
        }

这是日志文件显示的内容(删除了irrelevent条目):

...

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

酸酸醪阿奇的评论是耐人寻味,虽然-这将是有意义的,因为:(1)我使用的用于测试打印机生锈了蛛网出没(二)"假/流氓"正在打印的条形码已经被印在该设备上过去-因此必须将其"保留在内存中"等等

速聊1:
您可能需要在foreach(…){serialPort.Write(line))循环中添加调试,以输出要发送到打印机的内容,然后与打印机编程手册交叉引用调试输出的条形码命令.这可以解释为什么打印的条形码不匹配.
速聊2:
我会尝试的,但是我可以想象该值是完全错误的,但是它变成了一个完全不同的(但有效的)值,这才使我得到了旧的rompecabeza.
速聊3:
:如果您想让评论成为答案,我将其标记为答案.
解决过程1

这是一个长镜头,但我记得几年前Zebra打印机出现问题,它们会卡在条形码上,并且每次打印时始终打印相同的条形码.仅当您使用EPL / ZPL代码进行打印时才会发生这种情况,而如果您使用Windows图形命令则不会发生这种情况,但是如果您先关闭打印机再重新打开,或者在另一台机器上尝试使用它,这种情况将持续存在.

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

在Zebra网站上检查打印机型号的固件更新.

How could it be that one barcode is being read, yet another one is printed?

My code reads the value scanned and puts it in a textbox; e.g., a value such as "76145513"

Yet, when I print a label for that barcode, it prints "57056548"

This last is a valid barcode, but why is that ("57056548") printed instead of the value scanned ("76145513")?

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.

I put some debug/sanity check code in to make sure what was being sent to print was correct, and it is; I see, "Barcode in PrintUtils.ZebraQLn220Printer.PrintLabel is 76145513" in the first MessageBox.Show() and "barcode with checksum is 761455132" with the second one.

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?

Here is the core print 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);
            }
        }

This is what the log file shows (irrelevent entries elided):

. . .

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

BTW, "57056548" 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.

UPDATE

Following user3025177's suggestion, I added a debug msg to my printing code:

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

Here is what I saw:

! 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

And what is printed on the label is:

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

So the correct barcode ("761455132") is being sent, but "57056548" is being printed on the label.

Bizarro!

UPDATE 2

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 has been printed on that device in the past - so it must be "stuck in memory" or so...

Talk1:
You may want to add debug in the foreach(…){serialPort.Write(line)) loop to output what is being sent to the printer and then cross-reference the barcode commands of the debug output with the printer programming manual. This may explain why the printed barcode does not match.
Talk2:
I'll try that, but I can imagine the value being totally wrong, but that it is changed to a completely different (but valid) value is what gives me the old rompecabeza.
Talk3:
: If you want to make your comment an answer, I'll mark it as such.
Solutions1

This is a long shot, but I remember a problem years ago with Zebra printers where they would get stuck on a barcode and always print the same one whenever you printed. This only happened if you used EPL/ZPL codes to print, not if you used Windows graphics commands, but it would persist if you switched the printer off then on again, or tried it on another machine.

We fixed it by doing a hardware reset on the printer; on the model we had you had to open the case and press a button on the circuit board.

Check on the Zebra web site for a firmware update for your printer model.

转载于:https://stackoverflow.com/questions/28309989/why-is-the-wrong-barcode-being-printed

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入