Problema RS485-Modbus Arduino Uno

Buonasera a tutti,
mi sto cimentando con la lettura di registri Modbus mediante RS485, utilizzando Arduino Uno e la shied con MAX485,ma nonostante abbia provato tutte le libreria che mi sono capitate a tiro non sono riuscito a leggere nemmeno un valore.
Una delle ultime prove che ho effettuato, ho preso come riferimento l'esempio RS485HalfDouplex della libreria ModbusMaster e dopo aver modificato l'ID Modbus del dispositivo e il baudrate, collegati i pin D1 e R0 rispettivamente sui pin 1 e 0 di Arduino Uno, aprendo il monitor vengono stampati dei caratteri indecifrabili, nonostante il baudrate del monitor sia corretto.

Dato che ormai sto mettendo in dubbio tutto, (tranne il dispositivo da interrogare che ho testato con successo con un client modbus RTU e adattatore USB), avreste del materiale, idee, linee guida, qualsiasi cosa per poterne venire a capo?

Grazie mille anticipatamente.

Buonasera,
essendo il tuo primo post, nel rispetto del regolamento della sezione Italiana del forum (… punto 13, primo capoverso), ti chiedo cortesemente di presentarti IN QUESTO THREAD (spiegando bene quali conoscenze hai di elettronica e di programmazione ... possibilmente evitando di scrivere solo una riga di saluto) e di leggere con molta attenzione tutto il su citato REGOLAMENTO ... Grazie. :slight_smile:

Guglielmo

P.S.: Ti ricordo che, purtroppo, fino a quando non sarà fatta la presentazione nell’apposito thread, nessuno ti potrà rispondere, quindi ti consiglio di farla al più presto. :wink:

ciao enfo018,

dopo che ti sarai presentato e avrai letto il regolamento posta il codice lato Arduino che ci diamo un occhio.

ciao

Grazie ad entrambi, purtroppo nella foga del momento ho dimenticato le buone maniere.
Ho prontamente rimediato.
Scusate ancora.

ORSO2001:
... posta il codice lato Arduino che ci diamo un occhio.

... mi raccomando, in conformità al REGOLAMENTO , punto 7, il codice va racchiuso tra i tag CODE che, in fase di edit, ti inserisce il bottone </> ... primo a sinistra :wink:

Guglielmo

perfetto...adesso posta il codice lato arduino, nel modo indicato da Guglielmo..oppure allegalo come file, ed un link al device/slave a cui vuoi collegarti

ciao

Ciao ORSO2001,
questo è il dispositivo che vorrei interrogare S203TA-D.
Per quanto riguarda il codice, ho realizzato diversi script, per ora sto lavorando sull'esempio fornito con la documentazione della libreria SimpleModbusMaster, eliminando la funzione 16.
Stavo cercando di capire dove scrive il risultato della query modbus, ma ancora senza successo.

#include <SimpleModbusMaster.h>

