Skip to content

Commit

Permalink
Merge pull request #10 from riptideio/python3
Browse files Browse the repository at this point in the history
Python3
  • Loading branch information
dhoomakethu authored May 28, 2019
2 parents ec6491a + a46c601 commit c0869f8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
7 changes: 4 additions & 3 deletions modbus_simulator/ui/datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class ErrorPopup(Popup):
"""
def __init__(self, **kwargs):
# print kwargs
super(ErrorPopup, self).__init__(**kwargs)
super(ErrorPopup, self).__init__()
# super(ErrorPopup, self).__init__(**kwargs)
content = BoxLayout(orientation="vertical")
content.add_widget(Label(text=kwargs['text'], font_size=20))
mybutton = Button(text="Dismiss", size_hint=(1,.20), font_size=20)
Expand Down Expand Up @@ -422,7 +423,7 @@ def on_data_update(self, index, data):
if self.blockname in ['input_registers', 'holding_registers']:
self.list_view.adapter.data[index]['value'] = float(data)
else:
self.list_view.adapter.data.update({index: float(data)})
self.list_view.adapter.data[index]['value'] = int(data)
self.list_view._trigger_reset_populate()
data = {'event': 'sync_data',
'data': {index: self.list_view.adapter.data[index]}}
Expand Down Expand Up @@ -462,7 +463,7 @@ def update_registers(self, new_values, update_info):
to_remove = None
if count > 1:
offset = int(offset)
to_remove = [str(o) for o in list(xrange(offset+1, offset+count))]
to_remove = [str(o) for o in list(range(offset+1, offset+count))]

self.list_view.adapter.update_for_new_data()
self.refresh(new_values, to_remove)
Expand Down
24 changes: 17 additions & 7 deletions modbus_simulator/ui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,8 @@ def _update_data_models(self, active, tab, value):
registers = sum(
map(
lambda val: int(
filter(
str.isdigit, str(val.get('formatter', '16')))
''.join(list(filter(
str.isdigit, str(val.get('formatter', '16')))))
), _data['data'].values()))/16

# Old schema
Expand Down Expand Up @@ -588,9 +588,12 @@ def sync_data_callback(self, blockname, data):
v['formatter']
)
else:
# v = dict(value=int(v))
if not isinstance(v, dict):
v = dict(value=v)
self.modbus_device.set_values(int(self.active_slave),
current_tab,
k, int(v['value']))
k, v.get('value'))
except KeyError:
pass
except struct.error:
Expand All @@ -604,11 +607,17 @@ def sync_formatter_callback(self, blockname, data, old_formatter):
try:
_data = self.data_map[self.active_slave][current_tab]
_updated = {}
for k, v in data.items():
old_wc = int(filter(str.isdigit, str(old_formatter)))/16
new_wc = int(filter(str.isdigit, v.get('formatter')))/16
current = list(data.keys())
for k in current:
old_wc = int(''.join(list(
filter(str.isdigit, str(old_formatter))
)))/16
new_wc = int(''.join(list(
filter(str.isdigit, data[k].get('formatter'))
)))/16
new_val, count = self.modbus_device.decode(
int(self.active_slave), current_tab, k, v['formatter']
int(self.active_slave),
current_tab, k, data[k]['formatter']
)
data[k]['value'] = new_val
_updated['offset'] = k
Expand All @@ -621,6 +630,7 @@ def sync_formatter_callback(self, blockname, data, old_formatter):
)
for i, val in enumerate(missing):
o = int(k) + new_wc + i
o = int(o)
if not isinstance(k, int):
o = str(o)
data[o] = {'value': val, 'formatter': 'uint16'}
Expand Down
4 changes: 3 additions & 1 deletion modbus_simulator/utils/pymodbus_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def get_values(self, slave_id, block_name, address, size=1):
slave = self.get_slave(slave_id)
address = self._calc_offset(block_name, address)
if slave.validate(_FX_MAPPER[block_name], address, count=size):
return slave.getValues(_FX_MAPPER[block_name], address, size)
return slave.getValues(_FX_MAPPER[block_name], address, int(size))

def get_slave(self, slave_id):
return self.context[slave_id]
Expand All @@ -265,6 +265,8 @@ def encode(self, slave_id, block_name, offset, value, formatter):
builder = BinaryPayloadBuilder(byteorder=self.byte_order,
wordorder=self.word_order)
add_method = ENCODERS.get(formatter)
if 'int' in add_method: # Temp fix
value = int(value)
getattr(builder, add_method)(value)
payload = builder.to_registers()
return self.set_values(slave_id, block_name, offset, payload)
Expand Down
1 change: 1 addition & 0 deletions requirements
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Kivy-Garden==0.1.4
pygame==1.9.4
pyglet==1.2.4
Pygments==2.1.3
pymodbus==2.1.0
pyserial==3.2.1
requests==2.12.4
six==1.10.0

0 comments on commit c0869f8

Please sign in to comment.