8000 socketcand Backend for python-can by domologic · Pull Request #1140 · hardbyte/python-can · GitHub
[go: up one dir, main page]

Skip to content

socketcand Backend for python-can #1140

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

Merged
merged 14 commits into from
Dec 6, 2021
Prev Previous commit
Next Next commit
reformatted using https://black.vercel.app/
  • Loading branch information
telkamp committed Nov 26, 2021
commit c2041dedf0cd93400f39ca651d2588018e1fb5da
8000
11 changes: 3 additions & 8 deletions can/interfaces/socketcand/socketcand.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

log = logging.getLogger(__name__)


def convert_ascii_message_to_can_message(ascii_msg: str) -> can.Message:
if not ascii_msg.startswith("< frame ") or not ascii_msg.endswith(" >"):
log.warning(f"Could not parse ascii message: {ascii_msg}")
Expand Down Expand Up @@ -86,7 +87,7 @@ def __init__(self, channel, host, port, can_filters=None, **kwargs):
self.__receive_buffer = "" # i know string is not the most efficient here
connect_to_server(self.__socket, self.__host, self.__port)
self._expect_msg("< hi >")

log.info(
f"SocketCanDaemonBus: connected with address {self.__socket.getsockname()}"
)
Expand Down Expand Up @@ -170,25 +171,19 @@ def _recv_internal(self, timeout):
log.error(f"Failed to receive: {exc} {traceback.format_exc()}")
raise can.CanError(f"Failed to receive: {exc} {traceback.format_exc()}")


def _tcp_send(self, msg: str):
log.debug(f"Sending TCP Message: '{msg}'")
self.__socket.sendall(msg.encode("ascii"))


def _expect_msg(self, msg):
ascii_msg = self.__socket.recv(256).decode(
"ascii"
)
ascii_msg = self.__socket.recv(256).decode("ascii")
if not ascii_msg == msg:
raise can.CanError(f"{msg} message expected!")


def send(self, msg, timeout=None):
ascii_msg = convert_can_message_to_ascii_message(msg)
self._tcp_send(ascii_msg)


def shutdown(self):
self.stop_all_periodic_tasks()
self.__socket.close()
0