How to the code?
Please help me
I am new user and ı don't upload code.
I want see Arduino serial port ( Bms Battery voltage, Battery Temparature, SoC ...
try this, I think it matches your detailed needs
void setup() {}
void loop() {}
...
This is not a free programming service. We help you if you have a problem but we don't do your complete project. So start with an easier task, learn how to write code for the Arduino. Later try to solve above problem yourself and if you don't succeed post the code you wrote and describe what problem you still have. We will then try to help you.
#include <ModbusMaster.h>
/*!
We're using a MAX485-compatible RS485 Transceiver.
Rx/Tx is hooked up to the hardware serial port at 'Serial'.
The Data Enable and Receiver Enable pins are hooked up as follows:
*/
//int MAX485_RE_NEG = 8;
//int MAX485_DE = 7;
#define MAX485_RE_NEG 9
#define MAX485_DE 7
// instantiate ModbusMaster object
ModbusMaster node; // Se llamará ModbusMaster como node
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1); // Es el modo de transmisión
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0); // Es el modo de recepción
digitalWrite(MAX485_DE, 0);
}
void setup()
{
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Empieza en modo de recepción
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
// Modbus communication runs at 19200 baud
Serial.begin(19200, SERIAL_8O1); // Acá se define la paridad: 8bits, odd parity, 1 bit de parada
// Modbus slave ID 1
node.begin(1, Serial);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
void loop()
{
uint8_t ResultadoLectura;
uint8_t ResultadoEscritura;
ResultadoEscritura = node.writeSingleRegister(0x40103, 8); //HERE IS 226, Lee un registro
ResultadoLectura = node.readHoldingRegisters(0x40104, 1); //HERE IS 226, Lee un registro
if (ResultadoLectura == node.ku8MBSuccess)
{
Serial.print("Funcionó!");
Serial.print("\nPV: ");
Serial.println(node.getResponseBuffer(0x8A)); // Divide by 100.0f
Serial.print("\nSV: ");
Serial.println(node.getResponseBuffer(0x00));
Serial.print("\nBaud: ");
Serial.println(node.getResponseBuffer(0x00));
}
else {
Serial.print("\nError en conexión: ");
Serial.println(ResultadoLectura); // Debe dar 0
}
delay(1000);
}
No no ı work but ı am new user because ı do not upload code. And ı paste the code new message
Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).
According to your wiring diagram you connected the serial connection to Serial2.
0x40103 is much bigger than the available register address range in Modbus. I guess you wanted to get the register 103 (no "0x" necessary, that labels a hexadecimal literal). 40103 is just for dumb PLCs that cannot distinguish between the Modbus register types other than reserving address ranges for that purpose.
You won't find anything at position 0x8A = 138 in the response buffer if you just read 1 value.

