Arduino的MODBUS协议

时间:2014-03-19 09:46:47

标签: c arduino protocols network-protocols modbus

我有SCADA系统和两个arduino奴隶。 scada系统适用于Modbus协议。数据必须通过modbus协议从SLAVES发送到SCADA系统。让我知道如何在Arduino平台上实现modbus协议。沟通如何发生在这里。请分享arduino UNO的modbus协议接线图。

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

void setup(){
  Wire.begin();
  Serial.begin(9600);
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
  printDate();
  delay(1000);
}

void setDateTime(){

  byte second =  45; //0-59
  byte minute =01; //0-59
  byte hour = 13; //0-23
  byte weekDay =2; //1-7
  byte monthDay =24; //1-31
  byte month = 2; //1-12
  byte year  =       14; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

   int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

 /*  int second = 11;
  int minute =18;
  int hour = 18;
  int weekDay = 1;
  int monthDay = 2;
  int month = 11;
  int year = 12;
*/
  //print the date EG   3/1/11 23:59:59
  Serial.print(monthDay);
  Serial.print("/");


  Serial.print(month);
  Serial.print("/");

  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

 // Serial.print("Week Day Of week");
//  Serial.println(weekDay);


}

通过modbus协议发送的数据:

Serial.print(year);
 Serial.print(month);
 Serial.print(monthDay );
 Serial.print(hour);
 Serial.print(minute);
Serial.println(second);

我正在寻找Arduino的例子

链接 Modbus protocol Arduino link

3 个答案:

答案 0 :(得分:1)

您需要为arduino编写或查找MODBUS通信堆栈(库)。如何使用它的说明应该是该库的一部分。

注意:Modbus是一种通信协议,可以使用不同的物理层"恩。 RS-485,其中一些可能不受Arduino支持。

更多关于modbus: https://en.wikipedia.org/wiki/Modbus 更多关于RS-485: https://en.wikipedia.org/wiki/RS-485

答案 1 :(得分:0)

不确定您正在寻找什么。您可以访问SourceForge并找到任意数量的Arduino兼容库。

如果您正在寻找更像是完整草图的东西,那么这就是整个草图。它是j2mod的一部分,它是一个Java Modbus实现。

https://sourceforge.net/p/j2mod/code/HEAD/tree/trunk/src/sketches/OSS_DAQ/OSS_DAQ.ino

答案 2 :(得分:0)

你可以在arduino上使用Mudbus https://code.google.com/p/mudbus/