Hi every one,
I am new to this python programming.
Recently, I have started working with python programming to communicate with PCAN-usb.
I was able to Initialize PCAN, write message but i am unable to receive the data from it.
__________________________
but while using the PCAN view I am able to write and read the data but not with python programming.
I am expecting the following data when a particular message is send as shown below.
Attachment 32727
____________________________________________
can you please help out to fix this issue in my programming.
Thankingyou
_____________________________________________________________________________________________________________
import pycan.drivers.canusb as driver
from pycan.common import CANMessage
from ctypes import *
_____________________________________________________________________________________________________
# Represents a PCAN message
class TPCANMsg (Structure):
"""
Represents a PCAN message
"""
_fields_ = [ ("ID",c_ulong), ("MSGTYPE", TPCANMessageType), ("LEN", c_ubyte),("DATA", c_ubyte * 8) ]
____________________________________________________________________________________________________
class TPCANTimestamp (Structure):
"""
Represents a timestamp of a received PCAN message
Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow
"""
_fields_ = [ ("millis", c_ulong),("millis_overflow", c_ushort),("micros", c_ushort) ]
---------------------------------------------------------------------------------------------------------------------
#///////////////////////////////////////////////////////////
# PCAN-Basic API function declarations
#///////////////////////////////////////////////////////////
# PCAN-Basic API class implementation
#
class PCANBasic:
"""
PCAN-Basic API class implementation
"""
def __init__(self):
# Loads the PCANBasic.dll
#
self.__m_dllBasic = windll.LoadLibrary("PCANBasic")
if self.__m_dllBasic == None:
print "Exception: The PCAN-Basic DLL couldn't be loaded!"
# Initializes a PCAN Channel
#
def Initialize(self,Channel,Btr0Btr1,HwType = TPCANType(0),IOPort = c_uint(0),Interrupt = c_ushort(0)):
"""
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Initialize(Channel,Btr0Btr1,HwType,IOPort,Interrupt)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Initialize"
raise
# Uninitializes one or all PCAN Channels initialized by CAN_Initialize
#
def Uninitialize(self,Channel):
"""
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Uninitialize(Channel)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Uninitialize"
raise
# Resets the receive and transmit queues of the PCAN Channel
#
def Reset(self,Channel):
"""
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Reset(Channel)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Reset"
raise
# Retrieves a PCAN Channel value
#
def GetValue(self,Channel,Parameter):
"""
Returns:
A touple with 2 values
"""
try:
if Parameter == PCAN_API_VERSION or Parameter == PCAN_HARDWARE_NAME or Parameter == PCAN_CHANNEL_VERSION or Parameter == PCAN_LOG_LOCATION or Parameter == PCAN_TRACE_LOCATION:
mybuffer = create_string_buffer(256)
else:
mybuffer = c_int(0)
res = self.__m_dllBasic.CAN_GetValue(Channel,Parameter,byref(mybuffer),sizeof(mybuffer))
return TPCANStatus(res),mybuffer.value
except:
print "Exception on PCANBasic.GetValue"
raise
# Gets the current status of a PCAN Channel
#
def GetStatus(self,Channel):
"""
Gets the current status of a PCAN Channel
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_GetStatus(Channel)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.GetStatus"
raise
def Read(self,Channel):
"""
Reads a CAN message from the receive queue of a PCAN Channel
Remarks:
The return value of this method is a 3-touple, where
the first value is the result (TPCANStatus) of the method.
The order of the values are:
[0]: A TPCANStatus error code
[1]: A TPCANMsg structure with the CAN message read
[2]: A TPCANTimestamp structure with the time when a message was read
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Returns:
A touple with three values
"""
try:
msg = TPCANMsg()
timestamp = TPCANTimestamp()
print "inner function:-", msg
res = self.__m_dllBasic.CAN_Read(Channel,byref(msg),byref(timestamp))
return TPCANStatus(res),msg,timestamp
except:
print "Exception on PCANBasic.Read"
raise
# Transmits a CAN message
#
def Write(self,Channel,MessageBuffer):
"""
Transmits a CAN message
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
MessageBuffer: A TPCANMsg representing the CAN message to be sent
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Write(Channel,byref(MessageBuffer))
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Write"
raise
def GetErrorText(self,Error,Language = 0):
"""
Configures or sets a PCAN Channel value
Remarks:
The current languages available for translation are:
Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
Italian (0x10) and French (0x0C)
The return value of this method is a 2-touple, where
the first value is the result (TPCANStatus) of the method and
the second one, the error text
Parameters:
Error : A TPCANStatus error code
Language : Indicates a 'Primary language ID' (Default is Neutral(0))
Returns:
A touple with 2 values
"""
try:
mybuffer = create_string_buffer(256)
res = self.__m_dllBasic.CAN_GetErrorText(Error,Language,byref(mybuffer))
return TPCANStatus(res),mybuffer.value
except:
print "Exception on PCANBasic.GetErrorText"
raise
#____________________________________________________________________________________________________________
if __name__ == '__main__':
msg = TPCANMsg()
tmstamp = TPCANTimestamp()
data = 0x22,0xF1,0x51
msg._fields_ = [ ("ID",0x602),("MSGTYPE", PCAN_MESSAGE_STANDARD),("LEN", 3),("DATA", data) ]
#______________________________________________________________________________________________
#----------------Initializ---------------------------
#_______________________________________________________________________________________________
can = PCANBasic()
result = can.Initialize(PCAN_USBBUS1, PCAN_BAUD_500K)
if result == PCAN_ERROR_OK:
print "Getstatus:-",can.GetStatus(PCAN_USBBUS1)
#______________________________________________________________________________________________
#--------------Writing/Sending _data------------------
#______________________________________________________________________________________________
wrtstat= can.Write(PCAN_USBBUS1,msg)
if (wrtstat == PCAN_ERROR_OK):
print "Message was sent"
else:
print "Error Occured during writing"
#__________________________________________________________________________________________________
#-------------Reading----------------------------------
#_________________________________________________________________________________________________
status = PCAN_ERROR_QRCVEMPTY
print "beforeloop:-",status
while (status != PCAN_ERROR_OK ):
status,msg,time = can.Read(PCAN_USBBUS1)
can.FilterMessages(PCAN_USBBUS1, 0x400, 0x580, PCAN_MESSAGE_STANDARD)
print "inside loop:-", status
if (status == PCAN_ERROR_OK):
print "Message received"
#__________________________________________________________________________________________________
else:
print "Init error!"
print "End"
can.Uninitialize(PCAN_USBBUS1)
I am new to this python programming.
Recently, I have started working with python programming to communicate with PCAN-usb.
I was able to Initialize PCAN, write message but i am unable to receive the data from it.
__________________________
but while using the PCAN view I am able to write and read the data but not with python programming.
I am expecting the following data when a particular message is send as shown below.
Attachment 32727
____________________________________________
can you please help out to fix this issue in my programming.
Thankingyou
_____________________________________________________________________________________________________________
import pycan.drivers.canusb as driver
from pycan.common import CANMessage
from ctypes import *
_____________________________________________________________________________________________________
# Represents a PCAN message
class TPCANMsg (Structure):
"""
Represents a PCAN message
"""
_fields_ = [ ("ID",c_ulong), ("MSGTYPE", TPCANMessageType), ("LEN", c_ubyte),("DATA", c_ubyte * 8) ]
____________________________________________________________________________________________________
class TPCANTimestamp (Structure):
"""
Represents a timestamp of a received PCAN message
Total Microseconds = micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow
"""
_fields_ = [ ("millis", c_ulong),("millis_overflow", c_ushort),("micros", c_ushort) ]
---------------------------------------------------------------------------------------------------------------------
#///////////////////////////////////////////////////////////
# PCAN-Basic API function declarations
#///////////////////////////////////////////////////////////
# PCAN-Basic API class implementation
#
class PCANBasic:
"""
PCAN-Basic API class implementation
"""
def __init__(self):
# Loads the PCANBasic.dll
#
self.__m_dllBasic = windll.LoadLibrary("PCANBasic")
if self.__m_dllBasic == None:
print "Exception: The PCAN-Basic DLL couldn't be loaded!"
# Initializes a PCAN Channel
#
def Initialize(self,Channel,Btr0Btr1,HwType = TPCANType(0),IOPort = c_uint(0),Interrupt = c_ushort(0)):
"""
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Initialize(Channel,Btr0Btr1,HwType,IOPort,Interrupt)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Initialize"
raise
# Uninitializes one or all PCAN Channels initialized by CAN_Initialize
#
def Uninitialize(self,Channel):
"""
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Uninitialize(Channel)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Uninitialize"
raise
# Resets the receive and transmit queues of the PCAN Channel
#
def Reset(self,Channel):
"""
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Reset(Channel)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Reset"
raise
# Retrieves a PCAN Channel value
#
def GetValue(self,Channel,Parameter):
"""
Returns:
A touple with 2 values
"""
try:
if Parameter == PCAN_API_VERSION or Parameter == PCAN_HARDWARE_NAME or Parameter == PCAN_CHANNEL_VERSION or Parameter == PCAN_LOG_LOCATION or Parameter == PCAN_TRACE_LOCATION:
mybuffer = create_string_buffer(256)
else:
mybuffer = c_int(0)
res = self.__m_dllBasic.CAN_GetValue(Channel,Parameter,byref(mybuffer),sizeof(mybuffer))
return TPCANStatus(res),mybuffer.value
except:
print "Exception on PCANBasic.GetValue"
raise
# Gets the current status of a PCAN Channel
#
def GetStatus(self,Channel):
"""
Gets the current status of a PCAN Channel
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_GetStatus(Channel)
return TPCANStatus(res)
except:
print "Exception on PCANBasic.GetStatus"
raise
def Read(self,Channel):
"""
Reads a CAN message from the receive queue of a PCAN Channel
Remarks:
The return value of this method is a 3-touple, where
the first value is the result (TPCANStatus) of the method.
The order of the values are:
[0]: A TPCANStatus error code
[1]: A TPCANMsg structure with the CAN message read
[2]: A TPCANTimestamp structure with the time when a message was read
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
Returns:
A touple with three values
"""
try:
msg = TPCANMsg()
timestamp = TPCANTimestamp()
print "inner function:-", msg
res = self.__m_dllBasic.CAN_Read(Channel,byref(msg),byref(timestamp))
return TPCANStatus(res),msg,timestamp
except:
print "Exception on PCANBasic.Read"
raise
# Transmits a CAN message
#
def Write(self,Channel,MessageBuffer):
"""
Transmits a CAN message
Parameters:
Channel : A TPCANHandle representing a PCAN Channel
MessageBuffer: A TPCANMsg representing the CAN message to be sent
Returns:
A TPCANStatus error code
"""
try:
res = self.__m_dllBasic.CAN_Write(Channel,byref(MessageBuffer))
return TPCANStatus(res)
except:
print "Exception on PCANBasic.Write"
raise
def GetErrorText(self,Error,Language = 0):
"""
Configures or sets a PCAN Channel value
Remarks:
The current languages available for translation are:
Neutral (0x00), German (0x07), English (0x09), Spanish (0x0A),
Italian (0x10) and French (0x0C)
The return value of this method is a 2-touple, where
the first value is the result (TPCANStatus) of the method and
the second one, the error text
Parameters:
Error : A TPCANStatus error code
Language : Indicates a 'Primary language ID' (Default is Neutral(0))
Returns:
A touple with 2 values
"""
try:
mybuffer = create_string_buffer(256)
res = self.__m_dllBasic.CAN_GetErrorText(Error,Language,byref(mybuffer))
return TPCANStatus(res),mybuffer.value
except:
print "Exception on PCANBasic.GetErrorText"
raise
#____________________________________________________________________________________________________________
if __name__ == '__main__':
msg = TPCANMsg()
tmstamp = TPCANTimestamp()
data = 0x22,0xF1,0x51
msg._fields_ = [ ("ID",0x602),("MSGTYPE", PCAN_MESSAGE_STANDARD),("LEN", 3),("DATA", data) ]
#______________________________________________________________________________________________
#----------------Initializ---------------------------
#_______________________________________________________________________________________________
can = PCANBasic()
result = can.Initialize(PCAN_USBBUS1, PCAN_BAUD_500K)
if result == PCAN_ERROR_OK:
print "Getstatus:-",can.GetStatus(PCAN_USBBUS1)
#______________________________________________________________________________________________
#--------------Writing/Sending _data------------------
#______________________________________________________________________________________________
wrtstat= can.Write(PCAN_USBBUS1,msg)
if (wrtstat == PCAN_ERROR_OK):
print "Message was sent"
else:
print "Error Occured during writing"
#__________________________________________________________________________________________________
#-------------Reading----------------------------------
#_________________________________________________________________________________________________
status = PCAN_ERROR_QRCVEMPTY
print "beforeloop:-",status
while (status != PCAN_ERROR_OK ):
status,msg,time = can.Read(PCAN_USBBUS1)
can.FilterMessages(PCAN_USBBUS1, 0x400, 0x580, PCAN_MESSAGE_STANDARD)
print "inside loop:-", status
if (status == PCAN_ERROR_OK):
print "Message received"
#__________________________________________________________________________________________________
else:
print "Init error!"
print "End"
can.Uninitialize(PCAN_USBBUS1)