Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] improved client control #15

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7cf5d75
scpi base class
ddkohler Feb 7, 2023
f86194e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 7, 2023
eb922da
Update _scpi_sensor.py
ddkohler Feb 7, 2023
9b8c6db
Merge branch 'expand-scpi-capabilities' of https://github.com/yaq-pro…
ddkohler Feb 7, 2023
0c8439c
fixes
ddkohler Feb 7, 2023
4323c2a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 7, 2023
2778a5f
debugging for write errors
ddkohler Mar 7, 2023
9001c88
fix imports
ddkohler Mar 7, 2023
20c7f4c
context manager for resource
ddkohler Mar 7, 2023
8daa78c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 7, 2023
5b8c290
Update _scpi_base.py
ddkohler Mar 8, 2023
989e5e1
loop read/write until returned
ddkohler Mar 8, 2023
b34a3a8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 8, 2023
809317d
Update _scpi_base.py
ddkohler Mar 8, 2023
30ed8a2
Merge branch 'expand-scpi-capabilities' of https://github.com/yaq-pro…
ddkohler Mar 8, 2023
3b062aa
Update _scpi_base.py
ddkohler Mar 8, 2023
9eae94a
Update _scpi_base.py
ddkohler Mar 8, 2023
97c53a7
Update _scpi_sensor.py
ddkohler Mar 8, 2023
cf3e745
remove context manager
ddkohler Mar 8, 2023
636a580
diagnostic handling
ddkohler Mar 8, 2023
fe20c27
simplify _measure
ddkohler Mar 9, 2023
3f2bb0f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 9, 2023
20d99b5
Update _scpi_sensor.py
ddkohler Mar 13, 2023
1a47d95
Update _scpi_sensor.py
ddkohler Mar 13, 2023
d3706b8
new debug messages
ddkohler Mar 14, 2023
279d01d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 14, 2023
2fde817
Update _scpi_sensor.py
ddkohler Mar 14, 2023
8ed5529
Merge branch 'expand-scpi-capabilities' of https://github.com/yaq-pro…
ddkohler Mar 14, 2023
7b94b3e
Update _scpi_base.py
ddkohler Mar 16, 2023
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
36 changes: 36 additions & 0 deletions yaqd_scpi/_scpi_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
__all__ = ["SCPIBase"]

import pyvisa
import sys
import asyncio

from yaqd_core import IsDaemon


class SCPIBase(IsDaemon):
_kind = "scpi-sensor"

def __init__(self, name, config, config_filepath):
super().__init__(name, config, config_filepath)
if sys.platform.startswith("win32"):
rm = pyvisa.ResourceManager() # use ni-visa backend
else:
rm = pyvisa.ResourceManager("@py") # use pyvisa-py backend
self._instrument: pyvisa.Resource = rm.open_resource(config["visa_address"], timeout=500)

def direct_scpi_write(self, command: str):
self._instrument.write(command)

def direct_scpi_query(self, command: str) -> str:
assert "?" in command
return str(self._instrument.query(command))

def extra_scpi_read(self):
"""
purely for debugging
"""
read = self._instrument.read()
self.logger.info(f"extra read: {read}")

def close(self):
self._instrument.close()
28 changes: 18 additions & 10 deletions yaqd_scpi/_scpi_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,35 @@
from typing import Dict, Any, List

import pyvisa
import numpy as np

from yaqd_core import HasMeasureTrigger, IsSensor, IsDaemon
from yaqd_core import HasMeasureTrigger, IsSensor
from ._scpi_base import SCPIBase


class SCPISensor(HasMeasureTrigger, IsSensor, IsDaemon):
class SCPISensor(HasMeasureTrigger, IsSensor, SCPIBase):
_kind = "scpi-sensor"

def __init__(self, name, config, config_filepath):
super().__init__(name, config, config_filepath)
self._channel_names = list(config["channels"].keys())
self._channel_units = {k: config["channels"][k]["units"] for k in self._channel_names}
if sys.platform.startswith("win32"):
rm = pyvisa.ResourceManager() # use ni-visa backend
else:
rm = pyvisa.ResourceManager("@py") # use pyvisa-py backend
self._instrument = rm.open_resource(config["visa_address"])

async def _measure(self):
query_error = False
out = {}
for k in self._channel_names:
query = self._config["channels"][k]["query"]
self._instrument.write(query)
out[k] = float(self._instrument.read())
while True:
try:
response = self._instrument.query(self._config["channels"][k]["query"])
except Exception as e:
self.logger.error(f"error in _measure with key {k}")
self.logger.error(e)
query_error = True
await asyncio.sleep(0.1)
continue
out[k] = float(response)
break
if query_error:
self.logger.info(f"afer error, {k} = {str(out)}")
return out
10 changes: 3 additions & 7 deletions yaqd_scpi/_scpi_set_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@

import pyvisa

from yaqd_core import HasLimits, HasPosition, IsDaemon
from yaqd_core import HasLimits, HasPosition
from ._scpi_base import SCPIBase


