怎么可能是一个条码被读取,另一个条码被打印?
我的代码读取扫描的值并将其放入文本框中;例如,诸如“76145513”之类的值
然而,当我为该条形码打印标签时,它会打印“57056548”
最后一个是有效的条形码,但为什么打印的是 ("57056548") 而不是扫描的值 ("76145513")?
我正在从一张纸上读取条形码,上面有许多“超大”条形码。我正在使用的纸张包含正在扫描的值和正在打印的值,但我什至在扫描时掩盖了流氓条形码,以确保激光束不是墙眼和“看到”打印的值,尽管它的“独眼巨人光束”确实只点亮了正确的条形码。
我放入了一些调试/健全性检查代码,以确保发送到打印的内容是正确的,而且确实如此;我看到,第一个 MessageBox.Show() 中的“PrintUtils.ZebraQLn220Printer.PrintLabel 中的Barcode 是 76145513 ”,第二个 MessageBox.Show() 中的“ barcode with checksum is 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)正在打印的“伪造/流氓”条形码已打印在该设备上过去 - 所以它必须“陷入记忆”左右......