Tuesday, March 17, 2015

Barcode Reader / Scanner USB on PLC using Arduino USB Host Shield





This application about barcode reader/scanner USB for use on PLC, with additional hardware using Arduino Uno and Arduino USB Host Shield.

Transfer data from barcode reader /scanner to arduino using HID (Human Interface Device) protocol and transfer data from arduino to PLC using Modbus protocol.

Example application with barcode reader/scanner in industrial such as Kanban System or Toyota's "just-in-time" (JIT) production system, counting the number of products, accessing machine with barcode card , lamp/light control with barcode card, and other application.

For example, I use light control using barcode.



Barcode Reader Scanner USB on PLC for Light Control

Hardware Preparation:
1. Barcode reader/scanner USB
2. Arduino USB Host Shield
3. Arduino UNO
4. Serial Port Module, TTL to RS232 male connector
5. RS232 PLC Cable for Siemens S7-200 PLC
6. Siemens S7-200 PLC

Hardware of Barcode Application On PLC


Hardware Connection:

Hardware Connection of Barcode on PLC

Barcode for Light ON / Light OFF:

Barcode on PLC (Programmable Logic Controller)

Download Project File:
1. Arduino Project File (BarcodeToPLCviaModbus), click here


2. PLC ladder programming (BarcodePLCModbus), click here

Barcode Setting in PLC Ladder Programming:
Download Character to Value (ASCI code), click here

Setting of Barcode Value in PLC ladder programming


Arduino Code:
#include <hid.h>
#include <hiduniversal.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hidboot.h>

#include <SimpleModbusMaster.h> 
//Arduino Serial Port Connect to Port 0 of Siemens PLC S7 200
#define slaveAddr 1
#define baud 9600
#define timeout 1000
#define polling 200
#define retry_count 0
#define TxEnablePin 2 
enum
{
  PACKET1,
  TOTAL_NO_OF_PACKETS
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
#define Char_Max 20
unsigned int writeRegs[Char_Max];
boolean ModbusUpdate=true;
static uint32_t update_time = 0;  


USB     Usb;
USBHub     Hub(&Usb);                                          
HIDUniversal      Hid(&Usb);                                  
HIDBoot<HID_PROTOCOL_KEYBOARD>    Keyboard(&Usb);
 
class KbdRptParser : public KeyboardReportParser
{
        void PrintKey(uint8_t mod, uint8_t key);
protected:
 virtual void OnKeyDown (uint8_t mod, uint8_t key);
 virtual void OnKeyPressed(uint8_t key);
};
 
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) 
{
    uint8_t c = OemToAscii(mod, key);
 
    if (c)
        OnKeyPressed(c);
}
 

void KbdRptParser::OnKeyPressed(uint8_t key) 
{
static uint32_t next_time = 0;    
static uint8_t index_array = 0;

    if( millis() > next_time ) {
      memset(writeRegs,0,sizeof(writeRegs));
      index_array = 0;
      writeRegs[0] = (word) 0;
    }
    
    next_time = millis() + 100;   
    
    index_array++;    
    if (index_array<Char_Max){
      writeRegs[index_array] = (word) key;
      ModbusUpdate=true;
      update_time = millis() + 500;
    }else{
      //Error, I use value 255
      writeRegs[0] = (word) 255;  
      ModbusUpdate=false;  
    }
    
};
 
KbdRptParser Prs;
 
void setup()
{
    modbus_construct(packet1, slaveAddr, PRESET_MULTIPLE_REGISTERS, 0, 20, writeRegs);
    modbus_configure(&Serial, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);  
    if (Usb.Init() == -1) {
        //Error, I use value 255
        writeRegs[0] = (word) 255;
        ModbusUpdate=false;
    }
 
    delay( 200 );
 
    Hid.SetReportParser(0, (HIDReportParser*)&Prs);
    delay( 200 );
}
 
void loop()
{
  Usb.Task();
  if(ModbusUpdate)modbus_update();  
  if( millis() > update_time ) ModbusUpdate=false;  
}




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