Needed
1 RaspberryPI
1 SDM120C EnergyMeter
1 USB<>RS485 Converter
1 twisted pair cable
recommend install Raspbian
Set speed USB port - 2400 bps, 8E1
stty -F /dev/ttyUSB0 2400 cs8 parenb -parodd
C code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
union
{
char c[4];
float f;
}u;
int main(int argc, char* argv[]) {
struct termios serial;
char inBuff[16];
char *buffer;
char choice[5];
int wcount = 0;
char cVoltage[] = {0x01,0x04,0x00,0x00,0x00,0x02,0x71,0xCB};
char cCurrent[] = {0x01,0x04,0x00,0x06,0x00,0x02,0x91,0xCA};
char cPower[] = {0x01,0x04,0x00,0x0C,0x00,0x02,0xB1,0xC8};
char cAAPower[] = {0x01,0x04,0x00,0x12,0x00,0x02,0xD1,0xCE};
char cRAPower[] = {0x01,0x04,0x00,0x18,0x00,0x02,0xF1,0xCC};
char cPFactor[] = {0x01,0x04,0x00,0x1E,0x00,0x02,0x11,0xCD};
char cFrequency[] = {0x01,0x04,0x00,0x46,0x00,0x02,0x90,0x1E};
char cIAEnergy[] = {0x01,0x04,0x00,0x48,0x00,0x02,0xF1,0xDD};
char cEAEnergy[] = {0x01,0x04,0x00,0x4A,0x00,0x02,0x50,0x1D};
char cTAEnergy[] = {0x01,0x04,0x01,0x56,0x00,0x02,0x90,0x27};
int fd = open("/dev/ttyUSB2", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror(argv[2]);
return -1;
}
if (tcgetattr(fd, &serial) < 0) {
perror("Getting configuration");
return -1;
}
//////////////////// Set up Serial Configuration ////////////
serial.c_iflag = 0;
serial.c_oflag = 0;
serial.c_lflag = 0;
serial.c_cflag = 0;
serial.c_cc[VMIN] = 0;
serial.c_cc[VTIME] = 10;
serial.c_cflag = B2400 | CS8 | PARENB | CREAD & ~PARODD;
fcntl(fd,F_SETFL,0);
tcsetattr(fd, TCSANOW, &serial); // Apply configuration
//////////////////////////////////////////////////////////////
int rcount;
buffer = &inBuff[0];
int i = 0;
int j = 0;
////////////////////////// Voltage /////////////////////////////
i = 0; j = 0;
memset(&inBuff[0],0,sizeof(inBuff));
write(fd,cVoltage,sizeof(cVoltage));
buffer = &inBuff[0];
printf("Sent: ");
for(j = 0;j < sizeof(cVoltage);j++)
{
printf("%02x ",cVoltage[j]);
}
printf("\r\n");
printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
if (rcount < 0) {
perror("Read");
return -1;
}
buffer++;
printf("%02x ",inBuff[i]); i++;
}
u.c[3] = inBuff[3];
u.c[2] = inBuff[4];
u.c[1] = inBuff[5];
u.c[0] = inBuff[6];
printf("\r\n");
printf("Voltage: %4.2f V \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
////////////////////////// Current /////////////////////////////
i = 0; j = 0;
memset(&inBuff[0],0,sizeof(inBuff));
write(fd,cCurrent,sizeof(cCurrent));
buffer = &inBuff[0];
printf("Sent: ");
for(j = 0;j < sizeof(cCurrent);j++)
{
printf("%02x ",cCurrent[j]);
}
printf("\r\n");
printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
if (rcount < 0) {
perror("Read");
return -1;
}
buffer++;
printf("%02x ",inBuff[i]); i++;
}
u.c[3] = inBuff[3];
u.c[2] = inBuff[4];
u.c[1] = inBuff[5];
u.c[0] = inBuff[6];
printf("\r\n");
printf("Current: %4.2f A \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Power ///////////////////////////////
i = 0; j = 0;
memset(&inBuff[0],0,sizeof(inBuff));
write(fd,cPower,sizeof(cPower));
buffer = &inBuff[0];
printf("Sent: ");
for(j = 0;j < sizeof(cPower);j++)
{
printf("%02x ",cPower[j]);
}
printf("\r\n");
printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
if (rcount < 0) {
perror("Read");
return -1;
}
buffer++;
printf("%02x ",inBuff[i]); i++;
}
u.c[3] = inBuff[3];
u.c[2] = inBuff[4];
u.c[1] = inBuff[5];
u.c[0] = inBuff[6];
printf("\r\n");
printf("Power: %4.2f W \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Active Apparent Power ///////////////////////////////
i = 0; j = 0;
memset(&inBuff[0],0,sizeof(inBuff));
write(fd,cAAPower,sizeof(cAAPower));
buffer = &inBuff[0];
printf("Sent: ");
for(j = 0;j < sizeof(cAAPower);j++)
{
printf("%02x ",cAAPower[j]);
}
printf("\r\n");
printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
if (rcount < 0) {
perror("Read");
return -1;
}
buffer++;
printf("%02x ",inBuff[i]); i++;
}
u.c[3] = inBuff[3];
u.c[2] = inBuff[4];
u.c[1] = inBuff[5];
u.c[0] = inBuff[6];
printf("\r\n");
printf("Active Apparent Power: %4.2f VA \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Reactive Apparent Power ///////////////////////////////
i = 0; j = 0;
memset(&inBuff[0],0,sizeof(inBuff));
write(fd,cRAPower,sizeof(cRAPower));
buffer = &inBuff[0];
printf("Sent: ");
for(j = 0;j < sizeof(cRAPower);j++)
{
printf("%02x ",cRAPower[j]);
}
printf("\r\n");
printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
if (rcount < 0) {
perror("Read");
return -1;
}
buffer++;
printf("%02x ",inBuff[i]); i++;
}
u.c[3] = inBuff[3];
u.c[2] = inBuff[4];
u.c[1] = inBuff[5];
u.c[0] = inBuff[6];
printf("\r\n");
printf("Reactive Apparent Power: %4.2f VAr \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// Power Factor ///////////////////////////////////
i = 0; j = 0;
memset(&inBuff[0],0,sizeof(inBuff));
write(fd,cPFactor,sizeof(cPFactor));
buffer = &inBuff[0];
printf("Sent: ");
for(j = 0;j < sizeof(cPFactor);j++)
{
printf("%02x ",cPFactor[j]);
}
printf("\r\n");
printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
if (rcount < 0) {
perror("Read");
return -1;
}
buffer++;
printf("%02x ",inBuff[i]); i++;
}
u.c[3] = inBuff[3];
u.c[2] = inBuff[4];
u.c[1] = inBuff[5];
u.c[0] = inBuff[6];
printf("\r\n");
printf("Power Factor: %4.2f \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Frequency /////////////////////////////////////
i = 0; j = 0;
memset(&inBuff[0],0,sizeof(inBuff));
write(fd,cFrequency,sizeof(cFrequency));
buffer = &inBuff[0];
printf("Sent: ");
for(j = 0;j < sizeof(cFrequency);j++)
{
printf("%02x ",cFrequency[j]);
}
printf("\r\n");
printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
if (rcount < 0) {
perror("Read");
return -1;
}
buffer++;
printf("%02x ",inBuff[i]); i++;
}
u.c[3] = inBuff[3];
u.c[2] = inBuff[4];
u.c[1] = inBuff[5];
u.c[0] = inBuff[6];
printf("\r\n");
printf("Frequency: %4.2f Hz \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// Import Active Energy /////////////////////////////////////
i = 0; j = 0;
memset(&inBuff[0],0,sizeof(inBuff));
write(fd,cIAEnergy,sizeof(cIAEnergy));
buffer = &inBuff[0];
printf("Sent: ");
for(j = 0;j < sizeof(cIAEnergy);j++)
{
printf("%02x ",cIAEnergy[j]);
}
printf("\r\n");
printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
if (rcount < 0) {
perror("Read");
return -1;
}
buffer++;
printf("%02x ",inBuff[i]); i++;
}
u.c[3] = inBuff[3];
u.c[2] = inBuff[4];
u.c[1] = inBuff[5];
u.c[0] = inBuff[6];
printf("\r\n");
printf("Import Active Energy: %4.2f KWh \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// Export Active Energy /////////////////////////////////////
i = 0; j = 0;
memset(&inBuff[0],0,sizeof(inBuff));
write(fd,cEAEnergy,sizeof(cEAEnergy));
buffer = &inBuff[0];
printf("Sent: ");
for(j = 0;j < sizeof(cEAEnergy);j++)
{
printf("%02x ",cEAEnergy[j]);
}
printf("\r\n");
printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
if (rcount < 0) {
perror("Read");
return -1;
}
buffer++;
printf("%02x ",inBuff[i]); i++;
}
u.c[3] = inBuff[3];
u.c[2] = inBuff[4];
u.c[1] = inBuff[5];
u.c[0] = inBuff[6];
printf("\r\n");
printf("Export Active Energy: %4.2f KWh \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// Total Active Energy /////////////////////////////////////
i = 0; j = 0;
memset(&inBuff[0],0,sizeof(inBuff));
write(fd,cTAEnergy,sizeof(cTAEnergy));
buffer = &inBuff[0];
printf("Sent: ");
for(j = 0;j < sizeof(cTAEnergy);j++)
{
printf("%02x ",cTAEnergy[j]);
}
printf("\r\n");
printf("Received: " );
while( (rcount = read(fd,buffer,1)) > 0)
{
if (rcount < 0) {
perror("Read");
return -1;
}
buffer++;
printf("%02x ",inBuff[i]); i++;
}
u.c[3] = inBuff[3];
u.c[2] = inBuff[4];
u.c[1] = inBuff[5];
u.c[0] = inBuff[6];
printf("\r\n");
printf("Total Active Energy: %4.2f KWh \r\n\r\n",u.f);
///////////////////////////////////////////////////////////////////////////////////////////
close(fd);
}