Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么要打印错误的条形码?_C#_Printing_Barcode Scanner_Zebra Printers_Barcode Printing - Fatal编程技术网

C# 为什么要打印错误的条形码?

C# 为什么要打印错误的条形码?,c#,printing,barcode-scanner,zebra-printers,barcode-printing,C#,Printing,Barcode Scanner,Zebra Printers,Barcode Printing,怎么可能一个条形码正在读取,而另一个条形码正在打印 我的代码读取扫描的值并将其放入文本框中;e、 例如,一个值,如“76145513” 然而,当我为条形码打印标签时,它会打印“57056548” 最后一个是有效的条形码,但为什么打印的是(“57056548”)而不是扫描的值(“76145513”) 我正在从一张有很多“超大”条形码的纸上读条形码。我正在使用的表单包含正在扫描的值和正在打印的值,但我甚至在扫描时掩盖了流氓条形码,以确保激光束没有“看到”打印的值,尽管它的“cyclops光束”只会点

怎么可能一个条形码正在读取,而另一个条形码正在打印

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

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

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

我正在从一张有很多“超大”条形码的纸上读条形码。我正在使用的表单包含正在扫描的值和正在打印的值,但我甚至在扫描时掩盖了流氓条形码,以确保激光束没有“看到”打印的值,尽管它的“cyclops光束”只会点亮正确的条形码

我放入了一些调试/健全检查代码,以确保发送到打印的内容是正确的,并且是正确的;我看到第一个MessageBox.Show()中的“PrintUtils.ZebraQLn220Printer.PrintLabel中的条形码是76145513”,第二个MessageBox.Show()中的“带校验和的条形码是761455132”

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

以下是核心打印代码:

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);
            }
        }
这是日志文件显示的内容(不相关的条目被省略):

顺便说一句,“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) 正在打印的“伪造/流氓”条形码过去已经在该设备上打印过了,所以它一定是“卡在内存中”或是…

这是一个长期的问题,但我记得几年前斑马打印机出现了一个问题,它们会卡在条形码上,并且每次打印时都会打印相同的条形码。只有使用EPL/ZPL代码打印时才会发生这种情况,而不是使用Windows图形命令,但如果关闭打印机然后再次打开,或者在另一台机器上尝试,这种情况会持续存在

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


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

您可能需要在foreach(…){serialPort.Write(行)中添加调试)循环以输出发送到打印机的内容,然后将调试输出的条形码命令与打印机编程手册交叉引用。这可能解释了打印的条形码不匹配的原因。我会尝试一下,但我可以想象该值完全错误,但改为完全不同的(但有效)“价值是我得到旧rompecabeza的原因。@DourHighArch:如果你想让你的评论成为一个答案,我会把它标记为答案。”。
0.00 [right-aligned]
ITEM NOT FOUND [left-aligned]
[the barcode zebra stripes, centered]
57056548 [centered]