ESC/P Set Absolute Horizontal Print Position

我的未来我决定 提交于 2019-12-01 11:54:21

问题


I am having trouble with setting the horizontal print position in an Epson LX-300 II dot matrix printer. The command to set the horizontal print position does not work sometimes.

I need this to align the printable data to the appropriate column in the pre-printed paper form.

Given the following code:

        // row 1
        escp.setAbsoluteHorizontalPosition(1);
        escp.print("Equipment");
        escp.setAbsoluteHorizontalPosition(10);
        escp.print("Serial");
        escp.setAbsoluteHorizontalPosition(13);
        escp.print("Remarks");

        // row 2
        escp.lineFeed();
        escp.setAbsoluteHorizontalPosition(1);
        escp.print("Equipment");
        escp.setAbsoluteHorizontalPosition(10);
        escp.print("Serial");
        escp.setAbsoluteHorizontalPosition(13);
        escp.print("Remarks");

This is the expected output:

Equipment                  Serial     Remarks
Equipment                  Serial     Remarks

However, this is the actual printed output:

EquipmentSerial                        Remarks
EquipmentSerial                        Remarks

I cannot figure out why "Serial" is not printed on the correct position.

To try to figure this out, I made a test program which prints 'x' 1cm apart in a single line.

Here is a sample code from the test program:

    ESCPrinter escp = new ESCPrinter(sharedPrinterName, false);
    if((escp.initialize()) == false) {
        return;
    }
    escp.select10CPI();
    escp.set8LPI();
    escp.setCharacterSet(ESCPrinter.USA);                
    for(int x = 1; x < 15; x++) {
        escp.setAbsoluteHorizontalPosition(x);
        escp.print("x");
    }
    escp.formFeed();
    escp.close();

Expected output:

x    x    x    x    x    x    x    x    x    x    x    x    x    x

Actual output:

x    x    x    x    xxxxxx                    x    x    x    x

This is the Epson ESC/P Reference Manual, and this is the third-party code I am using to send printer commands, which I have slightly modified to implement the set8LPI() method.

In the reference manual, the command to set the absolute horizontal print position is in C-31.

I have tried to figure this out myself for several days now but I am no closer to finding the solution.

Additional Info

I made another test program that prints test data from positions 0.0cm to 19.8cm, with increments of 0.2cm and line feeds in between each.

The test data I have printed is also the absolute horizontal position I have specified (for easier identification).

It would be impractical to show you the exact printed output, so I will just describe them as best as I can.

Setting the horizontal position to 0.0 until 5.4 seems to work, with the output looking like this:

0.0
 0.2
  0.3
   0.4
   .
   .
   .
   -------->  5.4

After that, setting the absolute horizontal position to 5.6cm until 10.8cm is ignored. The printer just prints on the left-most part of the paper.

The "set absolute horizontal position" command is working again for 11.0cm until 16.2cm. Ignored again for 16.4cm until 19.8cm.

As I understand the ESC/P reference manual I have linked above, the printer ignores this command when the specified position is beyond the right margin. Nothing else is specified.

Obviously, my values are all within the margins.

So, could this be a hardware problem with the printer?

--> Not a hardware problem. I got same results using a different printer.

Update

Given the command to set absolute horizontal position: ESC $ nl nh

I have made another test program where I directly input the nh and nl parameters.

I have realized that this command is ignored whenever nl value exceeds 127.

I find this to be surprising since the reference manual indicates that max value for nl is 255.

This finding is consistent with the first test program where I input the horizontal position in centimeters. When I convert centimeters into the corresponding nh and nl values for the commands that are ignored, the resulting nl values are greater than 127.


回答1:


ESC/P Set Absolute Horizontal Print Position

From the given 3rd Party Java code on ESCPrinter.java, the approach to send ESC/P control code to the printer port is waiting for disaster. For your case, when the value is larger than 7-bit data (127 / 0x7F).

Given the command to set absolute horizontal position:

ESC $ nL nH
nL value: 0 <= nL < 256

When nL value is over 127, the value is converted incorrectly and sent to the printer port. The incorrect conversion is caused by PrintStream() class which will call the default charset encoding based on your system locale (internally create java.io.Writer() class). That's why the value nL is never correctly send to printer port.

To fix this problem, you must never try to use String() class or any other charset encoding related class to write the control code (e.g. .toString(), .toByteArray(charset), Writer).

You can try UTF-8 encoding for PrintStream(), to see if it fixes the bug or not.



来源:https://stackoverflow.com/questions/13852321/esc-p-set-absolute-horizontal-print-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!