Interfacing a thermal printer with PIC

Interfacing a thermal printer with PIC

Postby srlvrsn » Tue Sep 08, 2020 1:46 pm

Hello,

I an amateur in PIC, I am trying to print something using PIC16F18875 and Digilent Pmod RS232 to communicate with the printer(https://www.partner.com.tw/portfolio-it ... pt-printer) in MPLAB X IDE v5.40. I powered Pmod with Voltage source(3.3V) and GND and RX pin. I am a bit aware of using EUSART (RC6 - TX) but I do not know how to print some text to the printer,

should I use any library specific to the printer?
or something like putchar() would do?
or if anyone who has done it, could you please direct me in the right direction?

Thanks much for your time.
srlvrsn
 
Posts: 7
Joined: Tue Sep 08, 2020 12:57 pm

Re: Interfacing a thermal printer with PIC

Postby AussieSusan » Wed Sep 09, 2020 3:59 am

Using the link you provided, it mentions "ESC/POS and STAR command sets compatible". A Google search of "ESC/POS commands" leads to many pages, the first of which is (for me anyway) https://reference.epson-biz.com/modules ... ntent_id=2. From there, links to the command set (i.e. the various non-printing character sequences that control how the printer responds to any following characters) and also "Programming Examples" will show you how to send the commands and characters.
The all seem to be standard ASCII and so the UART interface should have no problems handling those.
As for how to send characters through the UART, there are many examples of how to do that both here and in the Microchip forums.
Susan
AussieSusan
Verified identity
 
Posts: 133
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Interfacing a thermal printer with PIC

Postby srlvrsn » Wed Sep 09, 2020 4:47 pm

Firstly, Thank you Susan for your response.

Do you mean that I should first start with the POS commands and then the text to be printed?
something like this :
Code: Select all
while (1)
    {
        EUSART_Write("ESC @"); // ESC @ = Initialize printer
        EUSART_Write("Hello!\r\n");
    }


Because the baudRate, polarity, number of bits are all already set in EUSART. I tried the above snippet nothing changed.
Am I missing something, here?
Ela
srlvrsn
 
Posts: 7
Joined: Tue Sep 08, 2020 12:57 pm

Re: Interfacing a thermal printer with PIC

Postby AussieSusan » Thu Sep 10, 2020 4:22 am

That really depends on what is hidden behind the "EUSART_Write" function.
Is it blocking - if not then you may be overflowing the Tx buffer.
Are you getting a signal out of the Tx pin? (You probably need an oscilloscope to test this - or hook up a serial-USB interface and use a PC to receive the characters using a terminal program).
One this is for certain, the character string you are sending is wrong. "ESC" refers to the escape character which is typically \033. Look at any ASCII table to se all of the prefix codes that are listed (e.g. LF is normally '\n' or '\0x0a' or '\012' depending on what number base you are comfortable with in your coding).
Also there is no space between the escape character and the ampersand - it is written that way in the tables for clarity only. Therefore that first line to the printer is probably:
Code: Select all
EUSART_Write("\033@");

Susan
AussieSusan
Verified identity
 
Posts: 133
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Interfacing a thermal printer with PIC

Postby srlvrsn » Thu Sep 10, 2020 4:49 pm

Sorry. I should have mentioned this before. I used MCC.

Code: Select all
void EUSART_Write(unsigned char txData)
{
    while(0 == PIR3bits.TXIF)
    {
    }
    TX1REG = txData;    // Write the data byte to the USART.
}


Earlier it had "uint8_t" now i changed it to "unsigned char". In the oscilloscope I could see a signal now (I just executed with hello) Not sure if it is a "HELLO" in the Oscilloscope. But when i connect it to the PC hyperTerminal windows isn't connecting to the port properly(All the port properties match (terminal = device Manager port properties = code)). All have BaudRate = 115200, T and R bits = 8, Non-Inverted data polarity. I don't know what is wrong.

I think there is something wrong with my SPBRG value. I am using 8Mhz oscillator with the baudrate of 115200. MCC generated this
Code: Select all
void EUSART_Initialize(void)
{
    // Set the EUSART module to the options selected in the user interface.
    // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
    BAUD1CON = 0x08;
    // SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled;
    RC1STA = 0x90;
    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
    TX1STA = 0x24;
    // SP1BRGL 3;
    SP1BRGL = 0x03;
    // SP1BRGH 0;
    SP1BRGH = 0x00;
    EUSART_SetFramingErrorHandler(EUSART_DefaultFramingErrorHandler);
    EUSART_SetOverrunErrorHandler(EUSART_DefaultOverrunErrorHandler);
    EUSART_SetErrorHandler(EUSART_DefaultErrorHandler);
    eusartRxLastError.status = 0;
}


I may have made a basic mistake. Couldn't figure out what. Do you see anything out of the ordinary?

Ela
srlvrsn
 
Posts: 7
Joined: Tue Sep 08, 2020 12:57 pm

Re: Interfacing a thermal printer with PIC

Postby AussieSusan » Fri Sep 11, 2020 3:41 am

Use the scope to check the Baud rate. Just send (say) 0x55 rather than a string of varying characters so that you can see the start and stop bits and also measure the time for each bit.
If MCC generated the oscillator configuration as well then I guess we have to assume that the settings are correct. Personally, for 115200 Baud, I would probably NOT use BRGH as high speed.
Susan
AussieSusan
Verified identity
 
Posts: 133
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Interfacing a thermal printer with PIC

Postby srlvrsn » Tue Sep 15, 2020 2:33 pm

Ha! I tried what you said. Almost everything fell into the right place! Almost! I could see what I send. In the terminal, first I could see a constant [01] no matter what char i send. Then I re-did from the first and it worked. Initially I was using 1MHz with 115200 baud. The error percent was around 8%. Then I shifted to 16MHz, resulted in less than 1% of error. I don't know if that was the issue. Atleast EUSART is working fine now.

I still couldn't understand how to control the printer. You mean I should pass these commands first before sending the text that's to be printed? Something like this?
Code: Select all
void main(void)
{
    SYSTEM_Initialize();
    INTERRUPT_GlobalInterruptEnable();
    INTERRUPT_PeripheralInterruptEnable();
    unsigned char rcd;
    while (1)
    {
        RB0_LED_SetHigh();
        while(eusart1RxCount) // to check the incoming data
        {
            RB7_LED_SetHigh();
            rcd = getch();
            EUSART1_Write('0x1B 0x4A 0x20');
            EUSART1_Write(rcd);
        }
        __delay_ms(2);       
    }
}


[Name] Print and feed paper
[Format] ASCII ESC J n
Hex 1B 4A n
Decimal 27 74 n
Range ] 0 <n <255
Description ] Prints the data in the print buffer and feeds the paper [ n X vertical or horizontal motion unit]
inches.

