Browsed by
Category: Communication Ports

Examples of how to use communication ports

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!

MODBUS RTU M-Duino Ardbox

MODBUS RTU M-Duino Ardbox

In this post it is shown how to implement Modbus RTU (RS485/RS232) in M-Duinos and Ardbox PLCs. The library used is a modification of the SimpleModbus, which has been adapted to the internal configuration of Industrial Shields PLCs (SimpleModbus library for Industrial Shields PLCs).  Adapted libraries to Industrial Shields PLCs  are available here

 First the code for the Master is showed below.

#include <SoftwareSerial.h>
#include <SimpleModbusMasterSoftwareSerial.h>

#define ARDBOX
//#define MDUINO

#ifdef ARDBOX
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 7 // DE //RS485 Direction pin
#define GroundRE 4 // RE
#elif MDUINO
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 14 // DE //RS485 Direction pin
#define GroundRE 13 // RE
#endif

#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate 
#define retry_count 10


int SSerialRX =11 ;// RO  //RS485 Receive pin
int SSerialTX=10; // DI  //RS485 Transmit pin
int  SSerialTxControl=7;  // DE  //RS485 Direction pin
int GroundRE=4 ; // RE
SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

enum
{
PACKET1,
PACKET2,
PACKET3,
PACKET4,
TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
#define TOTAL_NO_OF_REGISTERS 4
unsigned int regs[TOTAL_NO_OF_REGISTERS];


void setup()
{
  Serial.begin(9600);
  pinMode(SSerialTxControl, OUTPUT);  
  pinMode(GroundRE, OUTPUT);
  digitalWrite(GroundRE,LOW);

  modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 100, 1, 0); 
  // parameters: packet/ID Slave/ function/ Modbus address/Total numbr of registers/position in the  'regs' array.
 modbus_construct(&packets[PACKET2], 1, PRESET_SINGLE_REGISTER, 110, 1, 1);
  modbus_construct(&packets[PACKET3], 1, READ_COIL_STATUS, 0, 1, 2);
   modbus_construct(&packets[PACKET4], 1, FORCE_MULTIPLE_COILS, 16, 1,3);
  modbus_configure(&RS485Serial, baud, SERIAL_8N1, timeout, polling, retry_count, SSerialTxControl, packets, TOTAL_NO_OF_PACKETS, regs);
}

void loop()
{
  modbus_update();

regs[1]=200; //(PACKET 2) The value 200 will be written to Holdin register 110.
regs[3]=1; //  (PACKET 4)The value 1 will be written to COIL 16.
Serial.print(regs[0]); //(PACKET 1) It is printed in Serial Monitor the value contained in Holding Register 100 of the slave.
Serial.print(regs[2]);  //(PACKET 3) It is printed in Serial Monitor the value contained in the Coil 0 of the slave.
delay(1000);

}

The code for a Modbus RTU Slave is the following.


#include <SoftwareSerial.h>
#include <SimpleModbusSlaveSoftwareSerial.h>

#define ARDBOX
//#define MDUINO

#ifdef ARDBOX
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 7 // DE //RS485 Direction pin
#define GroundRE 4 // RE
#elif MDUINO
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 14 // DE //RS485 Direction pin
#define GroundRE 13 // RE
#endif

SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

#define  Q05 9  

// and at a glimpse informs you of your slaves register layout.

//////////////// registers of your slave ///////////////////
enum 
{     
  // just add or remove registers and your good to go...
  // The first register starts at address 0
  ADC_VAL,     
  PWM_VAL,        
  HOLDING_REGS_SIZE // leave this one
  // total number of registers for function 3 and 16 share the same register array
  // i.e. the same address space
};

unsigned int holdingRegs[HOLDING_REGS_SIZE]; // function 3 and 16 register array
////////////////////////////////////////////////////////////

void setup()
{

  pinMode(SSerialTxControl, OUTPUT);  
  pinMode(GroundRE, OUTPUT);
  

  digitalWrite(GroundRE,LOW);
  modbus_configure(&RS485Serial, 9600, SERIAL_8N2, 1, SSerialTxControl, HOLDING_REGS_SIZE, holdingRegs);

  // modbus_update_comms(baud, byteFormat, id) is not needed but allows for easy update of the
  // port variables and slave id dynamically in any function.
  modbus_update_comms(9600, SERIAL_8N2, 1);
  
  pinMode(Q05, OUTPUT);
}

void loop()
{
  modbus_update();
  
  holdingRegs[ADC_VAL] = analogRead(A0); // update data to be read by the master to adjust the PWM
  
  analogWrite(Q05, holdingRegs[PWM_VAL]>>2); // constrain adc value from the arduino master to 255
  
}

Contact us at industrialshields@industrialshields.com if you need more information.

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.

The routines of the sketches are the following:

#Master: The Master device reads (upon pressing “enter”) any characters that are typed from the Arduino IDE Serial monitor (Serial.read()). This  data is transmitted as byte type. The Master device reads the data and  resends it through RS485  bus (RS485Serial.write()).  After that, the Master is shifted to recive mode (RS485Receive) and it will read any bytes comming into the RS485 bus RS485Serial.read().  The read bytes are printed on the Serial monitor (Serial.write()). Finally, the Master device will print through RS232 bus “Hello World” characters (Serial1.write()). The described instructions run repeatedly in the void loop.

