Git Product home page Git Product logo

simplemodbusng's Introduction

SimpleModbus NG

SimpleModbus is a collection of Arduino libraries that enables you to communicate serially using the Modicon Modbus RTU protocol.

This project was born as an updated version of http://code.google.com/p/simple-modbus/ by Bester Juan because it lacks support for commands other than 3 and 16. More important the code is now on github, so you can contribute more easily.

This projects is actively maintained, so feel free to ask for features or reporting bugs!

Features

This library adds support for command 6 and provides a more extensive support for arduino pins. The goal of the project is to support all usable MODBUS commands on arduino and expose all arduino pins so you can use an arduino as an advanced automation controller for both analog/digital in/out.

NEW: Support for SoftwareSerial, really useful on AtTiny85. You can find both library and an example that works reliable on attiny85 microcontroller.

Usage

Simply copy the SimpleModbusMaster or SimpleModbusSlave or both into your Arduino IDE libraries folder. Than restart the ide and open the corresponding example into the example_master or example_slave folder.

simplemodbusng's People

Contributors

angeloc avatar brainelectronics avatar krosik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simplemodbusng's Issues

Examples for Slave are not working, missing lowLatency argument

Hi

in both Slave and SoftwareSerialSlave examples is error, because in
modbus_configure(115200, 1, 2, TOTAL_REGS_SIZE);

is mising last argument for

void modbus_configure(long baud, unsigned char _slaveID, unsigned char _TxEnablePin, unsigned int _holdingRegsSize, unsigned char _lowLatency)

Also this argument is not explained in examples or comments. I believe it should be 0 or 1, maybe better boolean true or false, but I do not understand what is exact use of it.

thx

updating register in slave with external master

Hi,

I have been trying to set up a Modbus RTU communication between a C# program (with another Modbus library) as a master and an arduino as as slave. I do so through a USB to RS485 cable. The arduino is connected to a MAX485 IC.

The goal of the project is for the slave to receive a few registers from the master, do some calculations with it and make the outcome available in another register for the master to read again.

The master can flawlessly read data from registers on the slave, I'm however not really grasping how my slave can receive and store bytes from the master.

#include <SimpleModbusSlave.h>

const int serialNumber = 7999;
const int securityCode = 999;

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

void setup()
{
  modbus_configure(&Serial, 115200, SERIAL_8N1, 3, 2, HOLDING_REGS_SIZE, holdingRegs);
  holdingRegs[0] = serialNumber; 
  holdingRegs[2] = securityCode; 
  holdingRegs[4] = 0; //I'd like this to become a value sent by the master
}

void loop()
{
modbus_update();

}

As I said, I have no issue reading serialNumber and securityCode when I request this data using function code 3 with this code.

Let's say now I want to send an integer from the master to the slave, let's say decimal 32000 (0x7D 0x00). I'd like this number to be available in holdingRegs[4] and holdingRegs[5]. My masters' library is limited to function code 16 and will always write a minimum of 32 bits (4 bytes).

This is what my masters' library outputs:
0x03: (slave ID)

0x10: (function code 16 - write multiple registers)
0x00:

0x04: (2 bytes to specify I want to start writing at register 4)
0x00:

