Browsed by
Category: Communication Ports

Examples of how to use communication ports

SimpleComm library to send data through any Arduino Stream

SimpleComm library to send data through any Arduino Stream

This is Industrial Shields library for different PLCs.

With the SimpleComm library you can send data through any Arduino Stream: RS-485, RS-232, Ethernet… It is enough flexible to support different kind of communication typologies: Ad-hoc, Master-Slave, Client-Server… Using an easy to use API.

Reference:

SimplePacket encapsulates the data into a packet and provides these functions:

setData(data)

Fill up the packet with the desired data to send or the received data from a remote device. It is possible to fill up the packet with different types of data: bool, char, unsigned char, int, unsigned int, long, unsigned long, double, const, const char, const void.

Read More Read More

How to create a web server to read a SD card in our PLC

How to create a web server to read a SD card in our PLC

This is an example it is explain how to read a SD card into the PLC through Ethernet connection. In this post we will see how create a server that when there is a request through a browser, you can see the directories and the files of the SD card in your browser.

The PLC has to be connected the Ethernet connection and the SD card correctly, check this post how to do it.

Once it is all connected correctly the next step is make the right configuration of the Ethernet port and create the server:

 

/*
   Copyright (c) 2017 Boot&Work Corp., S.L. All rights reserved

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <MDUINO.h>
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h>


#define SD_SS_PIN 3


File root;

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);

/////////////////////////////////////////////////////////////////////////////////
void setup() {
	Serial.begin(115200L);

  // Init Ethernet and TCP server
  Serial.println("Starting Ethernet ...");
  Ethernet.begin(mac);
  server.begin();

  // Init SD card
  Serial.println("Starting SD card ...");
  if (!SD.begin(SD_SS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
  EthernetClient client = server.available();
  if (client) {
    char lastC = 0;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if ((c == '\n') && (lastC == '\n')) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/plain");
          client.println("Connection: close");
          client.println();
          client.println("SD card files:");

          root = SD.open("/");
          printDirectory(root, client);

          break;
        }
        if (c != '\r') {
          lastC = c;
        }
      }
    }
    delay(50);
    client.stop();
  }
}

////////////////////////////////////////////////////////////////////////////////
void printDirectory(File dir, Stream &stream) {
  while (true) {
    File entry = dir.openNextFile();
    if (!entry) {
      // no more files
      break;
    }

    stream.print(entry.name());
    if (entry.isDirectory()) {
      stream.println("/");
    } else {
      // files have sizes, directories do not
      stream.print(" [");
      stream.print(entry.size(), DEC);
      stream.println("]");
    }
    entry.close();
  }
}

See also:

How to connect a SD card to a PLC, How to connect the Ethernet port using W5100 library

 

How to connect a SD card to a PLC

How to connect a SD card to a PLC

 

In this example we will see how to connect a SD card to each of our PLCs.

Hardware required:

  • One of our PLCs
  • A SD card

How to connect the SD card to our PLC?

First we start with the connections; as follows you have a table and circuit diagram where you can see how to connect the SD card to the PLC.

 

 

 

PLC SD CARD
MOSI MOSI
SCK SCK
PIN3 CS
CD
MISO DO
GND

 

Next there is an example code to how to configured our PLC to read the SD card.        

Read More Read More

Ardbox Relay Switch Configuration

Ardbox Relay Switch Configuration

plc-arduino-arbox-relay-20-dinArdbox Relay can be configurated with multiple communication ports. There are two DIP Switch to configurate all of them.

Switch Configuration:

LEFT Zone

Arduino

Pin

OFF*

ON

 ardbox_relay_side00

7

DE

R1

4

RE

R2

1

I0.3

RX

0

I0.2

TX

3

SCL

I0.1

2

SDA

I0.0

LEFT ZONE. To enable communication connections the switchs must be set to “OFF”. Set to “ON” position to enable I/Os  PLC connection.  Communications and I/Os on the chart can not work simultaneously. For exemple if DE is enabled (OFF),  R1 will not work. OFF position provides direct connection to Arduino Pin (NOT for TX and RX), so they can be programmed according to Arduino pin features.

Read More Read More

How to connect the Ethernet Port using w5100 library

How to connect the Ethernet Port using w5100 library

In this example, you will use the Ethernet port on the Ethernet family products.

Harware Required:

Comments:

All Ethernet family products use an Arduino Mega 2560 connected to the w5100 Ethernet chip.

The standard Ethernet shield for arduino use the SPI port where are connected the pins 50 (MISO), 51 (MOSI), 52 (SCK), 53 (SS), but for the M-duino Ethernet PLC the standard w5100 library from Arduino IDE the SS function is done by the digital PIN 10 because of was developed for the Arduino UNO board.

Then it is necessary to replace the original w5100.h library  to use the right library on that product.

note:

You can replace that library on:

using WINDOWS:

C:\Program Files (x86))
<Directori standard d’instal·lació>\Arduino\libraries\Ethernet\src\utility\w5100.h

using LINUX:

/Arduino/libraries/Ethernet/src/utility/w5100.h

 

UPLOADING EXAMPLE CODE:

In this example you can get the time from an NTP server

Read More Read More

Modbus TCP M-Duino

Modbus TCP M-Duino

Modbus TCP can be programmed in M-Duino PLCs.

Once the Arduino IDE is configured according to Industrial Shields requirements

http://blog.industrialshields.com/es/ethernet-shield-libraries/, the follwoing library can be used to program the M-Duino as Modbus TCP slave.

Basically, this example links holding registers 40000, 40001 and 40002 to outputs Q0.1, Q0.2 and Q0.3 of the M-Duino. In this way these 3 outputs can be operated via a Modbus TCP master. You will have to modify the IP address and network parameters according to your LAN.

An illustrative and simple example to test the application is with the following android free app. https://play.google.com/store/apps/details?id=com.bencatlin.modbusdroid&hl=es
Which will allow you to operate the M-Duino outputs via your smartphone. You only need to set the IP address of the slave and  you can begin controlling the M-Duino remotely.

ModbusDroid

Now you can simply remotely control your M-Duino.

Enjoy it!

PLC- HummTouch RS485/RS232

PLC- HummTouch RS485/RS232

In this post we are going to learn how to send data between Industrial Shields PLCs and HummTouch PanelPc. In order to apply or implement the functionalities and examples described, the PanelPC   HummTouch Linxu/Android is assumed to include the suitable preloaded image. All PanelPC acquired as of 21-08-2015 will include the required image. For those PanelPC  acquired before 21-08-2015 the procedure to install the new compatible image will be provided via email.

HummTouch

The following chart shows the D37 connector  pinout for the  HummTouch (Android and Linux).  

Table 1. HummingBoard/HummTouch pinout  reference

Hummingboard GPIOS HummTouch D37 CONNECTOR pin
195 28
194 27
73 6
72 25
71 7
70 8
67 30
1 4

HummTouch Linux:

HummTouch  supports TTL (5V-0V) serial communication through the D37 connector. MAX485 and  MAX232 components are required to adapt to RS232/RS485 protocols.

Table 2. RS232/RS485 pinout

Pin D37 Function Protocol
22 GND RS2323/485
23 TX RS2323/485
34 RX RS2323/485
Any GPIOS (for the example 25) Control/chip Rs485

 

RS485

Industrial Shields PLCs provide the A B connection for RS485 protocol. HummingBoard i2 requires to configure the control/chip pin (any GPIOS) to connect to the MAX485 component. For this example we take pin 25 from the HummingBoard PI D37 connector (GPIOS 72)  as chip/control pin. Steps to configure the 25 PIN as chip/control on HummingBoard i2.

  1. Open HummTouch console:
  2. User: hb
  3. Pass: hummingboard
  4. Write the following instructions
sudo su                                            //acquire superadmin rights

echo “72” >/sys/class/gpio/export

echo out>/sys/class/gpio/gpio72/direction // define GPIOS18 (pin 25 D37) as control/chip

echo 1 >/sys/class/gpio/gpio72/value         // set control/chip pin to transmit state (HIGH) 1

exit //

Table 3. Touchberry PI to MAX485 connection reference

D37 connector   MAX485
22 (GND) –> RE
23 (TX) –> DI
24  (RX) –> RO
25  (CS) –> DE

*TX and RX cables connection is not cross between D37 to MAX485 Steps to configure the 25 PIN as chip/control on HummTouch Linux. With the code and connections above, the RS485 protocol for HummTouch (Linux)  is set-up and configured to transmit state. In order to  test so, the communication between PLC and the PanelPC we are going to send some sentences through RS485 and RS232.

  1.  Open minicom on HummTouch (Linux).
  2. Type the following:
sudo minicom -D /dev/ttyAMA0 -b 4800

Any code written on the console should be send  through RS485 to the PLC. With the Serial Monitor on the PLC we  can see the code typed on the HummTouch console. The Arduino code to read messages on the RS48 Bus is here. Another example shows the  Arduino code to send data to the HummTouch Linux through the RS485 bus.

RS232

For RS232 there is no control pin, compared to RS485, so  no configuration instructions are required.  To connect the PanelPC to the  MAX232 the pins 22 (GND), 23 (TX), 24 (RX) (Table 4) are to be linked to to the  MAX232 pins GND, TX, RX. Table.4

D37 connector   MAX232
22 (GND) –> GND
23(TX) –> TX
24(RX) –> RX

*TX and RX cables connection is not cross between D37 to MAX232.

HummTouch Android

For the Android HummTouch  the conversion between TTL to RS232 and RS485 is carried out with USB-RS232 and USB-RS485 adaptors (cable and software).

  • USB-RS232

This connection is quite simple. First of all, an Android serial terminal   has to be installed in the HummTouch. This can be download from the following link. After that,  the USB-RS232 adaptor can be connected to the PLC and HummTouch.  Now the communication is configured, we just need to  read and write through the RS232 terminal.

  • USB-RS485 (soon to be released)

 Regarding communications with the USB-RS485 adaptor, no pin configurations is required, there is no need to specify the control pin nor its state (transmit, receive). The Arduino code to read and write thorugh  RS485 is the same file as for the RS232. Read and write. As for the I/Os on the HummingBoard, the GPIOS can be configured as input or outputs and its states toggeled: In this link there is an app to emulate a terminal console for Android which needs to be installed. The instructions to be typed on the console to configure the GPIOS are writen below.

su root

echo out>/sys/class/gpio/gpio72/direction // define GPIOS 72 (pin 25 D37) as output

echo 1 >/sys/class/gpio/gpio72/value         // set GPIOS 72  to HIGH  state

echo 0 >/sys/class/gpio/gpio72/value         // set GPIOS 72  to LOW  state

echo in>/sys/class/gpio/gpio72/direction // define GPIOS 72 (pin 25 D37) as input

PLC- Touchberry PI RS485/RS232

PLC- Touchberry PI RS485/RS232

In this post we are going to learn how to send data between Industrial Shields PLCs and TouchBerry PI PanelPc. In order to apply or implement the functionalities and examples described, the TouchBerry PI is assumed to include the suitable preloaded image. All PanelPC acquired as of 21-08-2015 will include the required image. For those PanelPC  acquired before 21-08-2015 the procedure to install the new compatible image will be provided via email.

Touchberry  PI

Touchberry PI supports TTL (5V-0V) serial communication through the D37 connector. MAX485 and  MAX232 components are required to adapt to RS232/RS485 protocols.

Table 1.

Pin D37 Function Protocol
22 GND RS2323/485
23 TX RS2323/485
34 RX RS2323/485
Any GPIOS (for the example 25) Control/chip Rs485

Table 2. Raspberry PI 2B  GPIOS –> ToucBerry PI D37 Connctor

GPIOS D37 CONNECTOR pin
24 28
23 27
17 6
18 25
27 7
22 8
25 30
14 4

RS485

Industrial Shields PLCs provide the A B connection for RS485 protocol. Raspberry PI 2B requires to configure the control/chip pin (any GPIOS) to connect to the MAX485 component. For this example we take pin 25 from the TouchBerry PI D37 connector (GPIOS 18)  as chip/control pin. Steps to configure the 25 PIN as chip/control on Raspberry Pi.

  1. Open Touchberry PI console:
  2. User:pi
  3. Pass:raspberry
  4.  Write the following instructions
sudo su                                            //acquire superadmin rights

echo “18” >/sys/class/gpio/export

echo out>/sys/class/gpio/gpio18/direction // define GPIOS18 (pin 25 D37) as control/chip

echo 1 >/sys/class/gpio/gpio18/value         // set control/chip pin to transmit state (HIGH) 1

exit //

With the code above the Raspberry PI RS485  protocol is set-up and configured to transmit state.

Table 3. Touchberry PI to MAX485 connection reference

D37 connector   MAX485
22 (GND) –> RE
23 (TX) –> DI
24  (RX) –> RO
25  (CS) –> DE

*TX and RX cables connection is not cross between D37 to MAX485 Now to test that the communication between PLC and Rasberrry PI is enabled we are going to send some sentences through RS485 and RS232.

  1. Open minicom on Touchberry PI.
  2. Type the following:
 sudo minicom -D /dev/ttyAMA0 -b 4800

Any code written on the console should be send  through RS485 to the PLC. With the Serial Monitor on the PLC we  can see the code typed on the Touchberry PI console. The Arduino code to read messages on the RS48 Bus is here. Another example shows the  Arduino code to send data to the Touchberry PI through the RS485 bus.

RS232

For RS232 there is no control pin, compared to RS485, so  no configuration instructions are required.  To connect the PanelPC to the  MAX232 the pins 22 (GND), 23 (TX), 24 (RX) (Table 4) are to be linked to to the  MAX232 pins GND, TX, RX Table.4

D37 connector   MAX232
22 (GND) –> GND
23(TX) –> TX
24(RX) –> RX

*TX and RX cables connection is not cross between D37 to MAX232.

RS485 and RS232 implementation

RS485 and RS232 implementation

rs232-485 logo ny 100x100

In this post we are going to implement  RS485 ans RS232 communication protocols to the Industrial Shields PLC Ardbox and/or MDuino families.

IMPORTANT!!: 

RS232 is already available for Ardbox family and for MDuino family. Included Arduino libraries for this post

  • SoftwareSerial.h

The example presents the code for a Master device and for a Slave device to communicate through RS485 and RS232 protocols.

Read More Read More