Code covered by the BSD License  

Highlights from
Modbus RS232 ASCII Communication Functions

Be the first to rate this file! 30 Downloads (last 30 days) File Size: 79.76 KB File ID: #27815

Modbus RS232 ASCII Communication Functions

by Steven Edmund

 

01 Jun 2010 (Updated 18 Jun 2010)

Modbus RS-232 ASCII serial communication with a Programmable Logic Controller (PLC)

| Watch this File

File Information
Description

Matlab communication with a Programmable Logic Controller (PLC) over an RS-232 serial interface using ASCII Modbus protocol. This code was developed while working with an Eaton-ELC PLC device and was modified for general distribution. Standard Modbus communication protocols were employed.

Files included are:
ExampleCommunication.m - an example of using the attached files and additional notes on their use
serialstart.m - initiate and set up serial object
LRC.m - perform longitudinal redundancy check operation
moderr.m - Function for displaying modbus error codes
modbus2.m - read 40 binary values (coils) from PLC
modbus3.m - read 16 positive integers (registers) from PLC
modbus15.m - write 40 binary values (coils) to PLC
modbus16.m - write 16 positive integers (registers) to PLC

To use this code the user must:
1. Set up the serial communication link by specifying the modbus communication settings on the PLC and ensuring that those specifications in serialstart.m match. An image of the ladder logic for setting up serial communication on the Eaton PLC is included.
2. Specify the device to be addressed: Multiple PLC's can use the same serial cable, thus each PLC has an address. In most applications with 1 PLC this will be '01'
3. Specify the memory in the PLC to be addressed. This is the starting address value, 16 or 40 memory spots will then be addressed sequentially based upon the modbus mode. The address value is a hex number i.e. '10FF' is the address for memory 04351. Be sure to reference your PLC user manual for proper memory addressing.
4. Pass your information and serial port object to the modbus interface function and receive a result.

Error checking: A longitudinal redundancy check is employed on all communication, in addition the communication is checked to ensure that data is received back from the PLC, and that the returned mode is correct. If the returned mode is incorrect then moderr.m will attempt to match the returned signal with a known error code. If a communication error is encountered the program will make multiple attempts.

Acknowledgements
This submission has inspired the following:
RS485/MODBUS Communication with JLD416PVA Power Meter
MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Updates
18 Jun 2010

Corrected Title

Tag Activity for this File
Tag Applied By Date/Time
modbus Steven Edmund 02 Jun 2010 09:43:37
rs232 Steven Edmund 02 Jun 2010 09:43:37
serial Steven Edmund 02 Jun 2010 09:43:37
plc programmable logic controller Steven Edmund 02 Jun 2010 09:43:37
longitudinal redundancy check Steven Edmund 02 Jun 2010 09:43:37
ascii Steven Edmund 02 Jun 2010 09:43:37
modbus Chee Suah 08 Jun 2010 15:56:08

Contact us at files@mathworks.com