Skip to content

Commit

Permalink
add PVs for device info
Browse files Browse the repository at this point in the history
  • Loading branch information
d-perl committed Nov 12, 2024
1 parent 390de3b commit 246d4a1
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/rtc6_fastcs/bindings/rtc6_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,19 @@ void check_conection()
}
}

void connect(const char *ipStr, char *programFilePath, char *correctionFilePath)
int connect(const char *ipStr, char *programFilePath, char *correctionFilePath)
{
init_dll();

// The library allows for connecting to multiple cards, but we just use one
// See manual page 855 for info about the conversion of IP address to an int
int cardNo = eth_assign_card_ip(eth_convert_string_to_ip(ipStr), 0);
int result = select_rtc(cardNo);
int result = acquire_rtc(cardNo);
if (result != cardNo)
{
throw rtc_error(str(format("select_rtc for card %1% failed with error: %2%. Most likely, a card was not found at the given IP address: %3%.") % cardNo % result % ipStr));
throw rtc_error(str(format("acquire_rtc for card %1% failed with error: %2%. Most likely, a card was not found at the given IP address: %3%.") % cardNo % result % ipStr));
}
auto serialNum = load_program_and_correction_files(cardNo, programFilePath, correctionFilePath);
check_conection();
return load_program_and_correction_files(cardNo, programFilePath, correctionFilePath);
}

struct CardInfo
Expand All @@ -139,6 +138,8 @@ struct CardInfo
int getSerialNumber() { return serialNumber; };
std::string getIpStr() { return ipStr; };
bool getIsAcquired() { return isAcquired; };

private:
int firmwareVersion;
int serialNumber;
std::string ipStr;
Expand All @@ -147,15 +148,21 @@ struct CardInfo

CardInfo get_card_info()
{
check_conection();
// check_conection(); // not working ?
int32_t out[16];
auto out_ptr = reinterpret_cast<std::uintptr_t>(&out);
eth_get_card_info(1, out_ptr);
return CardInfo(out);
}

int get_last_eth_error()
{
return eth_get_last_error();
}

void close_connection()
{
// TODO: check if there is anything to release
int releasedCard = release_rtc(1);
if (!releasedCard)
{
Expand Down Expand Up @@ -183,6 +190,7 @@ PYBIND11_MODULE(rtc6_bindings, m)
m.def("close", &close_connection, "close the open connection, if any");
m.def("close_again", &close_connection, "close the open connection, if any");
m.def("get_card_info", &get_card_info, "get info for the connected card; throws RtcConnectionError on failure");
m.def("get_last_error", &get_last_eth_error, "get the last error for an ethernet command");

// Just for testing - TODO remove these when things are going
m.def("add", &add, "A function that adds two numbers", py::arg("i"), py::arg("j"));
Expand Down
Binary file not shown.
8 changes: 7 additions & 1 deletion src/rtc6_fastcs/bindings/rtc6_bindings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ __all__ = [
"close_again",
"connect",
"get_card_info",
"get_last_error",
"ip_int_to_str",
"ip_str_to_int",
"throw_rtc_error",
Expand Down Expand Up @@ -57,7 +58,7 @@ def close_again() -> None:
close the open connection, if any
"""

def connect(ip_string: str, program_file_path: str, correction_file_path: str) -> None:
def connect(ip_string: str, program_file_path: str, correction_file_path: str) -> int:
"""
connect to the eth-box at the given IP
"""
Expand All @@ -67,6 +68,11 @@ def get_card_info() -> CardInfo:
get info for the connected card; throws RtcConnectionError on failure
"""

def get_last_error() -> int:
"""
get the last error for an ethernet command
"""

def ip_int_to_str(ip_int: int) -> str:
"""
convert IP address from int to string
Expand Down
6 changes: 6 additions & 0 deletions src/rtc6_fastcs/controller/rtc_connection.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from rtc6_fastcs.bindings.rtc6_bindings import CardInfo


class RtcConnection:
def __init__(self, box_ip: str, program_file: str, correction_file: str) -> None:
from rtc6_fastcs.bindings import rtc6_bindings as bindings
Expand All @@ -7,6 +10,9 @@ def __init__(self, box_ip: str, program_file: str, correction_file: str) -> None
self._program_file = program_file
self._correction_file = correction_file

def get_card_info(self) -> CardInfo:
return self._bindings.get_card_info()

async def connect(self) -> None:
self._bindings.connect(self._ip, self._program_file, self._correction_file)

Expand Down
21 changes: 21 additions & 0 deletions src/rtc6_fastcs/controller/rtc_controller.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import asyncio

from fastcs.attributes import AttrR
from fastcs.controller import Controller
from fastcs.datatypes import Bool, Int, String
from fastcs.wrappers import command

from rtc6_fastcs.controller.rtc_connection import RtcConnection


class RtcController(Controller):
firmware_version = AttrR(Int(), group="Information")
serial_number = AttrR(Int(), group="Information")
ip_address = AttrR(String(), group="Information")
is_acquired = AttrR(Bool(znam="False", onam="True"), group="Information")
# TODO add handlers for these which proc cardinfo

def __init__(self, box_ip: str, program_file: str, correction_file: str) -> None:
super().__init__()
self._conn = RtcConnection(box_ip, program_file, correction_file)
Expand All @@ -13,3 +24,13 @@ async def connect(self) -> None:

async def close(self) -> None:
await self._conn.close()

@command()
async def proc_cardinfo(self) -> None:
info = self._conn.get_card_info()
await asyncio.gather(
self.firmware_version.set(info.firmware_version),
self.serial_number.set(info.serial_number),
self.ip_address.set(info.ip_address),
self.is_acquired.set(info.is_acquired),
)
32 changes: 30 additions & 2 deletions tests/test_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,50 @@ def test_connection_exception():
def test_connect():
from rtc6_fastcs.bindings import rtc6_bindings as bindings

bindings.connect(
serial = bindings.connect(
"172.23.17.192",
"./rtc6_files/program_files",
"./rtc6_files/correction_files/Cor_1to1.ct5",
)

assert serial != 0


@pytest.mark.needs_librtc6
def test_card_info():
from rtc6_fastcs.bindings import rtc6_bindings as bindings

bindings.connect(
"172.23.17.192",
"./rtc6_files/program_files",
"./rtc6_files/correction_files/Cor_1to1.ct5",
)

info = bindings.get_card_info()
assert info.firmware_version
assert info.is_acquired


@pytest.mark.needs_librtc6
def test_close():
from rtc6_fastcs.bindings import rtc6_bindings as bindings

info = bindings.close()
_ = bindings.close()


@pytest.mark.needs_librtc6
def test_get_error():
from rtc6_fastcs.bindings import rtc6_bindings as bindings

try:
bindings.connect(
"172.23.17.192",
"./rtc6_files/program_files",
"./rtc6_files/correction_files/Cor_1to1.ct5",
)
except:
...

last_error = bindings.get_last_error()
bit_list = output = [int(x) for x in "{:032b}".format(last_error)]
assert bit_list[19]

0 comments on commit 246d4a1

Please sign in to comment.