Monday, October 13, 2014

Send Data From USB Keyboard to PLC using Raspberry Pi and Modbus

Send data from usb keyboard to PLC used for input: password, product name, data setting, replacement selector switch, replacement push button switch, expand plc input, replacement sensors, data testing, and others.
For sending data to the PLC, used Raspberry Pi with RS232 PLC cable, RS232 Module and Modbus protocol.
The process of reading the data: Read data from the usb keyboard by raspberry pi, and Raspberry Pi send keyboard data to the PLC memory.
In this article, I will give an example to check the product name in the input from the keyboard.
If product name is toyota, the PLC output 0 is ON.
If product name is mazda, the PLC output 1 is ON.

Keyboard keystrokes process:
Product name 1: toyota
toyota = press 't','o','y','o','t','a', press Enter
t=keycode 23 (in plc memory = 23)
o=keycode 18 (in plc memory = 18)
y=keycode 28 (in plc memory = 28)
o=keycode 18 (in plc memory = 18)
t=keycode 23 (in plc memory = 23)
a=keycode 4 (in plc memory = 4)
return/enter = keycode 40 (in plc memory = 40)
Keycode for toyota

Product name 2: mazda
mazda = press 'm','a','z','d','a', press Enter
m=keycode 16 (in plc memory = 16)
a=keycode 4 (in plc memory = 4)
z=keycode 29 (in plc memory = 29)
d=keycode 7 (in plc memory = 7)
a=keycode 4 (in plc memory = 4)
return/enter = keycode 40 (in plc memory = 40)
Keycode for mazda

After Enter pressed, PLC Output 0 / PLC Output 1 is ON.
Additional Key:
Keypad Enter=keycode 88 (in plc memory = 88)
Escape = keycode 41 (in plc memory = 41)
Download all keyboard code, click here

Hardware and Software needed:
1. Hardware Image and Install Software: Same the link
# Raspberry Pi, SD Card, mouse, keyboard, monitor and Adapter
# Programmable Logic Controller (PLC)
# PPI PLC Cable
# UART TTL to RS232 converter module
# RS232 crossover cable
2. USB PC Keyboard
Connecton for this application:
Connection of USB Keyboard to PLC and Raspberry Pi

On Raspberry Pi:
1. Get ID Information of Keyboard
Check Vendor ID and Product ID of Your USB Keyboard:
Open LXTerminal
Type lsusb
lsusb Vendor Id Product Id

2.Modified File of /etc/udev/rules.d/99-input.rules
Open LXTerminal
Type sudo nano /etc/udev/rules.d/99-input.rules
rules setting on raspberry pi

3. Change Hex Value of Vendor Id and Product Id on Python

Vendor Id and Product Id Setting on Python

Download Peoject File:
Copy-Paste this file to flash disk, extract rar file, and insert flash disk to USB on raspberry pi.
1. Python project file, click here
2. PLC Ladder Programming, click here
Video about Python project file setup on Raspberry Pi.


Newer Post Older Post Home

You may also like these ebook:

Get Free PLC eBook directly sent to your email,
and email subscription to

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

Your Email Will Be 100% Secured !