I'm trying to interface with a Morningstar Tristar MPPT solar charge controller as a bit of a side project to complete my off-grid system. It has two networking interfaces, TCP and RS-232. I'm trying to read the data from the controller via RS-232 however MODBUS is completely new to me and I'm struggling to understand how to read the registers.
The library I'm using is ModbusMaster.
Here is the code I have so far;
#include <ModbusMaster.h>
// instantiate ModbusMaster object
ModbusMaster node;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
// communicate with Modbus slave ID 2 over Serial (port 0)
node.begin(1, Serial);
}
void loop() {
static uint32_t i;
uint8_t j, result;
uint16_t data[2];
i++;
result = node.readHoldingRegisters(0026, 2);
// do something with data if read is successful
if (result == node.ku8MBSuccess)
{
for (j = 0; j < 6; j++)
{
data[j] = node.getResponseBuffer(j);
}
}
}
Literally just copied/pasted from the libraries documentation with the write functions removed.
I haven't used that library, however my guess is that you are seeing the raw modbus frames sent by the device. Some of the bytes are outside the printable ASCII range therefore they are shown as squares.
Normally you would use "Serial" for diagnostic messages and have the device connected to a second serial port, either real or software driven.
Modbus device <-> second serial port <-> Modbus library <-> Your Arduino code Serial.print -> Serial monitor
Your code is the magic that turns the data array into something human readable.
Most probably these are the requests sent by the Arduino.
The three seconds are the timeout, so the device did never reply. This probably means that connection isn't established, most probably a problem of the wiring. What hardware do you use for the adaption to the RS-232? If you connected the Arduino serial pins directly to the RS-232 port you might have fried them already as the voltages of an RS-232 port are not TTL compatible.
Post a wiring diagram of your setup!
What should be the result of that connection? Is the Arduino just a forwarder device to a PC? Does the Arduino react to the values and control something using them?
Of course you are right. The perils of a double connection. The data going out the Tx pin also goes to the USB serial Rx pin and then onto the PC, and the data coming back from the Modbus device disappears into the Arduino's UART so it's impossible to Serial.print the data back to the PC without also sending it out the Tx pin again. Using a second serial port makes things much less confusing.
Apologies for the delay. Just tested positive for cornavirus this morning so I'm feeling rotten.
I have a RS-232 to TTL converter I bought from eBay here;
I have the TX connected to my RX pin on my Arduino. RX from the converter to the TX on my Arduino. I'm powering the converter via 5v from the 5v pin on the Arduino and negative is connected to GND on the Arduino from the converter.
I also have the RS-232 DB9 cable connected to the charge controller and into the DB9 port on the RS-232 to TTL converter.
I then have a microusb cable going into the Arduino and USB into my computer so I can upload software onto the Arduino.
It seems like I'm mixing serial connections here. Is this something I can sort programmatically or do I need additional hardware?
@pylon thanks for clarifying that. Luckily it seems like the Arduino is fine, I hooked a GPS receiver up to it and it's still working fine over the TX/RX lines.
I'm at a loss at whether the converter is broken or I'm just failing miserably at how to actually query a register.