Skip to content

Instantly share code, notes, and snippets.

@nipunbatra
Forked from subinabr/currentvolt.py
Last active August 29, 2015 13:57
Show Gist options
  • Save nipunbatra/9407044 to your computer and use it in GitHub Desktop.
Save nipunbatra/9407044 to your computer and use it in GitHub Desktop.
from pymodbus.constants import Endian
from struct import pack, unpack
from pymodbus.constants import Endian
from pymodbus.utilities import pack_bitstring
from pymodbus.utilities import unpack_bitstring
from pymodbus.exceptions import ParameterException
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
from pymodbus.transaction import ModbusSocketFramer as ModbusFramer
import datetime
import time
import sys
import os
import logging
import logging.handlers
from os.path import join
ID_VENDOR = '0403'
ID_PRODUCT = '6001'
class BinaryPayloadDecoder(object):
''' A utility that helps decode payload messages from a modbus
reponse message. It really is just a simple wrapper around
the struct module, however it saves time looking up the format
strings. What follows is a simple example::
decoder = BinaryPayloadDecoder(payload)
first = decoder.decode_8bit_uint()
second = decoder.decode_16bit_uint()
'''
def __init__(self, payload, endian=Endian.Little):
''' Initialize a new payload decoder
:param payload: The payload to decode with
:param endian: The endianess of the payload
'''
self._payload = payload
self._pointer = 0x00
self._endian = endian
@classmethod
def fromRegisters(klass, registers, endian=Endian.Little):
''' Initialize a payload decoder with the result of
reading a collection of registers from a modbus device.
The registers are treated as a list of 2 byte values.
We have to do this because of how the data has already
been decoded by the rest of the library.
:param registers: The register results to initialize with
:param endian: The endianess of the payload
:returns: An initialized PayloadDecoder
'''
if isinstance(registers, list): # repack into flat binary
payload = ' '.join(pack('>H', x) for x in registers)
return klass(payload, endian)
raise ParameterException('Invalid collection of registers supplied')
@classmethod
def fromCoils(klass, coils, endian=Endian.Little):
''' Initialize a payload decoder with the result of
reading a collection of coils from a modbus device.
The coils are treated as a list of bit(boolean) values.
:param coils: The coil results to initialize with
:param endian: The endianess of the payload
:returns: An initialized PayloadDecoder
'''
if isinstance(coils, list):
payload = pack_bitstring(coils)
return klass(payload, endian)
raise ParameterException('Invalid collection of coils supplied')
def reset(self):
''' Reset the decoder pointer back to the start
'''
self._pointer = 0x00
def decode_8bit_uint(self):
''' Decodes a 8 bit unsigned int from the buffer
'''
self._pointer += 1
fstring = self._endian + 'B'
handle = self._payload[self._pointer - 1:self._pointer]
return unpack(fstring, handle)[0]
def decode_bits(self):
''' Decodes a byte worth of bits from the buffer
'''
self._pointer += 1
fstring = self._endian + 'B'
handle = self._payload[self._pointer - 1:self._pointer]
return unpack_bitstring(handle)
def decode_16bit_uint(self):
''' Decodes a 16 bit unsigned int from the buffer
'''
self._pointer += 2
fstring = self._endian + 'H'
handle = self._payload[self._pointer - 2:self._pointer]
return unpack(fstring, handle)[0]
def decode_32bit_uint(self):
''' Decodes a 32 bit unsigned int from the buffer
'''
self._pointer += 4
fstring = self._endian + 'I'
handle = self._payload[self._pointer - 4:self._pointer]
return unpack(fstring, handle)[0]
def decode_64bit_uint(self):
''' Decodes a 64 bit unsigned int from the buffer
'''
self._pointer += 8
fstring = self._endian + 'Q'
handle = self._payload[self._pointer - 8:self._pointer]
return unpack(fstring, handle)[0]
def decode_8bit_int(self):
''' Decodes a 8 bit signed int from the buffer
'''
self._pointer += 1
fstring = self._endian + 'b'
handle = self._payload[self._pointer - 1:self._pointer]
return unpack(fstring, handle)[0]
def decode_16bit_int(self):
''' Decodes a 16 bit signed int from the buffer
'''
self._pointer += 2
fstring = self._endian + 'h'
handle = self._payload[self._pointer - 2:self._pointer]
return unpack(fstring, handle)[0]
def decode_32bit_int(self):
''' Decodes a 32 bit signed int from the buffer
'''
self._pointer += 4
fstring = self._endian + 'i'
handle = self._payload[self._pointer - 4:self._pointer]
return unpack(fstring, handle)[0]
def decode_64bit_int(self):
''' Decodes a 64 bit signed int from the buffer
'''
self._pointer += 8
fstring = self._endian + 'q'
handle = self._payload[self._pointer - 8:self._pointer]
return unpack(fstring, handle)[0]
def decode_32bit_float(self):
''' Decodes a 32 bit float from the buffer
'''
self._pointer += 4
fstring = self._endian + 'f'
handle = self._payload[self._pointer - 4:self._pointer]
return unpack(fstring, handle)[0]
def decode_64bit_float(self):
''' Decodes a 64 bit float(double) from the buffer
'''
self._pointer += 8
fstring = self._endian + 'd'
handle = self._payload[self._pointer - 8:self._pointer]
return unpack(fstring, handle)[0]
def decode_string(self, size=1):
''' Decodes a string from the buffer
:param size: The size of the string to decode
'''
self._pointer += size
return self._payload[self._pointer - size:self._pointer]
def find_tty_usb(idVendor, idProduct):
"""find_tty_usb('067b', '2302') -> '/dev/ttyUSB0'"""
# Note: if searching for a lot of pairs, it would be much faster to search
# for the enitre lot at once instead of going over all the usb devices
# each time.
for dnbase in os.listdir('/sys/bus/usb/devices'):
dn = join('/sys/bus/usb/devices', dnbase)
if not os.path.exists(join(dn, 'idVendor')):
continue
idv = open(join(dn, 'idVendor')).read().strip()
if idv != idVendor:
continue
idp = open(join(dn, 'idProduct')).read().strip()
if idp != idProduct:
continue
for subdir in os.listdir(dn):
if subdir.startswith(dnbase + ':'):
for subsubdir in os.listdir(join(dn, subdir)):
if subsubdir.startswith('ttyUSB'):
return join('/dev', subsubdir)
def read_current_register(client, address, count=1, unit=146):
"""Reads the current register
Parameters
----------
address : Address of the register
count: Number of registers to read
unit: Unit of hardware
Returns
-------
current: float
"""
regObject = client.read_holding_registers(
address=address, count=count, unit=unit)
decoder = BinaryPayloadDecoder.fromRegisters(
regObject.registers, endian=Endian.Big)
after_decimal = decoder.decode_16bit_uint()
decoder = BinaryPayloadDecoder.fromRegisters(
regObject.registers, endian=Endian.Big)
before_decimal = decoder.decode_8bit_int()
if (after_decimal % 1000) > 500:
before_decimal -= 1
current = before_decimal + (after_decimal % 1000) / 1000.0
return current
def main():
METER_PORT = "/dev/ttyUSB0"
# print METER_PORT#reading to which port rs485(client) is connected
client = ModbusClient(method='rtu', port=METER_PORT, baudrate=9600,
stopbits=1, parity='E', timeout=0.3, bytesize=8)
client.connect()
f = open("abc.csv", "w")
while True:
try:
row = str(datetime.datetime.now())
for register in range(10, 23):
current = read_current_register(client, register)
row += "," + str(current)
row += ",\n"
f.write(row)
except Exception as e:
print "Internal Exception: Meter: " + '\n' + e.__str__()
client = None
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment