Commit 4bb93e3b authored by Manuel Navarrete's avatar Manuel Navarrete
Browse files

update changes examples

parent 4e069944
......@@ -53,19 +53,22 @@ if __name__ == '__main__':
new_slave = 2
baudrate = 6
tx_change = struct.pack('B',new_slave)+'\x10\x00\x20\x00\x01\x02'+struct.pack('B',new_slave)+struct.pack('B',baudrate)
tx_change = struct.pack('B',old_slave)+'\x10\x00\x20\x00\x01\x02'+struct.pack('B',new_slave)+struct.pack('B',baudrate)
tx_change += m.get_crc(tx_change)
print list(tx_change)
rx_change = m.raw(tx_change)
print list (rx_change)
tx_address = (struct.pack('B',new_slave)+'\x03\x00\x20\x00\x03')
tx_address = (struct.pack('B',new_slave)+'\x03\x00\x20\x00\x01')
tx_address += m.get_crc(tx_address)
rx_address = m.raw(tx_address)
print list (rx_address)
if len(rx_address)==7
address = struct.unpack('>B',rx[3])[0]
print 'La nueva dirección del sensor es:', address
elif len(rx_address)>0
if len(rx_address)==7:
address = struct.unpack('B',rx_address[3])[0]
print 'La nueva direccion del sensor es:', address
elif len(rx_address)>0:
print 'Error en el sensor : Trama erronea'
print list (rx_address)
else
print 'Error en el sensor : Trama vacía'
else:
print 'Error en el sensor : Trama vacia'
......@@ -72,7 +72,7 @@ def print_values(result):
: pararm result: values of the transducer
: type result: array
"""
print '------- MEASUREMENT-- -----'
print 'Sensor ', result[0]
......@@ -103,7 +103,7 @@ if __name__ == '__main__':
elif len (rx)>0:
print "Error en el sensor"+str(address_sensor[i])+": Trama erronea"
print list (rx)
else
else:
print "Error en el sensor"+str(address_sensor[i])+": Trama vacia"
time.sleep(0.5)
......@@ -43,7 +43,7 @@ import modbus
if __name__ == '__main__':
m =modbus.Modbus()
tx = '\x01\x03\x00\x01\x00\x02'
tx = '\x02\x03\x00\x01\x00\x02'
tx = tx + m.get_crc(tx)
while True:
......@@ -54,8 +54,7 @@ if __name__ == '__main__':
rx = m.raw(tx)
if len(rx) == 9
if len(rx)==9:
v = struct.unpack('>H',rx[3:5])[0] # miliVolts
t = struct.unpack('>H',rx[5:7])[0] # deciDegree Celsius
......@@ -67,10 +66,10 @@ if __name__ == '__main__':
print "Cell Temperature: "+str(t)+" C"
print '-----------------------'
elif len (rx)>0
elif len (rx)>0:
print 'Error en el sensor TA: Trama erronea '
print list (rx)
else
print 'Error en el sensor TC: Trama vacia'
else:
print 'Error en el sensor TA: Trama vacia'
time.sleep(1)
time.sleep(1)
......@@ -52,7 +52,7 @@ if __name__ == '__main__':
rx = m.raw(tx)
if len(rx) == 11
if len(rx) == 11:
i = struct.unpack('>H',rx[3:5])[0] # deciAmps
t1 = struct.unpack('>H',rx[5:7])[0] # deciDegree Celsius
t2 = struct.unpack('>H',rx[7:9])[0] # deciDegree Celsius
......@@ -67,10 +67,10 @@ if __name__ == '__main__':
print "Temperature 2: "+str(t2)+" C"
print '------------------------'
elif len (rx)>0
elif len (rx)>0:
print 'Error en el sensor TC: Trama erronea '
print list (rx)
else
else:
print 'Error en el sensor TC: Trama vacia'
time.sleep(1)
"""
Example to change the addres of the module TA
The change of address needs a delay time to change direction. In this case, the delay is 1s
Device:
Intelligent battery monitor
Model: HUASU H3G-TA-12
......@@ -49,7 +51,7 @@ sys.path.insert(0, module_path)
import modbus
if __name__ == '__main__':
m=modbus.Modbus()
m=modbus.Modbus(delay=1)
old_slave = 1
new_slave = 2
......@@ -58,15 +60,17 @@ if __name__ == '__main__':
tx_change += m.get_crc(tx_change)
rx_change = m.raw(tx_change)
m.set_delay(0)
tx_address = struct.pack('B',new_slave)+'\x03\x00\x0E\x00\x01'
tx_address += m.get_crc(tx_address)
rx_address = m.raw(tx_address)
if len(rx_address)==7
address =struct.unpack('>B',rx[3])[0]
print 'La nueva dirección del sensor TA es:', address
elif len(rx_address)>0
if len(rx_address)==7:
address =struct.unpack('>B',rx_address[4])[0]
print 'La nueva direccion del sensor TA es:', address
elif len(rx_address)>0:
print 'Error en el sensor TA : Trama erronea'
print list (rx_address)
else
print 'Error en el sensor TA : Trama vacía'
else:
print 'Error en el sensor TA : Trama vacia'
......@@ -126,7 +126,7 @@ def print_values_tc(result):
print "Temperature 2: "+str(result[2])+" C"
print '------------------------'
def print_value_ta(result):
def print_values_ta(result):
"""
Print the values
......@@ -152,7 +152,6 @@ if __name__ == '__main__':
for i in range(0,len(address_ta)):
tx_ta.append(struct.pack('B',address_ta[i])+'\x03\x00\x01\x00\x02')
tx_ta[i] += m.get_crc(tx_ta[i])
print list (tx_ta)
while True:
......@@ -160,25 +159,27 @@ if __name__ == '__main__':
rx_tc = m.raw(tx_tc)
if len(rx_tc) == 11:
result_tc = get_values_tc(rx _tc)
result_tc = get_values(rx_tc)
result_tc = scale_data_tc(result_tc)
print_values_tc(result_tc)
elif len (result_tc)>0
elif len (rx_tc)>0:
print 'Error en el sensor TC: Trama erronea '
print list (rx_tc)
else
else:
print 'Error en el sensor TC: Trama vacia'
#TA-MODULE
for i in range(0,len(tx_ta)):
rx_ta = m.raw(tx_ta[i])
if len (result_slave_ta) == 9:
result_ta = get_values_ta(rx_ta)
if len (rx_ta) == 9:
result_ta = get_values(rx_ta)
result_ta = scale_data_ta(result_ta)
print_values_ta(result_ta)
elif len (result_ta)>0
elif len (rx_ta)>0:
print "Error en el sensor TA "+str(address_ta[i])+": Trama erronea"
print list (rx_ta)
else
else:
print "Error en el sensor TA "+str(address_ta[i])+": Trama vacia"
time.sleep(1)
"""
Example to read the parameters of two transducers in real time
Device:
Digital transducers
Model 1: CE-AD11B-32ES5
http://www.ce-transducer.com/product/details/182/131/details
Model 2: CE-AD11B-32GS4
http://www.ce-transducer.com/product/details/185/108/details
TX_SLAVE1
Addres of slave 1: 01h
Function code: 03h
Addres of the first register: 0010h
Quanty of registers: 0006h
TX_SLAVE2
Addres of slave 2: 02h
Function code: 03h
Addres of the first register: 0010h
Quanty of registers: 0006h
RX_ COMMON
Addres of slave: 1 Byte
Function code: 1 Byte
Byte count: 1 Byte
Value 12 Bytes
CRC: 2 Bytes
"""
__author__ = 'Manuel Navarrete'
__copyright__ = 'Copyright (C) 2017'
__license__ = 'MIT (expat) License'
__version__ = '0.1'
__email__ = 'manuel.navarrete@whitewallenergy.com'
import struct
import time
import sys
import os
module_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, module_path)
import modbus
def get_values(rx):
"""
Get the values of the received frame
: pararm rx: Received frame
: type tx: Buffer of bytes
: return result: Values of the frame
: rtype: Array
"""
result = []
i = len(rx)-5
j = 3
result.append(struct.unpack('<B',rx[0])[0])
while (i>0):
result.append(struct.unpack('<H',rx[j:j+2])[0])
j = j+2
i = i-2
return result
def print_values(result):
"""
Print the values
: pararm result: values of the transducer
: type result: array
"""
print '------- MEASUREMENT-- -----'
print 'Sensor ', result[0]
print "Voltage of phase A: "+str(result[1])+"V"
print "Current of phase A: "+str(result[2])+"A"
print "Voltage of phase B: "+str(result[3])+"V"
print "Current of phase B: "+str(result[4])+"A"
print "Voltage of phase C: "+str(result[5])+"V"
print "Current of phase C: "+str(result[6])+"A"
print '---------------------------'
if __name__ == '__main__':
m = modbus.Modbus()
address_sensor = [1,2]
txs = []
for i in range(0, len(address_sensor)):
tx = struct.pack('B', address_sensor[i]) + '\x03\x00\x10\x00\x06'
tx += m.get_crc(tx)
txs.append(tx)
while True:
for i in range(0, len(txs)):
rx = m.raw(txs[i])
if len(rx) == 17:
print_values(get_values(rx))
elif len (rx)>0:
print "Error en el sensor"+str(address_sensor[i])+": Trama erronea"
print list (rx)
else
print "Error en el sensor"+str(address_sensor[i])+": Trama vacia"
time.sleep(0.5)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment