Skip to content

Commit

Permalink
Began implementing python-can communication in CANBus interface class
Browse files Browse the repository at this point in the history
  • Loading branch information
JaidePorter authored and RatikKapoor committed Jan 23, 2021
1 parent 437ab04 commit 0556623
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/net/canbus.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
import can

class CANBusNet:
""" Placeholder CANBus interface class. """
""" The CANBus interface class allows for communication to occur between
the CANBus and systems project. """

def __init__(self):
pass
""" Creates and initializes an instance of the bus object.
interface - The name of the interface that the bus is using.
channel - The default channel used.
bitrate - The bitrate in bits/s to use by default.
"""

self.bus = can.interface.Bus(bustype='socketcan', channel='vcan0', bitrate=50000)

def send_message(self, a_id, is_extended, dataset):
""" Creates and sends a CAN message to the CANBus.
a_id - A frame identifier used by the CANBus for arbitration.
is_extended - Controls the size of the arbitration_id.
dataset - This parameter may consist of bytes or a list of integers to be sent.
"""

message = can.Message(arbitration_id=a_id, is_extended_id=is_extended, data=dataset)
try:
self.bus.send(message)
print("Message sent on {}".format(self.bus.channel_info))
except can.CanError:
print("Message not sent")

def receive_message(self):
""" Accepts and returns a message that is sent from the CANBus. """

message = self.bus.recv()
print(message)
return message

def stop(self):
pass
""" Shuts the bus down and carries out interface clean up. """

self.bus.shutdown()

0 comments on commit 0556623

Please sign in to comment.