class SCPISetContinuous(HasLimits, HasPosition, IsDaemon):
class SCPISetContinuous(HasLimits, HasPosition, SCPIBase):
_kind = "scpi-set-continuous"

def __init__(self, name, config, config_filepath):
super().__init__(name, config, config_filepath)
self._scpi_command = self._config["scpi_command"]
if sys.platform.startswith("win32"):
rm = pyvisa.ResourceManager() # use ni-visa backend
else:
rm = pyvisa.ResourceManager("@py") # use pyvisa-py backend
self._instrument = rm.open_resource(config["visa_address"])

def _set_position(self, position):
self._busy = True
Expand Down
10 changes: 3 additions & 7 deletions yaqd_scpi/_scpi_set_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@

import pyvisa

from yaqd_core import IsDiscrete, HasPosition, IsDaemon
from yaqd_core import IsDiscrete, HasPosition
from ._scpi_base import SCPIBase


class SCPISetDiscrete(IsDiscrete, HasPosition, IsDaemon):
class SCPISetDiscrete(IsDiscrete, HasPosition, SCPIBase):
_kind = "scpi-set-discrete"

def __init__(self, name, config, config_filepath):
super().__init__(name, config, config_filepath)
self._scpi_command = self._config["scpi_command"]
self._identifiers_by_position = {k: v for v, k in self._config["identifiers"].items()}
if sys.platform.startswith("win32"):
rm = pyvisa.ResourceManager() # use ni-visa backend
else:
rm = pyvisa.ResourceManager("@py") # use pyvisa-py backend
self._instrument = rm.open_resource(config["visa_address"])

def _set_position(self, position):
self._busy = True
Expand Down
29 changes: 27 additions & 2 deletions yaqd_scpi/scpi-sensor.avpr
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@
"request": [],
"response": "boolean"
},
"direct_scpi_query": {
"doc": "",
"request": [
{
"name": "command",
"type": "string"
}
],
"response": "string"
},
"direct_scpi_write": {
"doc": "write a SCPI command",
"request": [
{
"name": "command",
"type": "string"
}
],
"response": "null"
},
"extra_scpi_read": {
"doc": "",
"request": [],
"response": "null"
},
"get_channel_names": {
"doc": "Get current channel names.",
"origin": "is-sensor",
Expand Down Expand Up @@ -230,8 +255,8 @@
"default": null,
"name": "units",
"type": [
"string",
"null"
"null",
"string"
]
}
],
Expand Down
16 changes: 14 additions & 2 deletions yaqd_scpi/scpi-sensor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ conda-forge = "https://anaconda.org/conda-forge/yaqd-scpi"
type = "record"
name = "channel"
fields = [{"name"="query", "type"="string", "doc"="SCPI query, e.g. MEAS:VOLT:DC?"},
{"name"="units", "type"=["string", "null"], "default"="__null__"}]
{"name"="units", "type"=["null", "string"], "default"="__null__"}]

[config]

Expand All @@ -26,4 +26,16 @@ default = {}

[config.visa_address]
type = "string"
doc = "VISA resource name."
doc = "VISA resource name."

[messages.direct_scpi_write]
doc = "write a SCPI command"
request = [{"name"="command", "type"="string"}]

[messages.direct_scpi_query]
doc = ""
request = [{"name"="command", "type"="string"}]
response = "string"

[messages.extra_scpi_read]
doc = ""
10 changes: 10 additions & 0 deletions yaqd_scpi/scpi-set-continuous.avpr
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@
"request": [],
"response": "boolean"
},
"direct_scpi_write": {
"doc": "write a SCPI command",
"request": [
{
"name": "command",
"type": "string"
}
],
"response": "null"
},
"get_config": {
"doc": "Full configuration for the individual daemon as defined in the TOML file.\nThis includes defaults and shared settings not directly specified in the daemon-specific TOML table.\n",
"origin": "is-daemon",
Expand Down
6 changes: 5 additions & 1 deletion yaqd_scpi/scpi-set-continuous.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ doc = "Units of position / destination."

[config.visa_address]
type = "string"
doc = "VISA resource name."
doc = "VISA resource name."

[messages.direct_scpi_write]
doc = "write a SCPI command"
request = [{"name"="command", "type"="string"}]
10 changes: 10 additions & 0 deletions yaqd_scpi/scpi-set-discrete.avpr
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@
"request": [],
"response": "boolean"
},
"direct_scpi_write": {
"doc": "write a SCPI command",
"request": [
{
"name": "command",
"type": "string"
}
],
"response": "null"
},
"get_config": {
"doc": "Full configuration for the individual daemon as defined in the TOML file.\nThis includes defaults and shared settings not directly specified in the daemon-specific TOML table.\n",
"origin": "is-daemon",
Expand Down
6 changes: 5 additions & 1 deletion yaqd_scpi/scpi-set-discrete.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ doc = "SCPI command, e.g. :SOUR1:FREQ"

[config.visa_address]
type = "string"
doc = "VISA resource name."
doc = "VISA resource name."

[messages.direct_scpi_write]
doc = "write a SCPI command"
request = [{"name"="command", "type"="string"}]