diff --git a/acspy/acsc.py b/acspy/acsc.py index dd6e9af..0347833 100644 --- a/acspy/acsc.py +++ b/acspy/acsc.py @@ -5,6 +5,7 @@ import ctypes import platform import re +import warnings from ctypes import byref, create_string_buffer import numpy as np @@ -104,13 +105,19 @@ class AcscError(Exception): } -def openCommDirect(): +def open_comm_direct() -> int: """Open simulator. Returns communication handle. """ - # caution: acs does treat errors differently for openComm functions! + warnings.warn( + "open_comm_direct is deprecated in favor of open_comm_simulator", + category=DeprecationWarning, + ) + # Caution: acs does treat errors differently for openComm functions! hcomm = acs.acsc_OpenCommDirect() + if hcomm == -1: + hcomm = open_comm_simulator() if hcomm == -1: err = getLastError() err_lng = int32() @@ -129,10 +136,43 @@ def openCommDirect(): return hcomm -def openCommEthernetTCP(address: str = "10.0.0.100", port: int = 701): +def openCommDirect() -> int: + return open_comm_direct() + + +def open_comm_simulator() -> int: + # Caution: acs does treat errors differently for openComm functions! + hcomm = acs.acsc_OpenCommSimulator() + if hcomm == -1: + hcomm = open_comm_direct() + if hcomm == -1: + err = getLastError() + err_lng = int32() + s = create_string_buffer(256) + if ( + acs.acsc_GetErrorString( + hcomm, int32(err), s, int32(ctypes.sizeof(s)), byref(err_lng) + ) + != 0 + ): + s[err_lng.value] = 0 + err_str = s.value.decode("ascii") + raise AcscError(str(err) + ": " + err_str) + else: + raise AcscError(err) + return hcomm + + +def openCommEthernetTCP(address: str = "10.0.0.100", port: int = 701) -> int: """Address is a string. Returns communication handle.""" + return open_comm_ethernet_tcp(address=address, port=port) + + +def open_comm_ethernet_tcp( + address: str = "10.0.0.100", port: int = 701 +) -> int: hcomm = acs.acsc_OpenCommEthernetTCP(address.encode(), port) if hcomm == -1: err = getLastError() @@ -169,6 +209,12 @@ def getSerialNumber(hcomm, wait=SYNCHRONOUS): return serial_number +def get_library_version() -> str: + resp: int = acs.acsc_GetLibraryVersion() + # TODO: Turn this into a string by extracting the bits appropriately + return resp + + def setVelocity(hcomm, axis: int, vel: float, wait=SYNCHRONOUS): """Sets axis velocity.""" call_acsc(acs.acsc_SetVelocity, hcomm, axis, double(vel), wait) diff --git a/acspy/tests/test_acsc.py b/acspy/tests/test_acsc.py new file mode 100644 index 0000000..a94f357 --- /dev/null +++ b/acspy/tests/test_acsc.py @@ -0,0 +1,13 @@ +"""Tests for ``acspy.acsc``.""" + +from acspy import acsc + + +def test_open_comm_simulator(): + hc = acsc.open_comm_simulator() + assert hc != -1 + + +def test_open_comm_direct(): + hc = acsc.open_comm_direct() + assert hc != -1