0x02: (2 bytes to specify there will be data for 2 registers (or more specifically 1 32-bit register)

0x04: 4 bytes to come

0x00:
0x00: first register (16 bits) is 0

0x7D:
0x00: value 32000

0xD8
0xB4: CRC

Hopefully, My understanding is not too wrong yet with this logic.

Here is where it probably goes wrong: I was under the impression that modbus_update(); would update holdingRegs[4] and holdingRegs[5] as soon as the function 16 from the master was received. However, when I read those registers later, they still have the original value 0.

I think I'm missing something very obvious. Any help would be greatly appreciated!
Thanks,
Jens

Modbus ASCII

Can simplymodbusmaster be used to communicate with a MODBUS ASCII slave device?

Typo in SimpleModbusSlaveSoftwareSerial.cpp

Hello angeloc,

Just wanted to thank you for your efforts, I was looking at your example code this afternoon, when i noticed a typo in the header declaration of your example file "SimpleModbusSlaveSoftwareSerial.cpp"

The second line should read

SimpleModbusSlaveSoftwareSerial.h

currently it reads as

include "SimpleModbusSlaveSs.h"

Best regards!

Tom

Timeout while writing multiple holding register

When i am trying to write 4 holding register code is working fine but when i try to write 5 holding register i am getting error in modscan which say modbus message timeout .

Can you please help me with same ?

I'm a stupid. I know it, but...

Please......... somebody help me.
I start 2 weeks ago to try all the existing examples for serial communication arduino modbus.
No-one example work.
I just want to compile a sketch modbus slave on my arduino Uno and control the slave's answers.
Now I read only illegal response or break connection.
I used ModbusPoll or Mtester with USB/RS485 cable converter and a shield RS485 on my arduino.
Thanks in advance.

get nothing implementation in PM1200

hallo ....

i was try in PM710 and working properly.

and i was try in another PM, like PM1200 and PM2120 i get nothing....

help me please....

#update
its clearly work, but how to convert data signet to float data from PM....

'packetPointer' does not name a type

Build options changed, rebuilding all
SimpleModbusMasterExample.ino:110:1: error: 'packetPointer' does not name a type
SimpleModbusMasterExample.ino:111:1: error: 'packetPointer' does not name a type
SimpleModbusMasterExample.ino: In function 'void setup()':
SimpleModbusMasterExample.ino:120:3: error: 'packet1' was not declared in this scope
SimpleModbusMasterExample.ino:127:3: error: 'packet2' was not declared in this scope
SimpleModbusMasterExample.ino:136:98: error: invalid conversion from 'int' to 'HardwareSerial*' [-fpermissive]
SimpleModbusMasterExample.ino:136:98: error: invalid conversion from 'Packet*' to 'unsigned char' [-fpermissive]
SimpleModbusMasterExample.ino:136:98: error: too few arguments to function 'void modbus_configure(HardwareSerial*, long int, unsigned char, long int, long int, unsigned char, unsigned char, Packet*, unsigned int, unsigned int*)'
In file included from SimpleModbusMasterExample.ino:1:0:
/Users/Nilanj/Documents/Arduino/libraries/SimpleModbusMasterV2rev2/SimpleModbusMaster.h:134:6: note: declared here
void modbus_configure(HardwareSerial* SerialPort,
^
SimpleModbusMasterExample.ino: In function 'void loop()':
SimpleModbusMasterExample.ino:143:57: error: too many arguments to function 'void modbus_update()'
In file included from SimpleModbusMasterExample.ino:1:0:
/Users/Nilanj/Documents/Arduino/libraries/SimpleModbusMasterV2rev2/SimpleModbusMaster.h:125:6: note: declared here
void modbus_update();
^
SimpleModbusMasterExample.ino:143:57: error: void value not ignored as it ought to be
SimpleModbusMasterExample.ino:155:13: error: 'packet1' was not declared in this scope
SimpleModbusMasterExample.ino:158:13: error: 'packet2' was not declared in this scope
Error compiling.

PlatformIO and Error counter in Master.

Hello Thank you for your bookstore.
I have a problem with PlatformIO.
if in platformio.ini I add the line:
lib_deps =
https://github.com/angeloc/simplemodbusng.git

It just downloads the SimpleModbusMaster folder for me.

And since I am.
Is there a way to reset or reset the error counter?
Since when it reaches the account the Master stops making requests and I can't find a way to restart the account, for that reason disable that function in the library.
.
I hope you can solve it.
Thanks.

[Question] about Serial Ports

Heya, I got a question regarding this library

does the master modbus library completely doesn't work with serial software? I've seen a youtube video of a guy that manages to do this, but i'm not so sure how.

I'm currently trying to setup my arduino Nano as a master, connected to a PM2120. Whilst i could easily use the built-in serial, I can't since i'll be using the only Nano's built-int serial to monitor the results in a PC.

Is there a way so i can specify to the library which ports i want to use? thanks!

Use the Stream class to allow other Serial interfaces

Iam using this library with arduino micro and I hace modified it in order to use the Serial1 instead of the Serial (emulated USB). It would be nice if we can select the serial in the constructor using the Stream class

Arduino Fio Compatible

Dear people, I tested this library under Arduino Fio, unfortunetely, if you have more than 4 slaves, they fail to comunicate.
I tested the same with Nano, without any problems, but Fio seems to need further adjusments in the library, maybe because of its 8mhz clock. Please, if you have any update or work around let me know.
Regards,
Oscar

Lovato DMG 610

Hello! I`m new with arduino. I need to make an Lovato DMG 610 power meter to communicate with my arduino mega 2560. I downloaded modbus library and tried but nothing. Can someone help me please with this problem? I have Arduino Mega 2560, LinkSprite RS485 Shield and Lovato DMG 610 power meter.
I attach the power meter datasheet. Power meter is set to default.
I317IGB10_17.pdf

Thanks!

Modbus RTU slave: auto baud rate detection

Hi,

is possible to use auto baud detection for Modbus RTU slave?

Something like this: https://gist.github.com/tuxmartin/46ca107057c650fbe6f63dcae5ee067d

My idea:

  • Modbus Master (Raspberry Pi) set serial port speed to X.
  • ? (Modbus Master send broadcast message for synchronization every X seconds.) ?
  • Modbus Slave (Arduino) run detection of speed every X seconds.
  • Modbus Master periodically read data from all slaves - if any of slaves do not respond, slow down serial speed.

Thanks.

Attiny85 as slave

Hi

I want use 50 attiny's as slave for a school project, but an attiny only has 2 digital outputs, which you use for the softwareserial, but you also need a 3th output for your max485?
image

how have you done this?

Regards
Floris

how can binary data show

need a help

show serial monitor binary data

#include <SimpleModbusMaster.h>
#define baud 9600 //baudrate modbus slave
#define timeout 1000
#define polling 100 // the scan rate
#define retry_count 1000
#define TxEnablePin 2

enum
{
  PACKET3,
  TOTAL_NO_OF_PACKETS
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet3 = &packets[PACKET3];//
BIN readmodul[125];// read 32016-32040



void setup()
{
  Serial.begin(9600);
 
  packet3->id = 1;
  packet3->function = READ_HOLDING_REGISTERS;
  packet3->address = 0077;
  packet3->no_of_registers = 35;
  packet3->register_array = readmodul;

  modbus_configure(baud, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
 

}

void loop()
{
  modbus_update(packets);
  
  //int read33 = readmodul[33];
   Serial.println (readmodul[33]); 
   Serial.print ("DATA  ");
//   Serial.println (read33);

delay(2000);  
}

Arduino Modbus master and raspberry pi as slave

@angeloc I am trying to implement the same thing written in the title. Using pymodbus for raspberry pi. But having problem and getting some error. Serial monitor on arduino is printing random garbage values. Can anyone tell me properly how simplemodbusmsater library works and explain me example code. Is the library made to print the holding register values we get from slave device(raspberry pi)? I found serial.read and serial.write commands for frame[buffer] in .cpp file so thats how i concluded that it can print the register values on monitor. But maybe i am wrong. So please if anyone call tell me how the library actually works that would be helpful.

malfunction in a bus with high trafic

Hi to everyone!
Is my first time doing a contibuition in github. Sorry if here is not the place to do it.
I have tested the library in using a Attiny board. The application was a remote thermometer, so with this tiny board I had to interrogate a DS18B20 and to implement the Modbus slave.
It took me the 92% of the total memmorie but I reach the objetive.
When I comunicate with de slave in a bus with just this slave everithing goes rigth. The problem apeared when I tried to included in crouded bus, where the master interrogate almos the 100% of the time.
I made a little change in the code of the librarie and now is working OK.
I would like to share with you the solution.

The problem is the implementation of the timeout T1_5 in modbus_update() function.
The original code is:

  while (mySerial.available())
  {
    // The maximum number of bytes is limited to the serial buffer size of 128 bytes
    // If more bytes is received than the BUFFER_SIZE the overflow flag will be set and the 
    // serial buffer will be red untill all the data is cleared from the receive buffer.
    if (overflow) 
      mySerial.read();
    else
    {
      if (buffer == BUFFER_SIZE)
        overflow = 1;
      frame[buffer] = mySerial.read();
      buffer++;
    }
    delayMicroseconds(T1_5); // inter character time out
  }

Here the "time out" is not implemented, insted a delay off the entire timeout is included.
I have conclude this is the reason why in the crowded bus this slave can not follow the trafic.
So I remplaced the code by follows. It now use a 94% inted of 92% of total memmorie

    unsigned long timeOut = micros();
     while ((*_port).available() || (timeOut+T1_5 <= micros())) {
      if((*_port).available()) {
         timeOut = micros();
          // The maximum number of bytes is limited to the serial buffer size of 128 bytes
          // If more bytes is received than the BUFFER_SIZE the overflow flag will be set and the
          // serial buffer will be red untill all the data is cleared from the receive buffer.
          if (overflow)
              (*_port).read();
          else {
              if (buffer == BUFFER_SIZE)
                  overflow = 1;
              frame[buffer] = (*_port).read();
              buffer++;
          }
      }
    }


Thank you for the librarie!!!






Multiple Slaves

Hi All,

I need to read register from multiple slaves on the bus, how can I achieve this maximising processing power? I will have a maximum of 16 Slaves with no more than 10 registers in each, these will also be Coils or Discrete only as I'm monitoring/controlling digital states, Would obviously like to poll each device write and return data for all 16 Slaves on the bus.

Regards

Jono

Compiling with esp8266

I´ve been using this library with arduino mega, it works very good, but now I want to use it with esp 8266 and the arduino IDE gives me an error ¨error compiling for esp 8266¨, this is because this library does not work with esp8266??

wiring with arduino as slave and by RS485 to master?

hi, I appreciate if someone could explain wiring arduino with other device (master) by RS485 or RS232, when using code from this library?

I understand code provided here can act as slave for some modbus rtu master simulation on PC, but what about using it with other devices (PLC, HMI, another arduino)?

Accessing 100 Holding registers

Hello,
I am trying to access 100 Holding registers, can't get past 28 registers. Not able to identify the problem.
Have tried with different levels of delay. Platform, Arduino via MAX485 ic, pin 2 as tx enable.
baud 9600, parity even, 8 bits.
start.
a. modbus_construct
b. delay(100)
c. modbus_update()
d. read registers
e. delay(25)
f. delay(1)
back to loop

SimpleModbusSlave.h: No such file or directory

Hi...sorry sir, I want to ask why when compiling I get the error "SimpleModbusSlave.h: No such file or directory"?. Even though I have installed this library into the Arduino IDE on my computer.

TXEnable not working?

The Library works fine when i disable the txEnable Pin.
for tests i use this Shield
https://joy-it.net/de/products/ARD-RS485

Here I can select the DirectionControl with Jumper to PIN D9 or TX_CTRL. When i now use
modbus_configure(19200, 2, 9, TOTAL_REGS_SIZE, 0); with D9 it doesn't work anymore.

With
modbus_configure(19200, 2, 0, TOTAL_REGS_SIZE, 0);
and the jumper to TX_CTRL it works fine.

What is wrong? Why TxEnable Pin not working?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.