07-02-2020, 11:55
Witam, mam program który działa, wymienia dane z PC lecz mam pewne pytanie. Czy używając tej bilioteki mogę zmienić adres tego urządzenia slave jeżeli będzie spełniony warunek?
Chciałbym to zrobić tak iż wysyłam 1 na rejest 40000 i zapala się dioda oraz zmienia się adres urządzenia slave przez co trace połączenie z masterem (PC) czy w ogóle jest to możliwe?
Chciałbym to zrobić tak iż wysyłam 1 na rejest 40000 i zapala się dioda oraz zmienia się adres urządzenia slave przez co trace połączenie z masterem (PC) czy w ogóle jest to możliwe?
Kod:
#include "ModbusRtu.h"
#include "LiquidCrystal.h"
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define led1 8 //Define as 2 led1
#define led2 9 //Define as 5 led2
#define TXEN 7 // assign the Arduino pin that must be connected to RE-DE RS485 transceiver
// data array for modbus network sharing
uint16_t au16data[16] = {
0, 0, 0, 0, 0, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };
Modbus slave(1,0,TXEN); // this is slave @1 and RS-485
void setup() {
lcd.begin(16,2); //Lcd set in 16x2 mode
lcd.print("RS-485 Modbus"); //Welcome Message
lcd.setCursor(0,1);
lcd.print("Arduino Slave");
delay(5000);
lcd.clear();
pinMode(led1,OUTPUT); //Led1 set as OUTPUT
pinMode(led2,OUTPUT); //Led2 set as OUTPU
slave.begin( 19200 ); // baud-rate at 19200
}
void loop() {
slave.poll( au16data, 3 );
if (au16data[0] == 0) //Depends upon value in au16data[0] written by Master Modbus
{
digitalWrite(led1,LOW); //LED OFF if 0
lcd.setCursor(0,0);
Modbus slave(1,0,TXEN);
lcd.print("L1:OFF");
}
else
{
digitalWrite(led1,HIGH);
//LED ON if value other than 0
lcd.setCursor(0,0);
Modbus slave(2,0,TXEN);
lcd.print("L1:ON");
}
if (au16data[1] == 0) //Depends upon value in au16data[1] written by Master Modbus
{
digitalWrite(led2,LOW); //LED OFF if 0
lcd.setCursor(8,0);
lcd.print("L2:OFF");
}
else
{
digitalWrite(led2,HIGH); //LED ON if value other than 0
lcd.setCursor(9,0);
lcd.print("L2:ON");
}
int pwm = au16data[2]; //Depends upon value in au16data[1] written by Master Modbus
lcd.setCursor(0,1);
lcd.print("Servo angle:");
lcd.print(pwm); //Prints Angle in 16x2 LCD display.
delay(200);
lcd.clear();
}