#Slave: Initially the Slave PLC  is set to receiving mode (digitalWrite(SSerialTxControl, RS485Receive)) and will read any bytes coming through RS485 bus RS485Serial.read().  The input bytes will be printed on Serial monitor (Serial.write()) and back to RS485 bus(RS485Serial.write()) .  Finally the Slave will read any incoming bytes from the RS232 bus (Serial1.read()) and resend them through the same protocol (Serial1.write()). The code on the .ino files allows easy-configuration for MDuino or Ardbox by commenting and/or uncommenting  the #define ARDBOX  / #define MDUINO instructions at the first lines of the sketch. Similarly, in order to enable RS232 communication  the #define ADDRS232Test  declaration should be uncommented. The .ino files can be downloaded from this link. In order to allow RS485 on Ardbox Family PLCs the RO, DI , RE , DE pins need to be enabled by setting the corresponding configuration switches to communication mode. This is described in the Ardbox Family User Guides. #Master code

#include

//Uncomment for test RS232
//#define ADDRS232Test

//Uncomment depending on the PLC you are using!
#define ARDBOX
//#define MDUINO

#ifdef ARDBOX
#define SSerialRX        11 // RO  //RS485 Receive pin
#define SSerialTX        10 // DI  //RS485 Transmit pin
#define SSerialTxControl 7  // DE  //RS485 Direction pin
#define GroundRE         4  // RE
#elif MDUINO
#define SSerialRX        11 // RO  //RS485 Receive pin
#define SSerialTX        10 // DI  //RS485 Transmit pin
#define SSerialTxControl 14 // DE  //RS485 Direction pin
#define GroundRE         15 // RE
#endif

#define RS485Transmit    HIGH
#define RS485Receive     LOW
#define PinLED         13

SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

int byteRecv;
int byteSend;

#ifdef ADDRS232Test
int counter = 0;
#endif

void setup() {
  Serial.begin(9600);
  
#ifdef ARDBOX
  while (!Serial);  
#endif
  
  Serial.println("Write some characters and press ENTER to send them through RS485");
  
  pinMode(PinLED, OUTPUT);   
  pinMode(SSerialTxControl, OUTPUT);  
  pinMode(GroundRE, OUTPUT);
  
  digitalWrite(SSerialTxControl, RS485Receive);
  digitalWrite(GroundRE,LOW);
 
  RS485Serial.begin(4800);

#ifdef ADDRS232Test
  Serial1.begin(9600);
#ifdef ARDBOX
  while (!Serial1);  
#endif
#endif  
}

void loop() {
  digitalWrite(PinLED, HIGH);

  if (Serial.available()) { // Read from console!
    byteRecv = Serial.read();
    
    digitalWrite(SSerialTxControl, RS485Transmit); 
    RS485Serial.write(byteRecv);
    digitalWrite(PinLED, LOW);    
    digitalWrite(SSerialTxControl, RS485Receive);       
  }
  
  if (RS485Serial.available()) { // Read from RS485!
    digitalWrite(PinLED, HIGH);
    byteRecv = RS485Serial.read();
    Serial.write(byteRecv); // Write to console!
    delay(10);
    digitalWrite(PinLED, LOW);
  }  

#ifdef ADDRS232Test
  if (counter > 5000) {
     Serial1.print("Hello World");
     counter = 0;
  }
  counter++;
#endif
}<pre>

#Slave code

 #include 

//Uncomment for test RS232
//#define ADDRS232Test

//Uncomment depending on the PLC you are using!
#define ARDBOX
//#define MDUINO

#ifdef ARDBOX
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 7 // DE //RS485 Direction pin
#define GroundRE 4 // RE
#elif MDUINO
#define SSerialRX 11 // RO //RS485 Receive pin
#define SSerialTX 10 // DI //RS485 Transmit pin
#define SSerialTxControl 14 // DE //RS485 Direction pin
#define GroundRE 15 // RE
#endif

#define RS485Transmit HIGH
#define RS485Receive LOW
#define PinLED 13

SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

int byteRecv;
int byteSend;

#ifdef ADDRS232Test
char inchar;
#endif

void setup() {
Serial.begin(9600);

#ifdef ARDBOX
while (!Serial);
#endif

Serial.println("RS485 Slave");

pinMode(PinLED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);
pinMode(GroundRE, OUTPUT);

digitalWrite(SSerialTxControl, RS485Receive);
digitalWrite(GroundRE,LOW);

RS485Serial.begin(4800);

#ifdef ADDRS232Test
Serial1.begin(9600);
#ifdef ARDBOX
while (!Serial1);
#endif
#endif
}

void loop() {
if (RS485Serial.available()) { // Read from RS485 Master!
byteSend = RS485Serial.read();

digitalWrite(PinLED, HIGH);
delay(10);
digitalWrite(PinLED, LOW);
Serial.println(byteSend); // Write to console!
digitalWrite(SSerialTxControl, RS485Transmit);
RS485Serial.write(byteSend);
delay(1000); // Change if too slow!
digitalWrite(SSerialTxControl, RS485Receive);
}

#ifdef ADDRS232Test
if(Serial1.available() > 0) {
inchar = Serial1.read();
Serial.print(inchar);
}
#endif
}