osman_cyp
Published © GPL3+

Modbus RTU SD Read & Write with

Modbus iletişimi ve kaydedicileri SD karta kaydetmek için tasarlanmış ilk proje.

AdvancedProtip11,900
Modbus RTU SD Read & Write with

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
LED (generic)
LED (generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Through Hole Resistor, 120 ohm
Through Hole Resistor, 120 ohm
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
MicroSD Module (Generic)
×1
Cable, USB to RS485
Cable, USB to RS485
×1
Pushbutton Switch, Push-Pull
Pushbutton Switch, Push-Pull
×1

Software apps and online services

Arduino IDE
Arduino IDE
Modbus_Software
iletişim için gerekli olan program.

Story

Read more

Schematics

Circuit SD Read Write with Modbus

Code

MOD_BUS_RTU_Slave.ino

Arduino
//RS-485 Modbus Slave (Arduino UNO)
//Circuit OZOX

#include<ModbusRtu.h>       //Library for using Modbus in Arduino
#include <Servo.h>          //Library for using Servo Motor
#include <SD.h>             //Library for using SD Card
#include <SPI.h>            //Library for using SPI IO

#define led1 2              //Define as 2 led1 
#define led2 5              //Define as 5 led2
#define led3 7              //Define as 7 led3 
#define led4 8              //Define as 8 led4 
#define but 3               //Define as 3 but as Button 

#define CS_PIN 9            //Define as 9 CS pin for SPI

File myFile;

Servo servo;                         //Initilize servo object for class Servo
Modbus bus;                          //Define Object bus for class modbus 
uint16_t modbus_array[10];           //Array initilized
const int chipSelect = 10;
int btnSta = 0;
int myFi[10];
String s = " ";
String myf[10];
  
void setup()
{
  pinMode(led1,OUTPUT);           //Led1 set as OUTPUT
  pinMode(led2,OUTPUT);           //Led2 set as OUTPUT
  pinMode(led3,OUTPUT);           //Led2 set as OUTPUT
  pinMode(led4,OUTPUT);           //Led2 set as OUTPUT
  pinMode(9, OUTPUT);
  pinMode(3, INPUT);
  
  SD.begin(CS_PIN);
  Serial.begin(9600);

  myFile = SD.open("test.txt", FILE_READ);
  if(myFile)
  {
    for(int i=0;i<10;i++)
    {
    myFi[i]=myFile.parseInt();
    modbus_array[i] = myFi[i];
    } 
   myFile.close();
  }
  else{
    for(int i=0;i<10;i++)
    {
    modbus_array[i] = 0x00;
    }
    Serial.print("File didnt open...");
    delay(20);
  }
  
  servo.attach(6);                //Servo PWM pin 6
  bus = Modbus(1,1,4);            //Modbus slave ID as 1 and 1 connected via RS-485 and 4 connected to DE & RE pin of RS-485 Module 
  bus.begin(9600);                //Modbus slave baudrate at 9600
}

void loop()
{
   bus.poll(modbus_array,sizeof(modbus_array)/sizeof(modbus_array[0]));       //Used to receive or write value from Master 
  
  if (modbus_array[0] == 0)
  {
    digitalWrite(led1,LOW);
  }
  else
  {
     digitalWrite(led1,HIGH);
  } 

 if (modbus_array[1] == 0)
  {
    digitalWrite(led2,LOW);
  }
  else
  {
     digitalWrite(led2,HIGH);
  }
   if (modbus_array[2] == 0)
  {
    digitalWrite(led3,LOW);
  }
  else
  {
     digitalWrite(led3,HIGH);
  }
   if (modbus_array[3] == 0)
  {
    digitalWrite(led4,LOW);
  }
  else
  {
     digitalWrite(led4,HIGH);
  }
  int pwm = modbus_array[4];
  servo.write(pwm);
  
  btnSta = digitalRead(but);
  if(btnSta==LOW)
  {
  }
  else
  {
      SD.remove("test.txt");
      delay(10);
      write_(myFile);
      //oku(myFile)); // Eer dosyay okumak isterseniz.
  }
  delay(250);
  }

  void write_(File ff)
  {
    String dd="";
       ff = SD.open("test.txt", FILE_WRITE);
       if(ff)
       {
    for(int i=0;i<10;i++)
    {
    myFi[i] = (int)modbus_array[i];
    dd += myFi[i] + s;
    }
    ff.print(dd);
    ff.close();
     Serial.print("Yazlan deerler: " + dd);
       }
       else
       {
        Serial.print("File didnt open..");
        delay(20);
       }
  }

    void read_(File ff)
  {
    String gg="";
    ff = SD.open("test.txt", FILE_READ);
    if(ff)
    {
    for(int i=0;i<10;i++)
    {
    myFi[i] = ff.parseInt();
    }
    for(int i=0;i<10;i++)
    {
      gg += myFi[i] + s;
    }
    ff.close();
    Serial.print("Okunan deerler: " + gg);
    }
    else
    {
        Serial.print("File didnt open..");
        delay(20);
    }
  }

Credits

osman_cyp

osman_cyp

1 project • 0 followers

Comments