Olá,
Preciso obter a temperatura e o setpoit de um controlador Novus_N1200 por meio da USB que está ligada em um módulo USB TO TTL, que por sua vez está ligada em um arduino mega 2560. Mas eu não estou conseguindo.
Obtive a saída da USB envidada pelo arduino: 01 03 00 00 00 02 C4 0B
Esta saída é diferente de quando eu faço a comunicação do Novus_N1200 com o computador, por meio de um código c implementado por meio da biblioteca libmodbus (https://libmodbus.org/). Por meio deste código em c eu consigo obter a temperatura e o setpoint do novus.
A mensagem enviada pelo código c é: 01 03 04 15 C0 01 0B BE 54
Em outro momento foi esta: 01 03 04 0F A0 01 10 F9 59
O código que estou rodando é o que segue. Alguém pode me ajudar?
#include <ModbusRtu.h>
#include <SoftwareSerial.h>
// data array for modbus network sharing
uint16_t au16data[16];
uint8_t u8state;
/**
* Modbus object declaration
* u8id : node id = 0 for master, = 1..247 for slave
* port : serial port
* u8txenpin : 0 for RS-232 and USB-FTDI
* or any pin number > 1 for RS-485
*/
Modbus master(0); // this is master and RS-232 or USB-FTDI via software serial
/**
* This is an structe which contains a query to an slave device
*/
modbus_t telegram;
SoftwareSerial mySerial(10, 11);//Create a SoftwareSerial object so that we can use software serial.
//Search "software serial" on Arduino.cc to find out more details.
unsigned long u32wait;
void setup() {
Serial.begin(9600);
master.begin(&mySerial, 9600);
// master.start(); // start the ModBus object.
master.setTimeOut( 5000 ); // if there is no answer in 2000 ms, roll over
u32wait = millis() + 1000;
u8state = 0;
Serial.println("Configurou...");
}
void printBuffer(){
Serial.print("buffer");
for (int i=0; i<9; i++) {
Serial.print(master.au8Buffer[i], HEX);
Serial.print(" ");
}
Serial.print("");
}
void loop() {
Serial.println("getID()");
Serial.println(master.getID());
switch( u8state ) {
case 0:
if (millis() > u32wait) {
u8state++; // wait state
Serial.println("0...");
}
break;
case 1:
Serial.println("1...");
telegram.u8id = 1; // slave address
telegram.u8fct = 0x3; // function code (this one is registers read)
telegram.u16RegAdd = 0; //0x0000;//0x0415; // start address in slave
telegram.u16CoilsNo = 2; //0x0002;//0xC001; // number of elements (coils or registers) to read
telegram.au16reg = au16data; // pointer to a memory array in the Arduino
master.query( telegram ); // send query (only once)
printBuffer();
u8state++;
break;
case 2:
Serial.println("2...");
master.poll(); // check incoming messages
if (master.getState() == COM_IDLE) {
u8state = 0;
u32wait = millis() + 2000;
Serial.println(au16data[0]);//Or do something else!
}
break;
}
Serial.println("getOutCnt()");
Serial.println(master.getOutCnt());
Serial.println("getInCnt()");
Serial.println(master.getInCnt());
Serial.println("getLastError()");
Serial.println(master.getLastError());
//Read Multiple Holding Register - Lectura de multiples registros
for (int i=0; i<2; i++) {
Serial.println(telegram.au16reg[i]); // Read Holding Register [0]
}
Serial.println("");
delay(3000);
}