Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions socs/agents/labjack/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import struct
import time
import traceback

import numexpr
import numpy as np
Expand All @@ -13,6 +14,7 @@
from ocs import ocs_agent, site_config
from ocs.ocs_twisted import TimeoutLock
from scipy.interpolate import interp1d
from twisted.internet import reactor

txaio.use_twisted()

Expand Down Expand Up @@ -247,8 +249,22 @@ def init_labjack(self, session, params=None):
return False, "Could not acquire lock."

# Connect with the labjack
self.handle = ljm.openS("ANY", "ANY", self.ip_address)
info = ljm.getHandleInfo(self.handle)
try:
self.handle = ljm.openS("ANY", "ANY", self.ip_address)
info = ljm.getHandleInfo(self.handle)
except LJMError as e:
self.log.error(f"Failed to connect to device: {e}")
self.log.error("{e}", e=traceback.format_exc())
self.log.critical("Stopping reactor.")
reactor.callFromThread(reactor.stop)
return False, 'Initialization failed.'
except Exception as e:
self.log.error(f"Caught unexpected {type(e).__name__} during init:")
self.log.error("{e}", e=traceback.format_exc())
self.log.critical("Stopping reactor.")
reactor.callFromThread(reactor.stop)
return False, 'Initialization failed.'

self.log.info("\nOpened LabJack of type: %i, Connection type: %i,\n"
"Serial number: %i, IP address: %s, Port: %i" %
(info[0], info[1], info[2],
Expand Down Expand Up @@ -336,7 +352,22 @@ def acq(self, session, params=None):
}

# Query the labjack
raw_output = ljm.eStreamRead(self.handle)
try:
raw_output = ljm.eStreamRead(self.handle)
except LJMError as e:
session.degraded = True
self.log.error(f"Failed to read stream: {e}")
self.log.error("{e}", e=traceback.format_exc())
self.log.critical("Stopping reactor.")
reactor.callFromThread(reactor.stop)
return False, 'Acquisition failed.'
except Exception as e:
session.degraded = True
self.log.error(f"Caught unexpected {type(e).__name__} while streaming:")
self.log.error("{e}", e=traceback.format_exc())
self.log.critical("Stopping reactor.")
reactor.callFromThread(reactor.stop)
return False, 'Acquisition failed.'
output = raw_output[0]

# Data comes in form ['AIN0_1', 'AIN1_1', 'AIN0_2', ...]
Expand Down