Commit 78d2dc82 authored by Manuel Navarrete's avatar Manuel Navarrete
Browse files

update test_resistence.py

parent 4619917a
......@@ -53,8 +53,8 @@ import modbus
if __name__ == '__main__':
m=modbus.Modbus(delay=1)
old_slave = 1
new_slave = 2
old_slave = 2
new_slave = 3
tx_change = struct.pack('B',old_slave)+'\x10\x00\x0E\x00\x01\x02'+struct.pack('>H',new_slave)
tx_change += m.get_crc(tx_change)
......
import struct
import time
import sys
import os
import shelve
import polling
module_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(0, module_path)
import modbus
if __name__ == '__main__':
m=modbus.Modbus()
slave_address = 3
slave_code = struct.pack('B',slave_address)
tx_test = slave_code + '\x10\x00\x0C\x00\x01\x02\xF0\xF0'
tx_test += m.get_crc(tx_test)
tx_tr = slave_code + '\x03\x00\x0C\x00\x01'
tx_tr += m.get_crc(tx_tr)
tx_r = slave_code + '\x03\x00\x03\x00\x01'
tx_r +=m.get_crc(tx_r)
print list(m.raw(tx_test))
while True:
rx = m.raw(tx_r)
value = struct.unpack('>H',rx[3:5])[0]
print list(rx)
print value
print list(m.raw(tx_tr))
......@@ -64,9 +64,9 @@ sys.path.insert(0, module_path)
import modbus
DATA = 'data.db'
VALUE_1 = 'value'
VALUE_2 = 'time'
VALUES = {VALUE_1: 0, VALUE_2: 0}
VALUE_KEY = 'value'
TIME_KEY = 'time'
VALUES = {VALUE_KEY: 0, TIME_KEY: 0}
def get_tr(tx):
"""
......@@ -80,6 +80,8 @@ def get_tr(tx):
"""
rx_tr = m.raw(tx)
print list (rx_tr)
#rx_tr='\x03\x00\x00\x00\x00\x00\x00'
if len(rx_tr)==7:
value = struct.unpack('>H',rx_tr[3:5])[0]
if value == 0:
......@@ -110,7 +112,8 @@ def get_r(tx):
"""
r = -1
rx_r = m.raw(tx)
print list (rx_r)
#rx_r='\x03\x00\x00\x00\x15\x00\x00'
if len(rx_r) == 7:
r = struct.unpack('>H',rx_r[3:5])[0]
if r>0:
......@@ -134,18 +137,18 @@ def save_data(value,slave):
: type value: string
"""
save_values = VALUES
d = None
time_value = time.time()
VALUES[VALUE_1] = value
VALUES[VALUE_2] = time_value
save_values[VALUE_KEY] = value
save_values[TIME_KEY] = time_value
try:
d = shelve.open(DATA,'c')
except Exception as e:
print 'Error de guardado'
else:
d[slave] = VALUES
d[slave] = save_values
d.close()
print 'Dato guardado'
......@@ -177,7 +180,7 @@ def get_data(slave):
d.close()
return result
def test_resistence(tx_tr,tx_r,tx_test):
def test_resistence(tx_tr,tx_r,tx_test,slave):
"""
Check the value of the test of resistence, the value of resistence and do the test of resistence
......@@ -190,34 +193,52 @@ def test_resistence(tx_tr,tx_r,tx_test):
"""
tr = get_tr(tx_tr)
if tr == True:
print 'No haciendo test'
r = get_r(tx_r)
if r == 0:
print 'Iniciar test de resistencia'
m.raw(tx_test)
elif r > 0:
print 'hay v'
data = get_data(slave)
real_time = time.time()
time_saved = real_time-data[VALUE_2]
time_saved = real_time-data[TIME_KEY]
if time_saved>timexp:
print 'tiempo de expiracion superado'
m.raw(tx_test)
save_data(r,slave)
else:
print 'Tiempo no superado'
if __name__ == '__main__':
m = modbus.Modbus()
data = VALUES
tr = False
timexp = 10
timexp = 5
slave_address = 2
slave_address = 3
slave = str(slave_address)
slave_code = struct.pack('B',slave_address)
tx_tr =slave_code + '\x03\x00\x0C\x00\x01'
tx_tr = slave_code + '\x03\x00\x0C\x00\x01'
tx_tr += m.get_crc(tx_tr)
tx_r = slave_code + '\x03\x00\x03\x00\x01'
tx_r +=m.get_crc(tx_r)
tx_test= slave_code + '\x10\x00\x0C\x00\x01\x02\xF0\xF0'
tx_test = slave_code + '\x10\x00\x0C\x00\x01\x02\xF0\xF0'
tx_test += m.get_crc(tx_test)
polling.poll(lambda:test_resistence(tx_tr,tx_r,tx_test), step=0.5, timeout=12)
while True:
test_resistence(tx_tr,tx_r,tx_test,slave)
"""
polling.poll(lambda:test_resistence(tx_tr,tx_r,tx_test,slave), step=10, timeout=120)
try:
polling.poll(doing, step=0.5, timeout=2)
except Exception as e:
print 'error', e
"""
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