Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 25, 2023
1 parent 2bab4c8 commit e73ee33
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
14 changes: 7 additions & 7 deletions socs/agents/hi6200/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,15 @@ def init(self, session, params=None):

try:
self.scale = Hi6200Interface(self.ip_address, self.tcp_port)
except:

except BaseException:
self.log.error(f"Some unknown error occurred initializing TCP Server")
return False, "TCP Failure"
self.log.info("Connected to scale.")

return True, 'Initialized Scale.'

@ocs_agent.param('wait', type=float, default=1)

def monitor_weight(self, session, params=None):
"""
Expand All @@ -78,7 +77,7 @@ def monitor_weight(self, session, params=None):
}

try:

data['data']["Gross"] = self.scale.read_scale_gross_weight()
data['data']["Net"] = self.scale.read_scale_net_weight()

Expand All @@ -89,9 +88,8 @@ def monitor_weight(self, session, params=None):
# Allow this process to be queried to return current data
session.data = data


except ValueError as e:

self.log.error(f"Scale responded with an anomolous number, ignorning: {e}")

except AttributeError as e:
Expand All @@ -111,6 +109,7 @@ def stop_monitoring(self, session, params=None):
self.monitor = False
return True, "Stopping current monitor"


def make_parser(parser=None):
"""Build the argument parser for the Agent. Allows sphinx to automatically
build documentation based on this function.
Expand Down Expand Up @@ -143,5 +142,6 @@ def main(args=None):

runner.run(agent, auto_reconnect=True)

if __name__ == '__main__':

if __name__ == '__main__':
main()
61 changes: 31 additions & 30 deletions socs/agents/hi6200/drivers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from pyModbusTCP.client import ModbusClient
import struct

from pyModbusTCP.client import ModbusClient


class Hi6200Interface():
"""
The Hi6200 Weight Processor uses a Modbus TCP Interface to communicate.
The Gross and Net weight sensors are always available to read on the 8,9 and 6,7 registers respectively.
"""

def __init__(self, ip_address, tcp_port, verbose=False, **kwargs):
"""
Connects to the Hi6200 weight sensor using a TCP Modbus Client with pyModbusTCP.
This works ~similarly to a socket connection using a IP and port.
ModbusClient will not throw errors upon incorrect ip_address!
ModbusClient will also allow multiple recconects unlike a socket.
"""
Expand All @@ -23,16 +25,16 @@ def read_scale_gross_weight(self):
"""
Returns the current gross weight reading of the scale in the sensors chosen unit (kg)
"""
#The gross weight is always available on the 8,9 registers.
#Reading these registers will return an int.
a,b = self.scale.read_holding_registers(8,2)
#The ints read on the registers must be converted to hex.
#The hex bits are then concatenated and converted to float as CDAB
#Strip the '0x' hex bit prefix as it is not useful.
#Then concatenate the bits

# The gross weight is always available on the 8,9 registers.
# Reading these registers will return an int.
a, b = self.scale.read_holding_registers(8, 2)

# The ints read on the registers must be converted to hex.
# The hex bits are then concatenated and converted to float as CDAB

# Strip the '0x' hex bit prefix as it is not useful.
# Then concatenate the bits
hex_a = hex(a)[2:]
while len(hex_a) < 4:
hex_a = '0' + hex_a
Expand All @@ -41,25 +43,25 @@ def read_scale_gross_weight(self):
while len(hex_b) < 4:
hex_b = '0' + hex_b

hex_weight= hex_b + hex_a
hex_weight = hex_b + hex_a

#This struct function converts the concatenated hex bits to a float.
# This struct function converts the concatenated hex bits to a float.
return struct.unpack('!f', bytes.fromhex(hex_weight))[0]

def read_scale_net_weight(self):
"""
Returns the current net weight reading of the scale in the sensors chosen unit (kg)
"""
#The gross weight is always available on the 6,7 registers.
#Reading these registers will return an int.
a,b = self.scale.read_holding_registers(6,2)
#The ints read on the registers must be converted to hex.
#The hex bits are then concatenated and converted to float as CDAB.
#Strip the '0x' hex bit prefix as it is not useful.
#Then concatenate the bits

# The gross weight is always available on the 6,7 registers.
# Reading these registers will return an int.
a, b = self.scale.read_holding_registers(6, 2)

# The ints read on the registers must be converted to hex.
# The hex bits are then concatenated and converted to float as CDAB.

# Strip the '0x' hex bit prefix as it is not useful.
# Then concatenate the bits
hex_a = hex(a)[2:]
while len(hex_a) < 4:
hex_a = '0' + hex_a
Expand All @@ -68,8 +70,7 @@ def read_scale_net_weight(self):
while len(hex_b) < 4:
hex_b = '0' + hex_b

hex_weight= hex_b + hex_a

#This struct function converts the concatenated hex bits to a float.
return struct.unpack('!f', bytes.fromhex(hex_weight))[0]
hex_weight = hex_b + hex_a

# This struct function converts the concatenated hex bits to a float.
return struct.unpack('!f', bytes.fromhex(hex_weight))[0]

0 comments on commit e73ee33

Please sign in to comment.