Modbus device simulation
Moderators: adafruit_support_bill, adafruit

Modbus device simulation

by Chillermec on Wed Mar 27, 2013 7:04 pm

I am considering a project to use Arduino as a simulator in place of a variable frequency drive. The Arduino will be a slave device. A master device is reading and writing to the locations as specified in the vfd manufacturers documentation. For the purposes of simulation, I would read the status of a digital input as an example at location XXXXX. I would also need to write values, but a DI state will be a 1st test. My question is can I assign in some manner the location from the manufacturers documentation to equate to the Arduino digital in 1 so that the master finds the data at the location it is expecting? The master controller is pre-programmed to read / write to specific locations for specific slave devices is why this conversion is needed.

Thanks for any suggestions
Chillermec
 
Posts: 23
Joined: Sun Mar 06, 2011 4:04 pm

Re: Modbus device simulation

by adafruit_support_mike on Wed Mar 27, 2013 11:12 pm

Hokay.. the acronym and field-specific vocabulary density is kind of high there. I'm sure you know what you're talking about, but I can't say anything useful about the manufacturer's specs until I know what device and what manufacturer.

I can extract enough meaning to understand that you want to simulate a motor controller in a way that works with the Modbus programmable logic controller protocols, but beyond that I need you to break it down into "civilians and people who wear ties" language.
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 9133
Joined: Thu Feb 11, 2010 1:51 pm

Re: Modbus device simulation

by Chillermec on Sat Apr 20, 2013 2:35 pm

Mike, I appreciate your reply. I am really a novice at programming, so I am sure it was hard for me to ask the right questions in my original post. Lets change the question. I found link to Modbus Slave library on Google code. I found it while reading a post where it was being used successfully. When I tried using the library the compile failed. I think the issue is at the beginning of the code, so, I am only listing the beginning of code. Error is 'modbusDevice' does not name a type. The same error pops up regarding the slave the 2nd line down from 1st error.

Advice?

#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>

/*
This example code shows a quick and dirty way to get an
arduino to talk to a modbus master device with a
device ID of 1 at 9600 baud.
*/

//Setup the brewtrollers register bank
//All of the data accumulated will be stored here

modbusDevice regBank; <<<<<<<<------------ Error location
//Create the modbus slave protocol handler
modbusSlave slave; <<<<<<<<------------ Error location

