Skip to content

Commit

Permalink
Add functions, tests, and start snake-casing
Browse files Browse the repository at this point in the history
  • Loading branch information
petebachant committed Mar 17, 2024
1 parent a735196 commit 15f8409
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
52 changes: 49 additions & 3 deletions acspy/acsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ctypes
import platform
import re
import warnings
from ctypes import byref, create_string_buffer

import numpy as np
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions acspy/tests/test_acsc.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 15f8409

Please sign in to comment.