Added method removeSignal(SignalLabel) to the message class, so that signals can be removed from the message.

This commit is contained in:
Christian Sltrop 2012-07-10 15:12:44 +02:00
parent 9ff6ce1301
commit 5bd3100d31
4 changed files with 45 additions and 22 deletions

View file

@ -6,7 +6,6 @@ Created on 06.07.2012
@author: Christian Sültrop
'''
#import CanSignal
import array
import sys
@ -14,15 +13,17 @@ class CanMessage(object):
'''
A CAN message.
A CAN message has an ID, a Length in bytes, an array of signals, and Data.
'''
# Class variables
A CAN message is represented by an ID, a Length in bytes, a CycleTime in ms, an array of Signals, and Data.
When calling the composeData() method, the data from all Signals is copied into the Data array of the message,
with respect to the signal begin and length definitions from the CanSignal objects in Signals.
'''
def __init__(self, MessageId, MessageLength, MessageCycleTime, Label):
'''
Create a new CanMessage object.
@param MessageId: The CAN ID. Valid range 0x00 to 0x7ff.
@param MessageLength: The message length in bytes.
@param messageCycleTime: Cycle time in ms.
@ -54,15 +55,21 @@ class CanMessage(object):
def addSignal(self, CanSignal):
'''
Add a Signal of type CanSignal to the list of signals.
@param CanSignal: The signal to add to the list of signals.
'''
self.Signals.update({CanSignal.Label: CanSignal})
def removeSignal(self, CanSignalLabel):
self.Signals.pop(CanSignalLabel)
def composeData(self):
'''
Takes the CanSignals from the list in self.Signals and copies the data that is stored in their Data fields into the Data field
of the CanMessage object. The position where to copy the data is defined in the Begin and Length fields of the CanSignal objects.
Takes the CanSignals from the list in self.Signals and copies
the data that is stored in their Data fields into the Data field
of the CanMessage object. The position where to copy the data
is defined in the Begin and Length fields of the CanSignal objects.
'''
for sigKey in self.Signals:
@ -77,16 +84,11 @@ class CanMessage(object):
tgtBitNo = (tgtBegin%8) # for the target (the CanMessage object) array
for i in range(0, sig.Length):
#print tgtByteNo
#print tgtBitNo
#print ''
# copy the source data bits to the target
# OR # get srcBitNo from srcByteNo and shift it to tgtBitNo
if tgtByteNo >= self.Length:
sys.exit('Signal does not fit into message!')
#print srcBitNo
tmp = sig.Data[srcByteNo]
tmp = tmp >> srcBitNo
tmp = tmp & 0x01
@ -97,7 +99,7 @@ class CanMessage(object):
# increment the counters
srcBitNo += 1
if srcBitNo >= 8: # on bit counter overflow, increment the byte counter
if srcBitNo >= 8: # on bit counter overflow, increment the byte counter and reset the bit counter
srcBitNo = 0
srcByteNo += 1

Binary file not shown.

View file

@ -6,31 +6,44 @@ Created on 06.07.2012
@author: Christan Sültrop
'''
import PCANBasic
import PCANBasic # PCANBasic wrapper library provided by PEAK
import sys
from PCANBasic import PCAN_BAUD_1M, PCAN_BAUD_125K
class PcanAdapter (object):
'''
A class for controlling a PEAK PCan adapter. Based on the PCANBasic library.
'''
# Class variables:
Baudrate = { '125k' : PCANBasic.PCAN_BAUD_125K,
'500k' : PCANBasic.PCAN_BAUD_500K,
'1000k' : PCANBasic.PCAN_BAUD_1M }
def __init__(self, Baudrate):
'''
Constructor.
@param Baudrate: Baudrate from the PcanAdapter.Baudrate dictionary
'''
# Object variables:
self.Channel = PCANBasic.PCAN_USBBUS1
self.Pcan = PCANBasic.PCANBasic()
self.Baudrate = Baudrate # Baudrate from PCANBasic
self.isInitialised = False
def __del__(self):
'''
Destructor.
Uninitializes the PCAN adapter.
'''
print '\nDestructor:'
self.uninitialize()
def initialize(self):
'''
Initializes the PCAN adapter.
'''
self.Pcan.Uninitialize(PCANBasic.PCAN_NONEBUS)
status = self.Pcan.Initialize(self.Channel, self.Baudrate)
if status != PCANBasic.PCAN_ERROR_OK:
@ -44,6 +57,9 @@ class PcanAdapter (object):
print 'DeviceNumber: ', hwId
def uninitialize(self):
'''
Unitialize the PCAN adapter.
'''
if self.isInitialised:
status = self.Pcan.Uninitialize(self.Channel)
if status != PCANBasic.PCAN_ERROR_OK:
@ -53,13 +69,18 @@ class PcanAdapter (object):
print("PCAN deinitialized")
def sendMessage(self, Message):
Message.composeData()
'''
Sends the CanMessage object Message onto the CAN bus.
@param Messge: The CanMessage object that is to be sent.
'''
Message.composeData() # compose message data array from the signals in Message.Signals.
msg = PCANBasic.TPCANMsg()
msg.ID = Message.Id
msg.MSGTYPE = PCANBasic.PCAN_MESSAGE_STANDARD
msg.LEN = Message.Length
msg.DATA[0:Message.Length] = Message.Data
msg = PCANBasic.TPCANMsg() # create a new PCAN message object
msg.ID = Message.Id # copy the ID
msg.MSGTYPE = PCANBasic.PCAN_MESSAGE_STANDARD # Message type is standard (not extended)
msg.LEN = Message.Length # copy the length
msg.DATA[0:Message.Length] = Message.Data # copy the message data into the PCAN messge object
self.Pcan.Write(self.Channel, msg)
self.Pcan.Write(self.Channel, msg) # write it onto the Bus
#print ('Message ' + Message.Label + ' written.')

Binary file not shown.