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
parse < hi > & < ok > messages
  • Loading branch information
telkamp committed Nov 24, 2021
commit e14987c3501eda5531a2c48aadc6ee5429204dd0
8000
49 changes: 32 additions & 17 deletions can/interfaces/socketcand/socketcand.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Interface to socketcand
see https://github.com/linux-can/socketcand

Author: Marvin Seiler
Author: Marvin Seiler, Gerrit Telkamp

Copyright (C) 2021 DOMOLOGIC GmbH
http://www.domologic.de
Expand All @@ -17,13 +17,13 @@

log = logging.getLogger(__name__)

def convert_ascii_message_to_can_message(ascii_message: str) -> can.Message:
if not ascii_message.startswith("< frame ") or not ascii_message.endswith(" >"):
log.warning(f"Could not parse ascii message: {ascii_message}")
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}")
return None
else:
# frame_string = ascii_message.removeprefix("< frame ").removesuffix(" >")
frame_string = ascii_message[8:-2]
# frame_string = ascii_msg.removeprefix("< frame ").removesuffix(" >")
frame_string = ascii_msg[8:-2]
parts = frame_string.split(" ", 3)
can_id, timestamp = int(parts[0], 16), float(parts[1])

Expand All @@ -43,8 +43,8 @@ def convert_can_message_to_ascii_message(can_message: can.Message) -> str:
data = can_message.data
length = can_message.dlc
bytes_string = " ".join("{:x}".format(x) for x in data[0:length])
ascii_message = f"< send {can_id:X} {length:X} {bytes_string} >"
return ascii_message
ascii_msg = f"< send {can_id:X} {length:X} {bytes_string} >"
return ascii_msg


def connect_to_server(s, host, port):
Expand All @@ -71,11 +71,15 @@ def __init__(self, channel, host, port, can_filters=None, **kwargs):
self.__message_buffer = deque()
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()}"
)
self._tcp_send(f"< open {channel} >")
self._expect_msg("< ok >")
self._tcp_send(f"< rawmode >")
self._expect_msg("< ok >")
super().__init__(channel=channel, can_filters=can_filters)

def _recv_internal(self, timeout):
Expand All @@ -100,11 +104,11 @@ def _recv_internal(self, timeout):
log.debug("Socket not ready")
return None, False

ascii_message = self.__socket.recv(1024).decode(
ascii_msg = self.__socket.recv(1024).decode(
"ascii"
) # may contain multiple messages
self.__receive_buffer += ascii_message
log.debug(f"Received Ascii Message: {ascii_message}")
self.__receive_buffer += ascii_msg
log.debug(f"Received Ascii Message: {ascii_msg}")
buffer_view = self.__receive_buffer
chars_processed_successfully = 0
while True:
Expand Down Expand Up @@ -152,13 +156,24 @@ 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, message: str):
log.debug(f"Sending Tcp Message: '{message}'")
self.__socket.sendall(message.encode("ascii"))

def send(self, message, timeout=None):
ascii_message = convert_can_message_to_ascii_message(message)
self._tcp_send(ascii_message)
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"
)
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()
Expand Down
0