Linux (and Mac OS X) Software to Read Data from Morningstar Products that Support the MODBUS Protocol

It is relatively easy to write software to read data from a Morningstar product that supports the MODBUS protocol. I have tested the software below with a SunSaver MPPT and a SureSine-300 using their MODBUS connections, a phone line splitter, and a Meterbus to Serial Converter (MSC). The programming principles in the software below should also work with the SunSaver Duo, the TriStar PWM, the TriStar MPPT, and the Relay Driver.

Example Hardware Required:

Software Required:

I have an off-the-grid solar power system with a Morningstar SunSaver MPPT, a Kyocera KD205GX-LP 205 watt photovoltaic (PV) panel, and a Morningstar SureSine-300 Inverter for AC loads. When I installed the system originally, I built a serial port-attached digital voltmeter to track the charge status of the batteries. This device was attached to a solar-powered Linksys NSLU2 running Debian Lenny. While this setup worked fine, the only information it could provide was the voltage of the batteries. I wanted to be able to also track the array voltage and charging current and the load voltage and current. The SunSaver MPPT provides this information via its MODBUS connection, so I recently bought a Morningstar Meterbus to Serial Converter (MSC) and started looking for examples of Linux software to communicate with the SunSaver MPPT. There is not much out there. The only information I could find of someone successfully using the MODBUS connection with Linux was at http://osaether.wordpress.com/. At least it could be done. I also found libmodbus - a MODBUS library for Linux and Mac OS X. It compiles and works well on both my Mac and my Linux computers and has some example software that helped point me in the right direction. I also replaced my Linksys NSLU2 with a much faster Sheevaplug (rewired to run from a DC source) running Debian Lenny.

Setup libmodbus

wget http://github.com/downloads/stephane/libmodbus/libmodbus-2.0.3.tar.gz
tar zxvf libmodbus-2.0.3.tar.gz
cd libmodbus-2.0.3
./configure
make
su
	make install
	ln -s /usr/local/lib/libmodbus.so.2.0.0 /usr/lib/libmodbus.so
	ln -s /usr/local/lib/libmodbus.so.2.0.0 /usr/lib/libmodbus.so.2
exit

*** Note on my Linux machine, I had to edit line 148 of modbus.h to increase char device[16] to char device[24] to allow for a longer file path and name to my udev-defined USB-serial port.

Simple Software to Read Data from the SunSaver MPPT

/*
 *  sunsavertest.c
 *  

Copyright 2010 Tom Rinehart.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see http://www.gnu.org/licenses/.

*/

/* Compile with: cc sunsavertest.c -o sunsavertest -lmodbus */

#include <stdio.h>
#include <stdlib.h>
#include <modbus/modbus.h>

#define SUNSAVERMPPT    0x01	/* Default address of the SunSaver MPPT */

int main(void)
{
	modbus_param_t mb_param;
	int ret;
	float adc_vb_f,adc_va_f,adc_vl_f,adc_ic_f,adc_il_f;
	uint16_t data[10];

	/* Setup the serial port parameters */
	modbus_init_rtu(&mb_param, "/dev/ttyUSB0", 9600, "none", 8, 2);

	/* Open the MODBUS connection */
	if (modbus_connect(&mb_param) == -1) {
		printf("ERROR Connection failed\n");
		exit(1);
	}

	/* Read the first five RAM Registers (0x04) and convert the results to
		their proper floating point values */
	ret = read_input_registers(&mb_param, SUNSAVERMPPT, 0x08, 5, data);

	adc_vb_f=data[0]*100.0/32768.0;
	printf("adc_vb_f=%.2f\n",adc_vb_f);

	adc_va_f=data[1]*100.0/32768.0;
	printf("adc_va_f=%.2f\n",adc_va_f);

	adc_vl_f=data[2]*100.0/32768.0;
	printf("adc_vl_f=%.2f\n",adc_vl_f);

	adc_ic_f=data[3]*79.16/32768.0;
	printf("adc_ic_f=%.2f\n",adc_ic_f);

	adc_il_f=data[4]*79.16/32768.0;
	printf("adc_il_f=%.2f\n",adc_il_f);

	/* Close the MODBUS connection */
	modbus_close(&mb_param);
        
	return(0);
}

Complete Examples of Accessing All of the RAM, EEPROM, and Log Registers in a Morningstar SunSaver MPPT

Building from the basic example above, the programs in the following archive read and process all of the RAM, EEPROM, and Log Registers and print out the results.

Download The Code

sunsaver.tar.gz

A Complete Example of Accessing All of the RAM and EEPROM Registers in a Morningstar SureSine-300

The program in the following archive reads and processes all of the RAM and EEPROM Log Registers and print out the results.

Download The Code

suresine.tar.gz

References

MODBUS Application Protocol Specification v1.1b

SunSaver MPPT MODBUS Specification

SureSine-300 MODBUS Specification

Last modified: November 7, 2010