-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_frame_manager.py
More file actions
110 lines (99 loc) · 4.15 KB
/
Copy pathsend_frame_manager.py
File metadata and controls
110 lines (99 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import can
import time
import socket
import pickle
import struct
import serial
from can_enums import connect_enum
class SendFrameManager:
def __init__(self, connection_manager, can_message_queue):
"""
Initialize the SendFrameManager.
:param can_bus: CAN bus object to send messages.
:param can_message_queue: Queue to store CAN messages for processing.
"""
self.connection_manager = connection_manager
self.can_message_queue = can_message_queue
def send_frame(self, can_id, is_extended, is_rtr, dlc, data):
bus = self.connection_manager.get_active_bus()
connection_type = self.connection_manager.get_connection_type()
if not bus and connection_type != connect_enum.SOCKETSERVER:
print("[ERROR] CAN bus is not initialized.")
return False, "CAN bus is not initialized."
try:
if connection_type == connect_enum.ACAN:
stx = 0xAA
etx = 0xBB
timestamp = int(time.time())
frame = struct.pack(
"<BIBI8sB",
stx,
timestamp,
dlc,
can_id,
bytes(data).ljust(8, b'\x00'),
etx
)
bus.write(frame)
message = can.Message(
timestamp=timestamp,
arbitration_id=can_id,
is_extended_id=is_extended,
is_remote_frame=is_rtr,
dlc=dlc,
data=bytearray(data),
is_rx = False
)
message.is_rx = False
self.can_message_queue.put(message)
return True, "Frame sent successfully (ACAN)."
elif connection_type == connect_enum.PCAN:
message = can.Message(
arbitration_id=can_id,
is_extended_id=is_extended,
is_remote_frame=is_rtr,
dlc=dlc,
data=data
)
bus.send(message)
message.timestamp = time.time()
message.is_rx = False
self.can_message_queue.put(message)
return True, "Frame sent successfully."
elif connection_type == connect_enum.SOCKETSERVER:
udp_socket = self.connection_manager.get_active_bus()
client_address = getattr(self.connection_manager, "client_address", None)
if not udp_socket:
print("[ERROR] UDP socket is not initialized.")
return False, "UDP socket is not initialized."
if not client_address:
print("[ERROR] Client address is not set. Awaiting initial connection.")
return False, "Client address is not set."
message = can.Message(
arbitration_id=can_id,
is_extended_id=is_extended,
is_remote_frame=is_rtr,
dlc=dlc,
data=bytearray(data),
is_rx=False,
timestamp=time.time()
)
frame_bytes = pickle.dumps(message)
udp_socket.sendto(frame_bytes, client_address)
self.can_message_queue.put(message)
return True, "Frame sent successfully (UDP)."
else:
print("[ERROR] Unsupported bus type for sending.")
return False, "Unsupported bus type."
except can.CanError as e:
print(f"[ERROR] Failed to send CAN frame to bus: {e}")
return False, f"Failed to send frame to bus: {e}"
except Exception as e:
print(f"[ERROR] Failed to process CAN frame: {e}")
return False, f"Failed to process frame: {e}"
def set_connection_type(self, connection_type):
if connection_type not in connect_enum:
self.connection_type = connect_enum.NONE
return False
self.connection_type = connection_type
return True