Trouble with understanding ESC/P codes

I recently started a project to do attempt to print on a dot matrix printer with a Arduino. I found this forums post which laid out some code on control over a LPT cable. I hooked everything up and the example worked perfectly.

However I need to print some small graphics too and need some formatting, so I looked around and found and read a little bit about ESC/P2 standard which was supported by my Epson FX-890. And so I tried to modify the code provided there to at first just try and define some margins, font sizes and formatting before I even tried to tackle graphics... needless to say I, for the most part, failed.

So my questions would be:

  1. How do I properly define the page dimensions ? I have A4 continuous paper 8.2 inch in width and 12 inch in height.

  2. How do I define margins so it does not print over the perforations and the holes on the side

  3. I am not quite sure how the graphics printing works, could it be possible to print a graphic inline to text, and if yes - how could I do that

I can't remember a thing from dot-for-dot graphics on printers, but I do remember sending a lot of escape sequences...

from this document...

Setting the page format
ESC ( C Set page length in defined unit • — — C-10
ESC ( c Set page format • — — C-11
ESC C Set page length in lines • • • C-13
ESC C NUL Set page length in inches • • • C-15
ESC N Set bottom margin • • • C-17
ESC O Cancel bottom margin • • • C-19
ESC Q Set right margin • • • C-21
ESC l Set left margin • • • C-23

I will take a SWAG and suggest getting (download) an Epson printer manual from the late 90's through maybe 2010.

Thank you, ill try to implement this over the weekend!

That would certantly help, I even tried finding one, however this is the only one I could find.. while it covers certain things like page length, it doesn't margins and graphics..

And the esc/p2 reference doesn't provide many examples ether, so I am a bit lost on that too..

Hello Avionic_20

Take a view here:

hth

Hi,

I don't quite fully understand how to calculate the High and Low values for a given property with the INT and MOD conventions as described in page 7.

I do not understand how the byte splitting happens.. nor can I find any resources on the internet explaining it..

Can anyone please provide some clarity/resources where or how tho calculate such values.

MOD is just another counting system. We count in base 10 (mod 10). Hex is MOD 16.

INTs that are over 256 (bits?) will use MOD 256... so 257 is 11(mod 256... 1 x 256 + 1 = 257). They are showing 520 is 2 (mod 256) (remainder 8)

That is where the HIGH and LOW byte come in... probably "2" is HIGH and "8" is low in the 520 example.... or the 257 example, where HIGH is 1 and LOW is 1.

As the manual explains, when you use int(520/256) the result is the integer part of the result of the calculation so int(520/256) equal 2

mod(520/256) gives the remainder of the calculation, ie 8

256 goes into 520 2 times (the int value) with a remainder of 8 (the mod value)

The int and mod values are both guaranteed to be less than 256 so will each fit in a single byte

In C++ you would do the calculations like this

byte hiByte = 520/256;
byte loByte = 520%256;

The two values can then be used as parameters for the ESC sequences that require values greater than 256

Hello @UKHeliBob - What is happens to the remainder? Is it simply truncated?

If you mean the remainder of 520/256 then it does not matter

Starting with 520/256 we get a high byte of 2 and a low byte of 8

The software that interprets the ESC code on the printer will, therefore, receive 2 and 8 which it interprets as (2 * 256) + 8

That is 512 + 8, ie 520, which is the value that needed to be sent but could not be sent as a single byte

There is an even easier way to obtain the values using the Arduino as it has built in highByte() and lowByte() functions

void setup()
{
    Serial.begin(115200);
    int x = 520;
    Serial.println(highByte(x));
    Serial.println(lowByte(x));
}

void loop()
{
}

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.