Raspberry Pi Serial

From Axel Public Wiki
Revision as of 17:38, 24 January 2018 by Axelpwiki (talk | contribs) (→‎Define desired parameters)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

The Axel PLC runtime for Raspberry PI offers some possibilities if you have an external USB serial device, for example an RS232 or RS485 USB interface.
First of all, from a command line, you have to identify what device file represents that serial line under Linux, usually once you have inserted a new serial interface on an usb port, the file /dev/ttyUSB0 will appear.
One you have identified the file, suppose /dev/ttyUSB0, you have three ways to use it under PLC runtime:

  • Use directly the serial line in IEC code, by using the I/O interface offered by the library Serial.
  • You can configure yor Raspberry PI as a Modbus RTU Slave.
  • You can drive some Modbus IO module, by using your Raspberry PI as a Modbus RTU Master
  • You can configure the Raspberry PI as a gateway from Modbus TCP to a Modbus RTU: this means that the Raspberry PI act as Modbus TCP Slave, but forward the commands on the serial line to some Modbus RTU slave.

Please follow the following instructions for an introduction.

Serial Library

The library Serial allows to read and write bytes and string from serial device directly from IEC code.
The functions in the library must be called from the background task.

Open port

To open the serial port you must declare a variable of type SERIAL_HANDLE,
then you can open the port with instruction like this (ST Language):

hand := Serial_ConfigPortByName('/dev/ttyUSB0',
 SERIAL_BAUDRATE_115200, 
 SERIAL_NOPARITY,
 SERIAL_BYTESIZE_8,
 SERIAL_ONESTOPBIT,
 TRUE
);

You can see the details from the library tree.

if (TO_INT(hand) >= 0) then port succesfully opened 
else some error occoured



Write (and read) data

You can write data on the serial line like this:

Serial_PutString(hand, 'Hello world!!!!');
Serial_PutByte(hand, 10); (* LF *)
Serial_PutByte(hand, 13); (* CR *)


You can read data as well with the functions Serial_GetString and Serial_GetByte

Close handle

After using the serial port you must close the SERIAL_HANDLE like this

Serial_ClosePort(hand);


Modbus RTU Slave

With LogicLab the Raspberry PI can act as a Modbus RTU slave.
It can be used as a Modbus TCP slave as well.
The steps to follow are:

  • Define some status variable or parameter in LogicLab
  • Configure RS485 port (or Ethernet port) as Modbus Slave.
  • Download the code and use it.

Define desired parameters

On the left please switch to resources tab.
On the tree please click on "Parameters" or "Status variables" node: this will open a configuration window.
The difference between them is that "Parameters" are persistent (their value is written on SD).
When you write the name of a parameter, LogicLab automatically generates a PLC variable with that name, you can use it from your PLC code, or you can access it from a Modbus master.

Configure Serial Line

On the left please switch to resources tab.
On the tree please click on RS485 node: this will open a configuration window.
Select "Modbus RTU Slave" and configure communication parameters.

Modbus RTU Master

To use the Raspberry PI as a ModbusRTU Master you have two ways.

  • Define your own objects with Modbus custom editor
  • Use the Generic modbus module.

Modbus custom editor

To open the Modbus custom editor please select menu Developer ==> Run Modbus custom Editor
On the Modbus editor please insert the Name of your object, then select if it is a Modbus RTU or a Modbus TCP slave.
Press "Add" button to insert some register, or coil.
Now you can save the device with the button Save.
Now select the RS485 port from the tree in tab Resources
You must configure the port as Modbus RTU master then set the appropriate communication parameters.
You can open the Catalog window from the menu Developer ==> View Catalog
Here you can select your device and drag it to the RS485 node in the Resources tree.

Click on the device in the resource tree: a main window will be opened.
In the General tab, insert the Modbus node number.
In the Input and Output tab you can configure the plc variables.
Press Add then select ie an Holding Register.
Insert a name in the colum variable, LogicLab will create the plc variable for you.
Once you have configured your input and output variables, you can use them in the IEC 61131 code.

Generic modbus module

You can select the RS485 port from the tree in tab Resources
You must configure the port as Modbus RTU master then set the appropriate communication parameters.
You can open the Catalog window from the menu Developer ==> View Catalog
Here you can select the device Generic Modbus then drag it to the RS245 node in the resources window.

Click on the generic modbus device in the resource tree: a main window will be opened.
In the General tab, insert the Modbus node number.
In the Catalog window the standard Modbus commands are displayed, please drag FC-03 "Read holding register" to the generic modbus device in the resources window.
Click on it in the resources window, then open tab Holding reg.
enter a name under the Label column, then LogicLab will create the plc variable for you.
You can now use it in the IEC 61131 code.

Modbus RTU Gateway

This feature allows you to use the Raspberry PI as a gateway from Modbus TCP to Modbus RTU.
This meas that you can connect your Raspberry to a Modbus TCP master, then connect a Modbus RTU node to your Raspberry PI, so the TCP master will drive the RTU slave.
To configure select the Ethernet node in the resources window on the left.
Select Modbus TCP Slave and Modbus RTU Gateway
Select the RS485 node on the resources window,
from here you can select Modbus RTU master and the right communication parameters.
Please connect and download the code on the target, the gatway functionality will immediately work.