Sunday, June 21, 2015

Embedded Thermal Printer on PLC (Programmable Logic Controller)




Embedded Thermal Printer on PLC used for Barcode Printing or other applications.
In this case, Embedded Thermal Printer used for barcode printing with print command from PLC.
Thermal Printer Applications in industrial such as print for next order Kanban System (JIT), printing machine condition information, print machine alarm history, print the number of products on the PLC counter, and others.



Embedded Thermal Printer on PLC Programmable Logic Controller

Hardware required for Embedded Thermal Printer on PLC:
1. Embedded Thermal Printer with TTL Serial Port Interface
2. Arduino Atmega 2560
3. Power Supply with DC Output: 5V 10A
4. PLC Cable : RS232 PPI Multi Master Cable
5. Serial Port Module: TTL to RS232 male
6. Siemens S7-200 PLC
7. 2 pieces Push Switch

Hardware for Embedded Thermal Printer on PLC


Hardware Connections for Embedded Thermal Printer on PLC:
1. Arduino to Embedded Thermal Printer:
1a. Arduino Pin 18 (TX1) to RX Thermal Printer
1b. Arduino Pin 19 (RX1) to TX Thermal Printer
2. Arduino to Serial Port Module:
2a. Arduino Pin 16 (TX2) to TX Serial Port Module
2b. Arduino Pin 17 (RX2) to RX Serial Port Module
3b. Arduino Pin 3.3V to VCC Serial Port Module
3. Power Supply 5V:
3a. Power Supply COMM(-V) to GND Arduino
3b. Power Supply COMM(-V) to GND Serial Port Module
3c. Power Supply COMM(-V) to GND Thermal Printer
3d. Power Supply (+V) to VH Thermal Printer
3e. Power Supply (+V) to Vin Arduino


Hardware Connections for Embedded Thermal Printer on PLC


Download Project File for Embedded Thermal Printer on PLC:
1. Arduino libraries, click here
Copy-Paste folder: SimpleModbusMaster into folder C:\Program Files (x86)\Arduino\libraries
2. Arduino Project File, click here
3. PLC Ladder Programming, click here
4. Tutorial about ESC Command for Embedded Thermal Printer, click here

Barcode Text and Type Setting in Arduino:
Set barcode and number of barcode for print barcode from PLC.

Barcode Text and Type Setting in Arduino


Print Command and Number of Barcode in PLC Ladder Programming:
Number of barcode for print use Increment data by PLC Input I0.1

Print Command and Number of Barcode in PLC Ladder Programming


Arduino Code for Embedded Thermal Printer on PLC:
#include <SimpleModbusMaster.h>
//Arduino Serial Port Connect to Port 0 of Siemens PLC S7 200
//for more info program-plc.blogspot.com
#define slaveAddr 1
#define baud 9600
#define timeout 1000
#define polling 200
#define retry_count 0
#define TxEnablePin 2 
enum
{
  PACKET1,
  PACKET2,
  TOTAL_NO_OF_PACKETS
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
packetPointer packet2 = &packets[PACKET2];
unsigned int readRegs[2];
unsigned int writeRegs[3];
unsigned int OrderInc=0;
bool CommandExec=true;

// Barcode types
#define UPC_A   0 //Barcode("12345678910", UPC_A);OK
#define EAN13   2 // Barcode("123456789101", EAN13);OK
#define EAN8    3 // Barcode("12345678", EAN8);NOT
#define CODE39  4 //Barcode("CODE", CODE39);OK
#define ITF     5 // Barcode("123456789012", ITF);OK
#define CODEBAR 6 // Barcode("12345678", CODEBAR);NOT
#define CODE93  7 //Barcode("CODE93", CODE93);OK
#define CODE128 8 //Barcode("Code128", CODE128); OK
#define CODE11  9 //Barcode("0123452", CODE11);OK
#define MSI    10 //Barcode("123456782", MSI);OK


#define printDensity   14 
#define printBreakTime  4 

byte heatTime=200;
unsigned long NextWriteBytes=0;
unsigned long  dotPrintTime = 30000; 
byte barcodeHeight = 50;

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  Printer_Init();
  
  modbus_construct(packet1, slaveAddr, READ_HOLDING_REGISTERS, 0, 2, readRegs);
  modbus_construct(packet2, slaveAddr, PRESET_MULTIPLE_REGISTERS, 2, 3, writeRegs);
  modbus_configure(&Serial2, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);  
 
}