Got from the Program manual of the printer. It was just explaining the commands

Could you please share any examples if you have or that you found? It will be of great help.

Ela
srlvrsn
 
Posts: 7
Joined: Tue Sep 08, 2020 12:57 pm

Re: Interfacing a thermal printer with PIC

Postby AussieSusan » Wed Sep 16, 2020 3:45 am

UARTs need to be within +/-2% typically to work so 8% is almost certain not to be understood properly by the printer.
You also need to learn some basic C programming. Character strings are surrounded by double-quotes (") not single quotes (') which are used for individual characters (see the way I used them in my post of the 10th of September above). Also when you convert that to an actual string, don't put in the spaces between the characters - typically these escape sequences MUST have consecutive characters so the spaces will break the sequence and stop it from being interpreted correctly.
By the way, what is the vertical motion unit for the paper - and do you really mean to raise the paper by 32 units?
Are you sure you are getting a known character in the 'rcd' variable?
Also I'd probably swap the two 'write' statements around so that you send the character and THEN move the paper. Looking at the description of the 'Print and feed paper" command you have shown, it implies that there is a print buffer in the printer which *might* mean that it will not immediately print a 'printable' character when it is received.
Susan
AussieSusan
Verified identity
 
Posts: 133
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Interfacing a thermal printer with PIC

Postby srlvrsn » Sat Sep 19, 2020 12:42 pm

I actually tried the double quotes already. I thought I had the problem with Pmod or RS232 cable which is working fine I believe. I just want to get any response from the printer.

I tried the below,
Code: Select all
EUSART1_Write("\29864865");//Decimal for select mode and cut (GS V m)

Code: Select all
EUSART1_Write("\0X1D0X560X48");//HEX


Both the code, commands the printer to cut the paper.

No response from the printer. I have connected oscilloscope also and whatever I pass is reflecting in the terminal. Maybe I shouldn't ignore the CTS and RTS? According to this (https://reference.digilentinc.com/refer ... nce-manual). I am using 3 wire connection. The printer isn't responding for anything. Any advises on that would be a temporary relief.
srlvrsn
 
Posts: 7
Joined: Tue Sep 08, 2020 12:57 pm

Re: Interfacing a thermal printer with PIC

Postby AussieSusan » Mon Sep 21, 2020 4:09 am

Sorry but this is down to some basic C programming.
What those code sequences should be are:
Code: Select all
EUSART1_Write("\0x1d\0x56\0x48");

or better still
Code: Select all
EUSART1_Write("\0x1dVH");

Actually, looking at the web page I referred to above (not your link - I couldn't find the command set linked to from that page) ESC V is a complete command that turns the 90 degree printing on and off, and there should not be any further characters after that. However "ESC m" (by the way 0x43 is 'H' not 'm' - there does not seem to be an'ESC H' command) is an obsolete command for a partial cut of the paper.
Therefore if you really do mean to issue both commands then you need:
Code: Select all
EUSART1_Write("\01dV\01dm");

Susan
AussieSusan
Verified identity
 
Posts: 133
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Next

Return to SCI/USART/EUSART

Who is online

Users browsing this forum: No registered users and 2 guests

cron