sensor_sdm120.c File Reference

library to query measurements from eastron SDM120-ModBus electricity meter (code) More...

Go to the source code of this file.

## Macros

#define | SENSOR_SDM120_USART 3
| USART peripheral.
#define | SENSOR_SDM120_REDE_PORT B
| GPIO port for RS-485 receiver and driver output enable signal.
#define | SENSOR_SDM120_REDE_PIN 12
| GPIO pin for RS-485 receiver and driver output enable signal.
#define | SENSOR_SDM120_TIMER 3
| timer number to count time

## Enumerations

enum | timeout_t { TIMEOUT_BEGIN = 0, TIMEOUT_END, TIMEOUT_BETWEEN, TIMEOUT_MAX }
| the ModBus timeouts to respect for sending messages More...

## Functions

static uint16_t | crc_modbus (uint8_t *buffer, uint8_t size)
| compute CRC for ModBus More...
void | sensor_sdm120_setup (uint32_t baudrate)
| setup peripherals to communicate with electricity meter More...
static bool | sensor_sdm120_transmit_request (uint8_t meter_id, uint8_t function, uint16_t address, float value)
| send request to electricity meter More...
bool | sensor_sdm120_measurement_request (uint8_t meter_id, enum sensor_sdm120_measurement_type_t type)
| request measurement from electricity meter More...
bool | sensor_sdm120_configuration_request (uint8_t meter_id, enum sensor_sdm120_configuration_type_t type)
| request configuration from electricity meter More...
bool | sensor_sdm120_configuration_set (uint8_t meter_id, enum sensor_sdm120_configuration_type_t type, float value)
| set configuration in electricity meter More...
float | sensor_sdm120_measurement_decode (void)
| decode received measurement More...
void USART_ISR() | SENSOR_SDM120_USART (void)
| USART interrupt service routine called when data has been transmitted or received.
void TIM_ISR() | SENSOR_SDM120_TIMER (void)
| interrupt service routine called on timeout

## Variables

static uint8_t | rx_buffer [9] = {0}
| buffer for received response (ModBus response messages can be 2+256+2 long but we will only read up to 2 registers)
static volatile uint8_t | rx_used = 0
| number of received data bytes in buffer
static uint8_t | tx_buffer [13] = {0}
| buffer for request to transmit (ModBus request messages can be 7+256+2 long but we will only write up to 2 registers
static volatile uint8_t | tx_used = 0
| number of byte to transmit
volatile bool | sensor_sdm120_measurement_received = false
| a measurement response has been received
static enum timeout_t | timeout
| the current timeout used
static uint16_t | timeout_times [TIMEOUT_MAX] = {0}
| current timeout used
static const uint16_t | register_input []
| SDM120 3xxxx input register start addresses for the measurement types. More...
static const uint16_t | register_holding []
| SDM120 4xxxx holding register start addresses for the configuration types. More...

## Detailed Description

library to query measurements from eastron SDM120-ModBus electricity meter (code)

Author
  King Kévin kingk.nosp@m.evin.nosp@m.@cuvo.nosp@m.odoo.nosp@m..info

Date
  2016

Note
  peripherals used: USART USART peripheral used for communication with electricity meter , GPIO GPIO peripheral used for controlling RS-485 adapter , timer timer peripheral to enforce waiting time between messages

Definition in file sensor_sdm120.c.

## Enumeration Type Documentation

## timeout_t

enum timeout_t

the ModBus timeouts to respect for sending messages

Enumerator
TIMEOUT_BEGIN | silent time before sending data
TIMEOUT_END | silent time after sending data
TIMEOUT_BETWEEN | time to wait between messages
TIMEOUT_MAX | last element (useful to no the number of elements)

Definition at line 67 of file sensor_sdm120.c.

## Function Documentation

## crc_modbus()

static uint16_t crc_modbus | ( | uint8_t * | buffer,
|  | uint8_t | size
| ) |  |

static

compute CRC for ModBus

Note
  ModBus uses ANSi/IBM 16-bits CRC (with normal polynomial 0x8005, reverse polynomial 0xA001, start value 0xfff)

Parameters

  [in] | buffer | data on which to compute the CRC for
  [in] | size | number of byte to compute the CRC for

Returns
  computed CRC checksum

Definition at line 111 of file sensor_sdm120.c.

## sensor_sdm120_configuration_request()

bool sensor_sdm120_configuration_request | ( | uint8_t | meter_id,
|  | enum sensor_sdm120_configuration_type_t | type
| ) |  |

request configuration from electricity meter

Parameters

  [in] | meter_id | electricity meter device ID
  [in] | type | configuration type to request

Returns
  if transmission started

Definition at line 245 of file sensor_sdm120.c.

## sensor_sdm120_configuration_set()

bool sensor_sdm120_configuration_set | ( | uint8_t | meter_id,
|  | enum sensor_sdm120_configuration_type_t | type,
|  | float | value
| ) |  |

set configuration in electricity meter

Parameters

  [in] | meter_id | electricity meter device ID
  [in] | type | configuration type to set
  [in] | value | configuration value to set

Returns
  if transmission started

Definition at line 253 of file sensor_sdm120.c.

## sensor_sdm120_measurement_decode()

float sensor_sdm120_measurement_decode | ( | void |  | ) |

decode received measurement

Returns
  decoded measurement or number of registers written, NaN if message has error or no new measurement has been received, infinity if an error or unknown message has been received

Definition at line 261 of file sensor_sdm120.c.

## sensor_sdm120_measurement_request()

bool sensor_sdm120_measurement_request | ( | uint8_t | meter_id,
|  | enum sensor_sdm120_measurement_type_t | type
| ) |  |

request measurement from electricity meter

Parameters

  [in] | meter_id | electricity meter device ID
  [in] | type | measurement type to request

Returns
  if transmission started

Definition at line 237 of file sensor_sdm120.c.

## sensor_sdm120_setup()

void sensor_sdm120_setup | ( | uint32_t | baudrate | ) |

setup peripherals to communicate with electricity meter

Parameters

  [in] | baudrate | baud rate of RS485 serial communication

Definition at line 127 of file sensor_sdm120.c.

## sensor_sdm120_transmit_request()

static bool sensor_sdm120_transmit_request | ( | uint8_t | meter_id,
|  | uint8_t | function,
|  | uint16_t | address,
|  | float | value
| ) |  |

static

send request to electricity meter

Parameters

  [in] | meter_id | electricity meter device id (ModBus salve address)
  [in] | function | ModBus function: 0x03 read two 16 bits holding registers, 0x04 read two 16 bits input registers, 0x10 write two 16 bits holding registers
  [in] | address | register start point address
  [in] | value | value to store in holding register (if function 0x10 is used)

Returns
  if request is correct and transmission started

Definition at line 180 of file sensor_sdm120.c.

## Variable Documentation

## register_holding

const uint16_t register_holding[]

static

Initial value:
= {
0x000c,
0x0012,
0x0014,
0x001c,
0x0056,
0xf900,
0xf910,
0xf920
}

SDM120 4xxxx holding register start addresses for the configuration types.

Definition at line 94 of file sensor_sdm120.c.

## register_input

const uint16_t register_input[]

static

Initial value:
= {
0x0000,
0x0006,
0x000c,
0x0012,
0x0018,
0x001e,
0x0046,
0x0048,
0x004a,
0x004c,
0x004e,
0x0156,
0x0158
}

SDM120 3xxxx input register start addresses for the measurement types.

Definition at line 77 of file sensor_sdm120.c.