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.
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 Connection:
Barcode for Light ON / Light OFF:
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
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:
Arduino
Arduino Microcontroller
Arduino USB Host Shield
Barcode Reader
Barcode Scanner
PLC and Arduino
PLC Application
Arduino
Arduino Microcontroller
Arduino USB Host Shield
Barcode Reader
Barcode Scanner
PLC and Arduino
PLC Application