* Added an error message
* Now using .dll from system path.
This commit is contained in:
parent
a04155be8c
commit
358a871493
3 changed files with 133 additions and 131 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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!"
|
||||
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue