如果writePort超时,Star打印机会打印垃圾

时间:2017-06-12 21:45:53

标签: java linux centos serial-port stario-sdk

我使用Star Micronics打印机和StarIO Java SDK打印一些收据。有时,当我以光栅模式发送位图数据时,StarIOPort.writePort()方法会超时。但是,这种方法不会抛出异常,甚至会返回写入大小> 0.在这种情况下,打印机会打印一行随机像素或大量空行/垃圾文本。

我想我可以检测到超时,但我不知道如何从中恢复。据我所知,一旦writePort()返回,打印机就已经打印了垃圾数据。这些超时非常频繁,每张收据发生1-2次。我可以调整波特率并可能启用奇偶校验,但我认为这不会完全消除这个问题。我可以防止这些超时或至少处理它们而无需重新打印吗?我在这里缺少什么?

打印机设置:

<JposEntry logicalName="TSP743II_linux_serial">
    <creation factoryClass="com.starmicronics.starjavapos.ServiceInstanceFactory" serviceClass="com.starmicronics.starjavapos.POSPrinterService" />
    <vendor name="Star Micronics" url="www.star-m.jp/eng/index.htm" />
    <jpos category="POSPrinter" version="1.9" />
    <product description="Star receipt printer" name="TSP743II" url="www.star-m.jp/eng/index.htm" />
    <prop name="model" type="String" value="TSP743II" />
    <prop name="portName" type="String" value="/dev/ttyS1" />
    <prop name="portSettings" type="String" value="19200,n,8,1,h" />
    <prop name="ioTimeoutMillis" type="Integer" value="10000" />
    <prop name="doCheckedBlockPrinting" type="Boolean" value="True" />
    <prop name="memorySwitchSetting" type="String" value=":1,0100:4,0002" />
</JposEntry>

创建端口:

final String portName = "/dev/ttyS1";
final String portSettings = "19200,n,8,1,h";
final int timeout = 10000;
try {
    port = StarIOPort.getPort(portName, portSettings, timeout);
} catch (StarIOPortException exception) {
    exception.printStackTrace();
}

端口写入逻辑:

byte[] command = ...;
int totalSizeCommunicated = 0;
while (totalSizeCommunicated < command.length) {
    // This returns > 0 and doesn't throw an exception on timeout!
    totalSizeCommunicated += port.writePort(command,
        totalSizeCommunicated, command.length - totalSizeCommunicated);
}

&#34;命令&#34;数组采用以下形式:

final String commandString =
    // Enter raster mode
    "\u001b*rA"
    // Top margin command 2
    + "\u001b*rT2\0"
    // Page length command 0
    + "\u001b*rP0\0"
    // Print quality command 2
    + "\u001b*rQ2\0"
    // Left margin command 7
    + "\u001b*rml7\0"
    // Set end of page mode command 1
    + "\u001b*rF1\0"
    // Set end of doc mode command 1
    + "\u001b*rE1\0";
    // Raster image data by line (not actual code but demonstrative)
    + "b" + sizeOne + size256One + pixelsOne;
    + "b" + sizeTwo + size256Two + pixelsTwo;
    + "b" + sizeThree + size256Three + pixelsThree;
    // Exit raster mode, clear raster buffer
    + "\u001b*rB\u001b*rC".getBytes();

释放端口:

StarIOPort.releasePort(port);

更新:启用奇偶校验检查没有帮助。完全没有。事实上,它可能会使情况变得更糟。

更新2:将波特率减半至9600也会使问题变得更糟。

0 个答案:

没有答案