void loop() {
  modbus_update();
  word Print_Command = (word)readRegs[0];
  word Barcode_Num = (word)readRegs[1];
  
  OrderInc++;
  if(OrderInc>32760)OrderInc=1;
  writeRegs[0]= (word)OrderInc;
  writeRegs[1]= (word)Printer_Status();
  if((writeRegs[1]&4)==4)CommandExec=false;//Out of Paper

  if(CommandExec && Barcode_Num>0 && Print_Command>0){     
      switch(Barcode_Num)
      {
      case 1:
        Printer_Feed(1);
        break;
      case 2:
         Barcode("KANBAN 99", CODE93);   
        break;       
      case 3:
         Barcode("KANBAN 3", CODE93);   
        break;
        
      case 4:
         Barcode("RUN", CODE39);   
        break;

      case 5:
         Barcode("STOP", CODE39);
        break;
        
       case 6:
         Barcode("BLOW ON", CODE93);
        break;
 
        case 7:
         Barcode("BLOW OFF", CODE93);
        break;
        
         case 8:
         Barcode("PUMP ON", CODE93);
        break;
        
        case 9:
         Barcode("PUMP OFF", CODE93);
        break;
        
      default:
        break;   
     }
  CommandExec=false;
  writeRegs[2]= (word)Print_Command;
  }
  
  if(Print_Command==0){
    CommandExec=true;
    writeRegs[2]= (word)0;  
  } 
  
}

void Printer_Init() {
  NextWriteTime(1500000L);
  Printer_On(); 
  Chinese_US(); 
  //Printer_Setting();
  BarcodeHeight(80);  
}


void Chinese_US(){
  writeBytes2(27, 116);// character code 
  writeBytes(0);//US

  writeBytes2(27, 82);// character code 
  writeBytes(0);//US
  
  writeBytes2(27, 57);// Chinese code format
  writeBytes(1);//UTF-8 encoding

  writeBytes2(28, 46);// cancel kanji characters
}

void Printer_Setting(){
  writeBytes2(27, 55);
  writeBytes(20); 
  writeBytes(heatTime);
  writeBytes(250);

  writeBytes2(18, 35);
  writeBytes((printBreakTime << 5) | printDensity);    
}

void Printer_On() {
  NextWriteTime(0);
  writeBytes(255);
  for(byte i=0; i<10; i++) {
    writeBytes(27);
    NextWriteTime(10000L);
  }
  writeBytes2(27, 64);
}


void WriteTimeout() {
  while((long)(micros() - NextWriteBytes) < 0L); 
}

void NextWriteTime(unsigned long x) {
  NextWriteBytes = micros() + x;
}


void writeBytes(byte a) {
  WriteTimeout();
  Serial1.write(a);
  NextWriteTime(1500L);
}

void writeBytes2(byte a, byte b) {
  WriteTimeout();
  Serial1.write(a);
  Serial1.write(b);
  NextWriteTime(3000L);
}

void writeBytes3(byte a, byte b, byte c) {
  WriteTimeout();
  Serial1.write(a);
  Serial1.write(b);
  Serial1.write(c);
  NextWriteTime(4500L);
}

void Printer_Feed(byte x){
  writeBytes3(27,100,x);
}

//bit0==0 Print is Ready 
//bit0==1 Print is Not Ready  
//bit2==0 There are paper 
//bit2==4 Out of Paper 
//bit3==0 Voltage is normal 
//bit3==8 Voltage higher than 9.5V  
//bit6==0 Temperature is normal 
//bit6==64 Temperature over 60 degrees  
   
byte Printer_Status() {
  writeBytes3(27, 118,0); 
  byte Pstatus=255; 
  for (int i = 0; i < 1000; i++) {
    if (Serial1.available()) {
       Pstatus = Serial1.read();
       return Pstatus;
    }
  }
  return Pstatus;
}

void BarcodeHeight(int val) { 
  if(val < 1) val = 1;
  barcodeHeight = val;
  writeBytes3(29, 104, val);
}

void Barcode(char * text, byte type) {
  int  i = 0;
  byte c;

  writeBytes3(29,  72, 2); //text: 0 no, 1 top, 2 btm, 3 top&btm
  writeBytes3(29, 119, 3);
  writeBytes3(29, 107, type);
  do {
    writeBytes(c = text[i++]);
  } while(c);
  NextWriteTime((barcodeHeight + 40) * dotPrintTime);
  Printer_Feed(2);
}




Labels:





Newer Post Older Post Home

You may also like these ebook:

Get Free PLC eBook directly sent to your email,
and email subscription to program-plc.blogspot.com




We hate SPAM. Your information is never sold or shared with anyone.

Your Email Will Be 100% Secured !

Your email is stored safely on Google FeedBurner