0

I want to print *.bmp to a receipt printer. for that I am reading pixel values (hex) inside a loop and then I need to append the read values with the printer command to print then write to the printer. printer api accepts only unsigned char. now I got the pixel values writing to a temp file. How to construct a unsigned char array with printer commands.

unsigned char printcommand [] = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67 };

int len=sizeof(printcommand)/sizeof(unsigned char);
printf("Length Of Array=%d\n", len);

result = C56_api_printer_clear (iPrinter);


/*----------READ RASTER DATA----------*/
for(r=0; r<=originalImage.rows - 1; r++) {
    for(c=0; c<=originalImage.cols - 1; c++) {
        /*-----read data and print in (row,column) form----*/
        fread(pChar, sizeof(char), 1, bmpInput);
                // here instead of writing to file i need to append to printercommand[]
        fprintf(rasterOutput, "0x%x,", *pChar);
    }
        // here i need to write to the printer as C56_api_printer_write(iPrinter, printcommand, sizeof(printcommand), 1000);
    fprintf(rasterOutput, "\n");
}

// close the printer

Any idea How to do this in C?

Thanks in advance for all your help.

3 Answers 3

1

What you should do is find the width and height of the bitmap and then malloc a width*height block of memory, then just set each index to the proper value.

What format does the printer take for printing? Letters? bit packed chars? Also what type of bitmap are you using? 1 bit? 24 bit?

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Aslai, the char array is not only going to contain pixel values but also it should start with printer specific commands. Cmd + Row 1 => [1B CD 38 08 00 01 02 09 0E ….], Cmd + Row 2 => [1B CD 38 08 02 03 01 0A 0C ….]. and the bitmap size I am using is 256x256.
Are all the commands uniform in length? And is there anything other than row advancement that you need?
yes buddy, first four byte [1B CD 38 08] is command. need to append a row data with this command before writing to the printer buffer. thats all.
Well then all you should need is a buffer that's (width+4)*height bytes large. Could you address some more of my questions? (What format does the printer take for printing? Letters? bit packed chars? Also what type of bitmap are you using? 1 bit? 24 bit?)
sure buddy! it can accept letters and I am trying with 24bit bit map.
0

The declaration

unsigned char printcommand [] = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67 };

creates an array of exactly 7 bytes. You can not append to it as that would write to memory outside the array, and may overwrite other data and/or code.

What you can do is declare the array to be of a larger size, like this:

unsigned char printcommand [128] = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0 };

Now the array is 128 bytes, starting with your seven bytes and the rest is filled with zeroes (the last value in the initialiser list is used for the rest of the array.) To append to this array you need to keep track of the next position to append to, or the size. Something like this:

int printcommand_current_size = 7;

/* ... */

/* Append a new byte to the array */
printcommand[printcommand_current_size++] = *pChar;

The last statement adds a new byte to the array, and increases the size.

You should add a check to make sure you don't try to append beyond the limits of the array.

Comments

0

You could allocate an unsigned char[] buffer of appropriate length. You can read this in a whole row at a time rather than character-by-character. Something like:

unsigned char *oneColumn;
/* oneColumn needs enough room for the print command + data */
oneColumn = (unsigned char *)malloc(originalImage.cols - 1 + len);

/* start off the buffer with the print command */
memcpy(oneColumn, printcommand, len);

/* get one row of data from the file */
fread(oneColumn+len, sizeof(char), originalImage.cols - 1, bmpInput);

Then you can send the buffer data off to the print command.

(This is based on assumptions from your sample code -- one byte per "pixel")

8 Comments

Hi @gregheo, Thanks for the idea. after fread, oneColumn will have the printcommand+one_row_data. am I right buddy?
That's right. The memcpy line will start off the first len number of bytes; then the fread call will fill in the following originalImage.cols - 1 number of bytes.
getting an warning warning: incompatible implicit declaration of built-in function ‘memcpy’ :(
I think you need the include file for memcpy. I'm not sure where it is -- perhaps #include <stdlib.h> or maybe #include <string.h>
#include <string.h> is the solution. if I write the buffer to the file I can see only first value in the printcommand which is copied to all places. how to inspect the buffer data. Thanks buddy.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.