void setup()
{

//Assign the modbus device ID.
regBank.setId(1);
Chillermec
 
Posts: 23
Joined: Sun Mar 06, 2011 4:04 pm

Re: Modbus device simulation

by adafruit_support_mike on Sat Apr 20, 2013 11:23 pm

That gives me a better idea of what you're looking for.. thanks.

The error you're seeing tells me that the Arduino IDE (Integrated Development Environment.. basically a word processor that can talk to the Arduino) can't find the Modbus libraries.

That's a problem which catches people new to the Arduino all the time.

The code you downloaded from Google Code should go into a folder named 'libraries' in your Arduino IDE 'sketchbook' folder. If you don't know where that is, click 'Preferences' under the 'Arduino' menu and you should get a panel that gives you the file path.

We have more information about libraries.. what they are and how to use them with the Arduino.. here: http://learn.adafruit.com/arduino-tips- ... -libraries
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 9133
Joined: Thu Feb 11, 2010 1:51 pm

Re: Modbus device simulation

by Chillermec on Mon Apr 22, 2013 5:17 am

Mike, that was the solution. The folder was in the libraries directory, but, for some unknown reason was not functioning. I deleted, and extracted again from the zip. The code works now. Thanks very much for clearing me up on that.
Chillermec
 
Posts: 23
Joined: Sun Mar 06, 2011 4:04 pm

Re: Modbus device simulation

by adafruit_support_mike on Mon Apr 22, 2013 10:02 am

Good to hear it's working for you.

Based on this comment:

Code: Select all | TOGGLE FULL SIZE
//Setup the brewtrollers register bank

I'll guess you're working on a home brewing rig? If so, I'm pretty sure the community will want pictures and progress reports. Geek culture tends to see "making beer" as one of the best ways to apply technology. ;-)
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 9133
Joined: Thu Feb 11, 2010 1:51 pm

Re: Modbus device simulation

by Chillermec on Tue Apr 23, 2013 5:19 am

Actually that feature was already a part of the library. I thought it was a requirement to crack open a few beers while working with the Arduino. Seems like a good idea to me!

Cheers,
Chillermec
 
Posts: 23
Joined: Sun Mar 06, 2011 4:04 pm

Re: Modbus device simulation

by Chillermec on Sun Aug 18, 2013 11:05 am

Greetings all. It has been a busy summer, so just getting back into this project. I have been able to test my Arduino with a modbus utility to confirm I can read and write values to and from the Arduino over serial with usb cable. I am ready to set up communications over RS 485 with Max 485 chip with the board attached to a master. Of all the projects I have viewed on line, I have not found a good explanation of the code required regarding putting the 485 chip in the transmit and receive mode from digital pin. I understand I need to switch between high and low signal, but, the code part is what is elusive. Is there a generic method of triggering when to write these values? It seems I should set the chip to receive mode initially, and after receiving request from the master, then set the chip to transmit mode, but not sure what signals when to toggle back and forth.
Any suggestions would be greatly appreciated!

Cheers,
Chillermec
 
Posts: 23
Joined: Sun Mar 06, 2011 4:04 pm

Re: Modbus device simulation

by Chillermec on Sun Aug 18, 2013 12:35 pm

My code is posted below, if that will help....

Code: Select all | TOGGLE FULL SIZE
#include <modbus.h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>




/* PINS
Add more registers if needed
Digital input pins 2,3,4,5,6,7
Digital output pins 8,9,12,13
Analog output pins 10,11 (PWM)
Analog input pins 0,1,2,3,4,5
*/


modbusDevice regBank;
modbusSlave slave;

int AI0,AI1,AI2,AI3,AI4,AI5;
int val1 = 0;  // This is to scale AI1 value from 20% to 100%.  See map function below.
int val2 = 0;  // This is to scale AI2 value from 20% to 100%.  See map function below.   


void setup()
{   
  regBank.setId(1); ///Set Slave ID 1
 
 
// Add control word register banks
  regBank.add(00001);  // ABB Off 1 bit 0
  regBank.add(00002);  // ABB
  regBank.add(00003);  // ABB
  regBank.add(00004);  // ABB
  regBank.add(00005);  // ABB
  regBank.add(00006);  // ABB
  regBank.add(00007);  // ABB
  regBank.add(8);  // ABB            Note, with leading 0's error in compile
  regBank.add(9);  // ABB            Note, with leading 0's error in compile
  regBank.add(00010);  // ABB
  regBank.add(00011);  // ABB
  regBank.add(00012);  // ABB
  regBank.add(00013);  // ABB
  regBank.add(00014);  // ABB
  regBank.add(00015);  // ABB
 
 
//Add Digital Input registers
  regBank.add(10034);  // ABB DI2 input
  regBank.add(10035);  // ABB DI3 input
  regBank.add(10036);  // ABB DI4 input
  // regBank.add(10005);  //commented out of original code
  // regBank.add(10006);  //commented out of original code
  //regBank.add(10007);   //commented out of original code
   
 

//Analog input registers
  regBank.add(30001);  // ABB AI1
  regBank.add(30002);  // ABB AI2
  // regBank.add(30003);  //commented out of original code
  // regBank.add(30004);  //commented out of original code
  // regBank.add(30005);  //commented out of original code
  // regBank.add(30006);  //commented out of original code
 
// Drive command and feedback registers
     regBank.add(40001);  // Drive stop_start and reset command
     regBank.add(40002);  // Drive speed command from MCB
     regBank.add(40103);  // Drive speed feedback to MCB
     regBank.add(40305);  // Fault status from drive to MCB
     
     
//Analog Output registers //commented out of original code
  // regBank.add(40010);  //commented out of original code
  // regBank.add(40011);  //commented out of original code

  slave._device = &regBank; 
  slave.setBaud(19200);             // Set to 19,200 for VFD comm
 
 
 
 

 
 
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
  pinMode(6,INPUT);
  pinMode(7,INPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(12,OUTPUT); 
  pinMode(13,OUTPUT); 
   
}
void loop(){

  while(1){   
  //Digital Input
    byte DI2 = digitalRead(2);
    if (DI2 >= 1)regBank.set(10034,1);  // ABB DI2
    if (DI2 <= 0)regBank.set(10034,0);  // ABB DI2
    byte DI3 = digitalRead(3);
    if (DI3 >= 1)regBank.set(10035,1);  // ABB DI3
    if (DI3 <= 0)regBank.set(10035,0);  // ABB DI3
    byte DI4 = digitalRead(4);
    if (DI4 >= 1)regBank.set(10036,1);  // ABB DI4
    if (DI4 <= 0)regBank.set(10036,0);  // ABB DI4
   
                               
 
           
  //Analog input  ***READ Twice deliberately
   
   
    AI1 = analogRead(1);  // ABB AI1
    delay(10);
    AI1 = analogRead(1);
    val1 = map (analogRead (1),0,1023,20,100) ;
    regBank.set(30001, (word) val1);
    delay(10);
   
   
    AI2 = analogRead(2);  // ABB AI2
    delay(10);
    AI2 = analogRead(2);
    val2 = map (analogRead (2),0,1023,20,100) ;
    regBank.set(30002, (word) val2);
    delay(10);
   
   
     //Analog output
    word AO10 = regBank.get(40010);
    analogWrite(10,AO10);
    delay(10);
    word AO11 = regBank.get(40011);
    analogWrite(11,AO11);
    delay(10);
       
  slave.run(); 
  }
}
Chillermec
 
Posts: 23
Joined: Sun Mar 06, 2011 4:04 pm

Re: Modbus device simulation

by adafruit_support_mike on Mon Aug 19, 2013 3:21 pm

If you're looking for information about driving the chip, you can get that from the datasheet: http://datasheets.maximintegrated.com/e ... MAX491.pdf

If you're looking for information about handling modbus data transactions, your best bet will be to go to the specs for that: http://www.modbus.org/specs.php (requires agreement to a EULA, but the terms aren't especially heinous).
When you void a product warranty, you give up your right to sue the manufacturer if something goes wrong and accept full responsibility for whatever happens next. And then you truly own the product.
User avatar
adafruit_support_mike
 
Posts: 9133
Joined: Thu Feb 11, 2010 1:51 pm