/* To communicate with a slave you need to create a
   packet that will contain all the information
   required to communicate to that slave.

   There are numerous counters for easy diagnostic.
   These are variables already implemented in a
   packet. You can set and clear these variables
   as needed.

   There are general modbus information counters:
   requests - contains the total requests to a slave
   successful_requests - contains the total successful requests
   total_errors - contains the total errors as a sum
   timeout - contains the total time out errors
   incorrect_id_returned - contains the total incorrect id returned errors
   incorrect_function_returned - contains the total incorrect function returned errors
   incorrect_bytes_returned - contains the total incorrect bytes returned errors
   checksum_failed - contains the total checksum failed errors
   buffer_errors - contains the total buffer errors

   And there are modbus specific exception counters:
   illegal_function - contains the total illegal function errors
   illegal_data_address - contains the total illegal data_address errors
   illegal_data_value - contains the total illegal data value errors
   misc_exceptions - contains the total miscellaneous returned exceptions

   And finally there is a variable called "connection" that
   at any given moment contains the current connection
   status of the packet. If true then the connection is
   active if false then communication will be stopped
   on this packet untill the programmer sets the "connection"
   variable to true explicitly. The reason for this is
   because of the time out involved in modbus communication.
   EACH faulty slave that's not communicating will slow down
   communication on the line with the time out value. E.g.
   Using a time out of 1500ms, if you have 10 slaves and 9 of them
   stops communicating the latency burden placed on communication
   will be 1500ms * 9 = 13,5 seconds!!!!

   modbus_update() returns the previously scanned false connection.
   You can use this as the index to your packet array to find out
   if the connection has failed in that packet and then react to it.
   You can then try to re-enable the connecion by setting the
   packet->connection attribute to true.
   The index will only be available for one loop cycle, after that
   it's cleared and ready to return the next false connection index
   if there is one else it will return the packet array size indicating
   everything is ok.

   All the error checking, updating and communication multitasking
   takes place in the background!

   In general to communicate with to a slave using modbus
   RTU you will request information using the specific
   slave id, the function request, the starting address
   and lastly the number of registers to request.
   Function 3 and 16 are supported. In addition to
   this broadcasting (id = 0) is supported on function 16.
   Constants are provided for:
   Function 3 -  READ_HOLDING_REGISTERS
   Function 16 - PRESET_MULTIPLE_REGISTERS

   The example sketch will read a packet consisting
   of 3 registers from address 0 using function 3 from
   the GM7U LS Industrial PLC (id = 2) and then write
   another packet containing the same 3 registers and
   the counter information of both packets using function 16
   to address 3 in the PLC. Using the supplied PLC software
   you can then view in realtime what the values are in
   the PLC's registers.
*/


//////////////////// Port information ///////////////////
#define baud 19200
#define timeout 1000
#define polling 200 // the scan rate

// If the packets internal retry register matches
// the set retry count then communication is stopped
// on that packet. To re-enable the packet you must
// set the "connection" variable to true.
#define retry_count 10

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
  PACKET1,
  // leave this last entry
  TOTAL_NO_OF_PACKETS
};

// Create an array of Packets for modbus_update()
Packet packets[TOTAL_NO_OF_PACKETS];

// Create a packetPointer to access each packet
// individually. This is not required you can access
// the array explicitly. E.g. packets[PACKET1].id = 2;
// This does become tedious though...
packetPointer packet1 = &packets[PACKET1];

// The data from the PLC will be stored
// in the regs array
unsigned int regs[8];

void setup()
{
  // read 3 registers starting at address 0
  packet1->id = 2;
  packet1->function = READ_HOLDING_REGISTERS;
  packet1->address = 6;
  packet1->no_of_registers = 1;
  packet1->register_array = regs;

  // P.S. the register_array entries above can be different arrays

  // Initialize communication settings etc...
  modbus_configure(baud, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
}

void loop()
{
  unsigned int connection_status = modbus_update(packets);
  Serial.println(connection_status);
  if (connection_status != TOTAL_NO_OF_PACKETS)
  {
    // You could re-enable the connection by:
    //packets[connection_status].connection = true;
  }
  else
  // update the array with the counter data
  regs[3] = packet1->requests;
  regs[4] = packet1->successful_requests;
  regs[5] = packet1->total_errors;

}

ciao...scusa ma questo è uno degli esempi della libreria...non è impostato per il tuo device!?

devi farci vedere cosa fai esattamente...e cosa ottieni....

Ciao, si esatto, volevo basarmi sull'esempio per capire come interrogare il dispositivo.
Le modifiche che ho fatto all'esempio riguardano il baudrate, l'ID del dispositivo e la cancellazione della funzione 16 modbus che al momento non mi serve. Stavo cercando di capire come recuperare la risposta alla query modbus che gli passo.

Scusate l'UP, sono riuscito a stampare i dati a video, facendo una print del primo elemento di regs.
Rimane mi rimane il dubbio su una stringa di caratteri incomprensibili che viene stampata poco prima della stampa del risultato.
Potrebbe essere la trasmissione della query?

posta il codice che stai usando...dicci se per inviare/ricevere i dati usi la Serial od una SoftwareSerial...uno schema di coolegamento sarebbe gradito.

ciao