convert PCANBasic.c_ubyte_Array_8 in string

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
mcabon
Posts: 7
Joined: Mon 9. Sep 2019, 15:39

convert PCANBasic.c_ubyte_Array_8 in string

Post by mcabon » Thu 19. Sep 2019, 13:31

Hi Everyone,

I tried to converte my ('DATA', <class 'PCANBasic.c_ubyte_Array_8'>) in string for reading.

in fact i have a PCAN-USB connected on my CAN.
I have a programs in python for reading messages.
I tried :
bytes(DATA).decode('ascii') -> but this don't works because TypeError: cannot convert '_ctypes.PyCArrayType' object to bytes

Code: Select all

	# READING
	print ("CAN_Read")
	time.sleep(2)	
	canStatus = can.Read(channel1)
	if(canStatus[0] == PCAN_ERROR_OK):
		print ("NO ERROR - Read Msg :", canStatus[1]._fields_)
		ID = canStatus[1]._fields_[0][1]
		MSGTYPE = canStatus[1]._fields_[1][1]
		LEN = canStatus[1]._fields_[2][1]
		DATA = canStatus[1]._fields_[3][1]
		print ( "DATA :" , DATA )
	elif (canStatus[0] == PCAN_ERROR_QRCVEMPTY):
		print (" no MSG in a queue")
	else :
		print ("ERROR : ", canStatus)
so my programs return :

Code: Select all

result1:  (0, b'No Error')
No Error Init:
Getstatus:- 0
CAN_Read
NO ERROR - Read Msg : [('ID', <class 'ctypes.c_uint'>), ('MSGTYPE', <class 'ctypes.c_ubyte'>), ('LEN', <class 'ctypes.c_ubyte'>), ('DATA', <class 'PCANBasic.c_ubyte_Array_8'>)]
DATA : <class 'PCANBasic.c_ubyte_Array_8'>
End
 
SO my question is : how can i convert data in string ?

Thanks so much for your helps !

K.Wagner
Software Development
Software Development
Posts: 1082
Joined: Wed 22. Sep 2010, 13:36

Re: convert PCANBasic.c_ubyte_Array_8 in string

Post by K.Wagner » Thu 19. Sep 2019, 13:56

Hello,

just a reminder: this is mainly a programming related question, not to our APIs or to CAN. This forum is for CAN based protocols APIs and CAN devices from the company PEAK-System. Please visit forums for python and other programming languages to get help on problems like this in the future.

The DATA field of a TPCANMsg is a ctypes byte array. You need to extract the underlying bytearray object by using the python function "bytearray()"

Code: Select all

>>> msg = TPCANMsg()
>>> msg.DATA[0] = 0xFF
>>> msg.DATA[1] = 0x11

>>> temp1 = bytearray(msg.DATA)
>>> str(temp1)
This generates the following output:

Code: Select all

'\xFF\x11\x00\x00\x00\x00\x00\x00'
Best regards,
Keneth

mcabon
Posts: 7
Joined: Mon 9. Sep 2019, 15:39

Re: convert PCANBasic.c_ubyte_Array_8 in string

Post by mcabon » Thu 19. Sep 2019, 14:03

thanks so mush for your help and sorry for the wrong place to published my question.

Have a good day :)

Post Reply