Loading...
Pages: [1]   Go Down
Author Topic: MAX 485 & ModbusMaster - Basic Guide  (Read 1217 times)
0 Members and 1 Guest are viewing this topic.
United States
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is like my 5th post or something, but I wanted to give back. I didn't see a "guides" section and this doesn't really meet many standards in way of a guide, but I thought it could really help someone get started, especially REALLY new people.


Youtube video showing physical hookup:
https://www.youtube.com/watch?v=P2DYBKIAHQA

Excellent post with schematic:
http://pskillenrules.blogspot.com/2009/08/arduino-and-rs485.html

Modbus Master Library:
http://www.arduino.cc/playground/Code/ModbusMaster

Code:
Code:
#include <ModbusMaster.h>


// instantiate ModbusMaster object as slave ID 2
// This is how to set the serial port, 3 is serial # and 2 is the SLAVE ID of the node
ModbusMaster node(3,2);
const int txRxPin = 4;
int txState = LOW;


void setup()
{
  // initialize Modbus communication baud rate
  node.begin(19200);
  pinMode(txRxPin, OUTPUT);
}


void loop()
{
//creating a counter that transmits "i"
 static uint32_t i;
  uint8_t j, result;
  uint16_t data[6];
 
  i++;

  txState = HIGH;
  digitalWrite(txRxPin, txState);
  // slave: write TX buffer to (2) 16-bit registers starting at register 0
  result = node.writeMultipleRegisters(0, 2);
 
 

}
Essentially this code shows you how to transmit over modbus a 32bit int
« Last Edit: November 22, 2012, 09:14:42 pm by zabaat » Logged

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 71
Posts: 6830
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks pretty simple, better than I would have thought for Modbus but I guess all the clever stiff is in the library.

______
Rob
Logged

Rob Gray aka the GRAYnomad http://www.robgray.com

United States
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

EDIT : The below could be false... checking...


To add receive capability to MAX485 use this link to modify your modbusmaster.cpp file   (file attached)

http://electronics.stackexchange.com/questions/49097/arduino-as-modbus-master-with-max485-doesnt-get-any-response

Code:
    // code edited to work with MAX485:
  // transmit request
  UCSR0A=UCSR0A |(1 << TXC0); 
  Serial.flush();
  digitalWrite(3, HIGH);

  for (i = 0; i < u8ModbusADUSize; i++)
  {
#if defined(ARDUINO) && ARDUINO >= 100
    MBSerial.write(u8ModbusADU[i]);
#else
    MBSerial.print(u8ModbusADU[i], BYTE);
#endif
  }
  while (!(UCSR0A & (1 << TXC0)));
  digitalWrite(3, LOW);
  // --

  u8ModbusADUSize = 0;
« Last Edit: November 21, 2012, 03:47:34 am by zabaat » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I am newbie in arduino. I read this topic but still  I do not know how to use a 0x05 - Write Single Coil function.  Could anyone please explain, how to send (for example 01 05 00 00 FF 00 8C 3A)  the commands using the ModbusMaster library after the condition in the program?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Zabaat good job. Have known tons of info from you .Thanks
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm starting a project with serial communication between arduinos on ModBUS protocol. The master library that Zabaat is using (click) seems to me the best choice of all libraries available out there, but I have problem with it. I can not compile it for Leonardo or for DUE board. I will be using DUE as a master device, but I get following errors:

If I try to compile for DUE:
Code:
In file included from "sketch_name.ino":1:
C:\Users\...\Arduino\libraries\ModbusMaster/ModbusMaster.h:83: fatal error: util/crc16.h: No such file or directory
compilation terminated.

If I try to compile for Leonardo:
Code:
C:\Users\...\Arduino\libraries\ModbusMaster\ModbusMaster.cpp:36: error: conversion
 from 'Serial_' to non-scalar type 'HardwareSerial' requested
C:\Users\...\Arduino\libraries\ModbusMaster\ModbusMaster.cpp: In member function 'void
 ModbusMaster::begin(uint16_t)':
C:\Users\...\Arduino\libraries\ModbusMaster\ModbusMaster.cpp:139: error: no match for
 'operator=' in 'MBSerial = Serial'
C:\Users\...\Arduino\"install folder of Arduino"\arduino-1.5.2\hardware\arduino\avr\cores\
 arduino/HardwareSerial.h:33: note: candidates are: HardwareSerial& HardwareSerial::
 operator=(const HardwareSerial&)

If I try to compile it for Uno, Duemilanove, Nano, Mega, Mega 2560... the arduino compile it without problems. I didn't find any compatibility list for this library, but I think that DUE and Leonardo boards aren't supported from this library. Have anyone working modbus master-serial communication for DUE board? Can I somehow change code, so it would work with DUE too? If this library doesn't work with Due or leonardo, can someone give me an advice, what library for master-slave communication between DUE and few Leonardo's should I use? Is there any good guide on how to setup all things? Also later I will try to communicate with industrial frequency inverters trought ModBUS, any suggestions (I'm using Schneider inverters).

PS: I'm using example code from the same zip file I downloaded library.
Example code:
Code:
#include <ModbusMaster.h>

// instantiate ModbusMaster object as slave ID 2
// defaults to serial port 0 since no port was specified
ModbusMaster node(2);

void setup()
{
  // initialize Modbus communication baud rate
  node.begin(19200);
}

void loop()
{
  static uint32_t i;
  uint8_t j, result;
  uint16_t data[6];
 
  i++;
 
  // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  node.setTransmitBuffer(0, lowWord(i));
 
  // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  node.setTransmitBuffer(1, highWord(i));
 
  // slave: write TX buffer to (2) 16-bit registers starting at register 0
  result = node.writeMultipleRegisters(0, 2);
 
  // slave: read (6) 16-bit registers starting at register 2 to RX buffer
  result = node.readHoldingRegisters(2, 6);
 
  // do something with data if read is successful
  if (result == node.ku8MBSuccess)
  {
    for (j = 0; j < 6; j++)
    {
      data[j] = node.getResponseBuffer(j);
    }
  }
}

Thank you very much for all replies!

Jakob
« Last Edit: April 21, 2013, 01:13:06 pm by JUGmobile » Logged

Pages: [1]   Go Up
Print
 
Jump to: