Technical FAQs

Ask a Question

How can I use Script Driver to Send Variable Data to a Printer?

Here is a sample script to send variable data to a printer connected to serial port and using script driver:

// 1. Printer control characters

byte LineFeed = (byte) 0x0a;        // Line Feed

byte CarriageReturn = (byte) 0x0d;  // Carriage Return

byte FormFeed = (byte) 0x0c;        // Form Feed

 

// 2. Printer buffer and other variables

byte PrintBuffer[] = new byte[1024];

char[] sendToPrinterTMP; // temporary char array

byte sendToPrinter[];    // byte array for each variable

int numOfChar = 0,       // string length

i, j, k, BufferPosition = 0;

 

// 3. Print variable values

for (i=0; i<10; i++)

{

// a. Create a String with the variable name and value

String LocalMessage = "PrintValues" + "[" + i + "]";

LocalMessage.concat(" = " + PrintValues[i].getStringValue());

// b. Convert from String to character array

numOfChar = LocalMessage.length();

sendToPrinter = new byte[numOfChar]; // define size of byte array

sendToPrinterTMP = LocalMessage.toCharArray();

// c. Convert unicode character array to ASCII byte array

for (j=0; j<numOfChar; j++)

{

sendToPrinter[j] = (byte)sendToPrinterTMP[j];

}

// d. Move characters to print buffer array

for (k=0; k<j; k++)

{

PrintBuffer[BufferPosition++] = sendToPrinter[k];

}

// e. Change to a new line

PrintBuffer[BufferPosition++] = LineFeed;

PrintBuffer[BufferPosition++] = CarriageReturn;

}

// 4. Change to a new page

PrintBuffer[BufferPosition++] = FormFeed;

// 5. Send the buffer to the script driver

Print_Variables.write(true, BufferPosition+1, PrintBuffer);

Was this helpful?
What can we do to improve the information ?