From 358a87149379eec1350d2313ac0a3e01befc3635 Mon Sep 17 00:00:00 2001 From: Christian Sueltrop Date: Mon, 21 Oct 2013 09:45:52 +0200 Subject: [PATCH] * Added an error message * Now using .dll from system path. --- CANLibrary/PCan.py | 261 ++++++++++++++++++++-------------------- PCANBasic/PCANBasic.py | 3 +- PCANBasic/PCANBasic.pyc | Bin 14948 -> 14926 bytes 3 files changed, 133 insertions(+), 131 deletions(-) diff --git a/CANLibrary/PCan.py b/CANLibrary/PCan.py index b27c906..1fa0148 100644 --- a/CANLibrary/PCan.py +++ b/CANLibrary/PCan.py @@ -1,130 +1,131 @@ -# -*- coding: UTF-8 -*- - -''' -Created on 06.07.2012 - -@author: Christian Sültrop -''' - -import PCANBasic # PCANBasic wrapper library provided by PEAK -import sys - - -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: Baud rate from the PcanAdapter. Baud rate dictionary - ''' - # Instance variables: - self.Messages = {} - self.Channel = PCANBasic.PCAN_USBBUS1 - self.Pcan = PCANBasic.PCANBasic() - self.Baudrate = Baudrate # Baud rate from PCANBasic - self.isInitialised = False - - def __del__(self): - ''' - Destructor. - Uninitialises the PCAN adapter. - ''' - print '\nDestructor:' - self.uninitialize() - - def addMessage(self, CanMessage): - ''' - Add a Message of type CanMessage to the list of messages. - - @param CanMessage: The message to add to the list of messages. - ''' - - self.Messages.update({CanMessage.Label: CanMessage}) - - def removeMessage(self, CanMessageLabel): - try: - self.Messages.pop(CanMessageLabel) - except: - pass - - - def clearMessages(self): - self.Messages = {} - - 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: - print 'Error: ', self.Pcan.GetErrorText(status, 0)[1] - sys.exit("PCAN initialisation error") - - print("PCAN initialised") - self.isInitialised = True - - channel, hwId = self.Pcan.GetValue( self.Channel, PCANBasic.PCAN_DEVICE_NUMBER ) - 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: - print 'Error: ', self.Pcan.GetErrorText(status, 0)[1] - sys.exit("PCAN deinitialisation error") - - print("PCAN deinitialised") - - def sendMessage(self, Message): - ''' - 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() # 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 message object - - self.Pcan.Write(self.Channel, msg) # write it onto the Bus - #print ('Message ' + Message.Label + ' written.') - - def receiveMessage(self): - ''' - Tries to receive a CAN message and puts its data into the according CanMessage object's Data field. - This method should be called frequently. - ''' - - while True: # do this while messages are read from the bus - result, msg, timestamp = self.Pcan.Read(self.Channel) - - if result == PCANBasic.PCAN_ERROR_OK: - # loop through the messages and look for one that matches the received message: - for msgKey in self.Messages: - if self.Messages[msgKey].Id == msg.ID: - if msg.LEN != self.Messages[msgKey].Length: - # an error message could be posted at this point - pass - else: - #print msgKey - for i in range(0, self.Messages[msgKey].Length): - self.Messages[msgKey].Data[i] = msg.DATA[i] - self.Messages[msgKey].decomposeData() - elif result == PCANBasic.PCAN_ERROR_QRCVEMPTY: - break +# -*- coding: UTF-8 -*- + +''' +Created on 06.07.2012 + +@author: Christian Sültrop +''' + +import PCANBasic # PCANBasic wrapper library provided by PEAK +import sys + + +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: Baud rate from the PcanAdapter. Baud rate dictionary + ''' + # Instance variables: + self.Messages = {} + self.Channel = PCANBasic.PCAN_USBBUS1 + self.Pcan = PCANBasic.PCANBasic() + self.Baudrate = Baudrate # Baud rate from PCANBasic + self.isInitialised = False + + def __del__(self): + ''' + Destructor. + Uninitialises the PCAN adapter. + ''' + print '\nDestructor:' + self.uninitialize() + + def addMessage(self, CanMessage): + ''' + Add a Message of type CanMessage to the list of messages. + + @param CanMessage: The message to add to the list of messages. + ''' + + self.Messages.update({CanMessage.Label: CanMessage}) + + def removeMessage(self, CanMessageLabel): + try: + self.Messages.pop(CanMessageLabel) + except: + pass + + + def clearMessages(self): + self.Messages = {} + + 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: + print 'Error: ', self.Pcan.GetErrorText(status, 0)[1] + sys.exit("PCAN initialisation error") + + print("PCAN initialised") + self.isInitialised = True + + channel, hwId = self.Pcan.GetValue( self.Channel, PCANBasic.PCAN_DEVICE_NUMBER ) + 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: + print 'Error: ', self.Pcan.GetErrorText(status, 0)[1] + sys.exit("PCAN deinitialisation error") + + print("PCAN deinitialised") + + def sendMessage(self, Message): + ''' + 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() # 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 message object + + self.Pcan.Write(self.Channel, msg) # write it onto the Bus + #print ('Message ' + Message.Label + ' written.') + + def receiveMessage(self): + ''' + Tries to receive a CAN message and puts its data into the according CanMessage object's Data field. + This method should be called frequently. + ''' + + while True: # do this while messages are read from the bus + result, msg, timestamp = self.Pcan.Read(self.Channel) + + if result == PCANBasic.PCAN_ERROR_OK: + # loop through the messages and look for one that matches the received message: + for msgKey in self.Messages: + if self.Messages[msgKey].Id == msg.ID: + if msg.LEN != self.Messages[msgKey].Length: + # an error message could be posted at this point + print 'ERROR: Message ID %s received, but length does not match definition.' % (hex(msg.ID)) + pass + else: + #print msgKey + for i in range(0, self.Messages[msgKey].Length): + self.Messages[msgKey].Data[i] = msg.DATA[i] + self.Messages[msgKey].decomposeData() + elif result == PCANBasic.PCAN_ERROR_QRCVEMPTY: + break diff --git a/PCANBasic/PCANBasic.py b/PCANBasic/PCANBasic.py index bafa69e..c89315a 100644 --- a/PCANBasic/PCANBasic.py +++ b/PCANBasic/PCANBasic.py @@ -227,7 +227,8 @@ class PCANBasic: def __init__(self): # Loads the PCANBasic.dll # - self.__m_dllBasic = windll.LoadLibrary("../PCANBasic/PCANBasic") + #self.__m_dllBasic = windll.LoadLibrary("../PCANBasic/PCANBasic") + self.__m_dllBasic = windll.LoadLibrary('PCANBasic') if self.__m_dllBasic == None: print "Exception: The PCAN-Basic DLL couldn't be loaded!" diff --git a/PCANBasic/PCANBasic.pyc b/PCANBasic/PCANBasic.pyc index 81ae74b43286c3358ed9123cb2b712687e67bb50..10571194bbcd525bd31ba99163a1d867f91b9f08 100644 GIT binary patch delta 113 zcmV-%0FM9Ubk1}K1M>|ElUiP}2m1>JQW5|F0FxaI3$xb} zN&*2Slh+bvvxpUn0RcU;6Boe)0aLTp8b<;FbF&>C^aBBlvl1b{1OcqG(j{920n@WC TDhUJu_>(v;+yM@=6)#T<0G%VN delta 135 zcmX?C@}z{F`7?iMy?7z9Ci^Uii81(e?1DqZGoDz#OlM$@R>^x5xnK!@TabsqD zv)Ndnmx+;U^FbjmCPwAS2Zho$7m0Q;GTLnZD|V2XF=+FCDR(BuqRs5GUzi!YHvduB i&%(HR^B&b`7RF1PCA2tM7{5