Raspberry Pi Serial: Difference between revisions

From Axel Public Wiki
Jump to navigation Jump to search
No edit summary
Line 43: Line 43:


= Modbus RTU Slave =
= Modbus RTU Slave =
With LogicLab the Raspberry PI can act as a Modbus RTU slave.<br>
It can be used as a Modbus TCP slave as well.<br>
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.
== Configure Serial Line ==
On the left please switch to resources tab.<br>
On the tree please click on RS485 node: this will open a configuration window.<br>
Select "Modbus RTU Slave" and configure communication parameters.


= Modbus RTU Master =
= Modbus RTU Master =

Revision as of 10:48, 23 January 2018

_TOC_

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.

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

Modbus RTU Gateway