Modbus TCP with the Bmini shield

From Biemme Italia Wiki
Jump to: navigation, search

Contents


The Bmini shield, when coupled with an Arduino Ethernet Board, can manage Modbus TCP as well as RTU requests. The key ingredients are the following:

  • The BminiIO library
  • The Mudbus library
  • The SimpleSlaveModbus library

Pre requisites

We suppose that you're already familiar with Arduino statements, Arduino IDE, and how Arduino MCU works in general. In case you're not, please refer to Arduino Getting Started tutorial or any good Arduino book. All the examples that we will present, are compliled with Arduino 1.0.1.

Libraries download and installation

The BminiIO library needed is ready for download directly from our wiki here. Please be sure to select the version for Arduino Ethernet.

The Mudbus library is also ready for download from Google code repository.

In case you want to create a modbus RTU/TCP gateway with the Bmini, you need the Modbus RTU library as well. One of the library supported by the Bmini is simple-modbus. There exist two versions, one for Slave and the other for Master devices. Select the one you need depending on the function your device will have in the project architecture.

The versions we use in the examples are:

As with any other Arduino libraries, you can place them under the libraries directory of the Arduino installation. In case you are not familiar with that or you are not able to identify the correct directory, please take a look at this brief tutorial that explains where Arduino libraries must be placed.

Initialization

The setup() section of the Arduino sketch, you need to define the Ethernet parameters, like mac id, ip address, gateway and start the Arduino ethernet library (by calling the begin function). After that, we initialized the modbus registers to their default values, and finally we added the initialization of the Bmini library through the call to bmini_init().

void setup()
{
  uint8_t mac[]     = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
  uint8_t ip[]      = { 192, 168, 1, 8 };
  uint8_t gateway[] = { 192, 168, 1, 1 };
  uint8_t subnet[]  = { 255, 255, 255, 0 };
  Ethernet.begin(mac, ip, gateway, subnet);
  //Avoid pins 4,10,11,12,13 when using ethernet shield
 
  delay(5000);  //Time to open the terminal
 
  //Digital inputs
  Mb.R[0] = 0;
  Mb.R[1] = 0;
  Mb.R[2] = 0;
  Mb.R[3] = 0;
  //digital ouputs  
  Mb.R[4] = 0;
  Mb.R[5] = 0;
  Mb.R[6] = 0;
  Mb.R[7] = 0;
  //analog inputs
  Mb.R[8] = 0;
  Mb.R[9] = 0;
  Mb.R[10] = 0;
  Mb.R[11] = 0;
 
  // bmini pins initialization. Make sure you have installed the BminiIO library for Arduino Ethernet
  bmini_init();
 
}

The Loop section

In the loop() section of the sketch, you only need to update the registers’ state and create your logic based on the project’s requirements. For the purpose of this article, we considered to update the registers based on the modbus tcp requests the node receives.

void loop()
{
  int i;
  //modbus TCP update
  Mb.Run();
 
  //reads digital inputs and store them into the first 4 registers
  for(i=1; i < 5; i++){
    Mb.R[i-1] = get_inputs(i);
  }
  //get the relays state from the following 4 registers and update it with the relay function
  for(i=1; i < 5; i++){
   if (Mb.R[3+i] == 1 ){
    relay(i, HIGH);
   }else{
     relay(i, LOW);
   }
  }
  
  //reads analog values and store the integer representation in the following 4 modbus registers
  for(i=1; i < 5; i++){
    Mb.R[7+i] = analogReadPin(i);
  }
 
  delay(10);
}

Whole Arduino Sketch Example

#include <SPI.h>
#include <Ethernet.h>
#include "Mudbus.h"
#include "BminiIO.h"
 
Mudbus Mb;
//Function codes 1(read coils), 3(read registers), 5(write coil), 6(write register)
//signed int Mb.R[0 to 125] and bool Mb.C[0 to 128] MB_N_R MB_N_C
 
void setup()
{
  uint8_t mac[]     = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
  uint8_t ip[]      = { 192, 168, 1, 8 };
  uint8_t gateway[] = { 192, 168, 1, 1 };
  uint8_t subnet[]  = { 255, 255, 255, 0 };
  Ethernet.begin(mac, ip, gateway, subnet);
  //Avoid pins 4,10,11,12,13 when using ethernet shield
 
  delay(5000);  //Time to open the terminal
 
  //Digital inputs
  Mb.R[0] = 0;
  Mb.R[1] = 0;
  Mb.R[2] = 0;
  Mb.R[3] = 0;
  //digital ouputs  
  Mb.R[4] = 0;
  Mb.R[5] = 0;
  Mb.R[6] = 0;
  Mb.R[7] = 0;
  //analog inputs
  Mb.R[8] = 0;
  Mb.R[9] = 0;
  Mb.R[10] = 0;
  Mb.R[11] = 0;
 
  // bmini pins initialization. Make sure you have installed the BminiIO library for Arduino Ethernet
  bmini_init();
 
}
 
void loop()
{
  int i;
  //modbus TCP update
  Mb.Run();
 
  //reads digital inputs and store them into the first 4 registers
  for(i=1; i < 5; i++){
    Mb.R[i-1] = get_inputs(i);
  }
  //get the relays state from the following 4 registers and update it with the relay function
  for(i=1; i < 5; i++){
   if (Mb.R[3+i] == 1 ){
    relay(i, HIGH);
   }else{
     relay(i, LOW);
   }
  }
  
  //reads analog values and store the integer representation in the following 4 modbus registers
  for(i=1; i < 5; i++){
    Mb.R[7+i] = analogReadPin(i);
  }
 
  delay(10);
}

Pin mapping

The library add a layer on top of the Arduino API letting you safetly use the shield pins without considering the pin mapping. If you need more control over it and use the Arduino functions like digitalRead, digitalWrite and analogRead take a look at pins mapping for Arduino Ethernet.

Note that when using the Bmini shield together with the Arduino Ethernet Board, the PWM pins of the J6 terminal block plug can not be used (even though you can still use J6 +5V and 0V pins).

Licensing

This documentation is licensed under the Creative Commons Attribution-ShareAlike License 3.0. Take a look at Simple-Modbus library web site and Mudbus library website for more information on the libraries' licences.

Personal tools
Namespaces

Variants
Actions
Structure
Store