Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Library writers >> MODBUS library problem.
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1295917375

Message started by Zap on 01/25/11 at 01:02:55

Title: MODBUS library problem.
Post by Zap on 01/25/11 at 01:02:55
I'm trying to use the arduino MODBUS slave lib from here:
http://sites.google.com/site/jpmzometa/arduino-mbrt/arduino-modbus-slave
Along with the rtu slave pde example from here:
http://sites.google.com/site/jpmzometa/arduino-mbrt/examples

When I try and compile I get several errors, such as 'Serial' was not declared in ModbusSlave.cpp and several others.

Could anyone help with the modifications to the MODBUS lib's to suit wiring?

Thanks Zap.

Title: Re: MODBUS library problem.
Post by barragan on 01/25/11 at 01:21:16
hi I tried and note that modbus_rtu_slave.pde doesn't need to include the library, in fact the code doesn't include it.
Just compile the modbus_rtu_slave.pde file and it will work fine. Do you have another example that uses the library?

Title: Re: MODBUS library problem.
Post by Zap on 01/25/11 at 02:14:04
Thanks , that is correct if you use the non lib version of the MODBUS slave.
However if you try the lib version in the examples (also below) I get thoses errors.

#include <ModbusSlave.h>

/* First step MBS: create an instance */
ModbusSlave mbs;

/* slave registers */
enum {       
        MB_CTRL,        /* Led control on, off or blink */
        MB_TIME,        /* blink time in milliseconds */
        MB_CNT,        /* count the number of blinks */
        MB_REGS            /* total number of registers on slave */
};

int regs[MB_REGS];
int ledPin = 13;
unsigned long wdog = 0;         /* watchdog */
unsigned long tprev = 0;         /* previous time*/

void setup()
{
       
/* the Modbus slave configuration parameters */
const unsigned char SLAVE = 1;
const long BAUD = 115200;
const char PARITY = 'e';
const char TXENPIN = 0;

       /* Second step MBS: configure */
      mbs.configure(SLAVE,BAUD,PARITY,TXENPIN);

        pinMode(ledPin, OUTPUT);
       
}

void loop()
{
        /* Third and las step MBS: update in loop*/
        if(mbs.update(regs, MB_REGS))
                wdog = millis();

        if ((millis() - wdog) > 5000)  {      /* no comms in 5 sec */
                regs[MB_CTRL] = 0;      /* turn off led */
        }       

        /* the values in regs are set by the modbus master */
        switch(regs[MB_CTRL]) {
        case 0:
                digitalWrite(ledPin, LOW);
                break;
        case 1:
                digitalWrite(ledPin, HIGH);
                break;       
        default: /* blink */
                if (millis() - tprev > regs[MB_TIME]) {
                        if (LOW == digitalRead(ledPin)) {
                                digitalWrite(ledPin, HIGH);
                                /* this is how you change your holding registers
                                  so the master can read values from the slave */
                                regs[MB_CNT]++;
                        } else {
                                digitalWrite(ledPin, LOW);
                        }
                        tprev = millis();
                }
        }

}


Thanks Zap.

Title: Re: MODBUS library problem.
Post by barragan on 01/25/11 at 16:08:36
Could you point me to an example that uses the library? those are not included with the library. Thx.

Title: Re: MODBUS library problem.
Post by Zap on 01/28/11 at 04:43:46

I don't know of any, other than the one supplied.

Zap

Title: Re: MODBUS library problem.
Post by barragan on 01/28/11 at 05:51:26
got it, this is a quick fix for 0027, it won't happen on 0028 that will be released soon. On the library there is a file called ModBusSlave.h, open it and edit and add the include line shown after the WProgram.h line:


[code c++]/****************************************************************************
* BEGIN MODBUS RTU SLAVE FUNCTIONS
****************************************************************************/
#include "WProgram.h"
#include "HardwareSerial.h"
[/code]

That's it, try to compile your sketch after that.
let me know.

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.