This is a new test code i did but it works randomly.
#include <SimpleModbusMaster.h>
//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 300 // the scan rate
#define retry_count 10
// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2
#define LED 9
// The total amount of available memory on the master to store data
#define TOTAL_NO_OF_REGISTERS 1
// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
PACKET1,
PACKET2,
TOTAL_NO_OF_PACKETS // leave this last entry
};
// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];
// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 8; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// Initialize each packet
// modbus_construct(&packets[PACKET1], 1, FORCE_SINGLE_COIL, 0, 0xFF00, 0);
// modbus_construct(&packets[PACKET2], 1, PRESET_MULTIPLE_REGISTERS, 4358, 1, 0);
// Initialize the Modbus Finite State Machine
modbus_configure(&Serial, baud, SERIAL_8N1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
pinMode(LED, OUTPUT);
}
void loop() {
regs[0] = analogRead(0); // update data to be written to arduino slave
analogWrite(LED, regs[0]>>2); // constrain adc value from the arduino slave to 255
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
modbus_construct(&packets[PACKET2], 1, PRESET_MULTIPLE_REGISTERS, 4358, 0, 0);
modbus_update();
//digitalWrite(ledPin, HIGH);
} else {
modbus_construct(&packets[PACKET2], 1, PRESET_MULTIPLE_REGISTERS, 4358, 1, 0);
modbus_update();
// turn LED off:
//digitalWrite(ledPin, LOW);
}
}
This is a new test code i did but it works randomly.
Оживлю немного тему. ничего подходящего не нашел.
Идя, такая. Смотрю что пришло в порт и рересылаю. Все верно ?
Можете помочь с библиотекой для modbus?