Page 1 of 1

Python CAN-ID Message Problem

Posted: Fri 11. Dec 2020, 13:49
by Bobity
PCAN Python Problem….

I’m running Windows 10 and Pycharm 2020.3 (a python IDE) & using Neurofield64 which is software that allows the control of various Neurofield eeg recording and stimulation devices, which work using a PEAK USB/CAN adapter. A Neurofield Z3 is a tACS/tDCS/tRNS brain stimulation device. I want to be able to directly control a Z3 from my Python code.

So, using PCAN Explorer 6, I traced Neurofield64 running a Z3 protocol, which got the Z3 to produce a range of stimulation over time. While the Neurofield64 Z3 protocol was running, I monitored the output of the Z3 using a Picoscope desktop oscilloscope, so I can see that the Z3 is producing expected signals.

I exported the trace as a csv in PCAN Explorer the first line of which is here :

Index Time Offset [ms] Bus CAN-ID (hex) Symbol Rx/Tx Type Length Data
1 20892.54 1 610901 Rx Data 8 00 00 00 80 00 00 00 00


If I replay the traced recording of the Neurofield64 protocol within PCAN Explorer 6, I get the same results from the Z3 as I do running the original Neurofield64 Z3 protocol. I can see this from the output of the Picoscope.

If I attempt to send a single line of the same trace data, as a message containing the CAN-ID & the data in a simple Python program (see below) I see a different CAN-ID (x101) and it appears the message does not get to the Z3:



Time Offset [ms] Bus CAN-ID (hex) Symbol Rx/Tx Type Length Data
1 797788.8 1 101 Rx Data 8 00 00 00 80 00 00 00 00

Any idea what I’m doing wrong?

Thanks

Rob

Code: Select all

from PCANBasic import *
channel = PCAN_USBBUS1
baud = PCAN_BAUD_500K

## The Plug & Play Channel (PCAN-USB) is initialized
##
#objPCAN =# The Plug & Play Channel (PCAN-USB) is initialized
#
objPCAN = PCANBasic()
result = objPCAN.Initialize(channel, baud)
if result != PCAN_ERROR_OK:
	# An error occured, get a text describing the error and show it
	#
	result = objPCAN.GetErrorText(result)
	print(result[1])
	channel = PCAN_USBBUS2
	objPCAN = PCANBasic()
	result = objPCAN.Initialize(channel, baud)
	if result != PCAN_ERROR_OK:
		# An error occured, get a text describing the error and show it
		#
		result = objPCAN.GetErrorText(result)
		print(result[1])
	else:
		print("PCAN-USBx was initialized")
else:
	print("PCAN-USBx was initialized")

	# Check the status of the USB Channel
	#
	result = objPCAN.GetStatus(channel)
if result == PCAN_ERROR_BUSLIGHT:
	print("PCAN-USB (Ch-x): Handling a BUS-LIGHT status...")
elif result == PCAN_ERROR_BUSHEAVY:
	print("PCAN-USB (Ch-x): Handling a BUS-HEAVY status...")
elif result == PCAN_ERROR_BUSOFF:
	print("PCAN-USB (Ch-x): Handling a BUS-OFF status...")
elif result == PCAN_ERROR_OK:
	print("PCAN_USB (Ch-x): Status is OK")
else:
	# An error occured, get a text describing the error and show it
	#
	result = objPCAN.GetErrorText(result)
	print(result[1])

msg = TPCANMsg()
msg.ID = 0x610901
msg.MSGTYPE = PCAN_MESSAGE_STANDARD
msg.LEN = 8
msg.DATA[0] = 0x00
msg.DATA[1] = 0x00
msg.DATA[2] = 0x00
msg.DATA[3] = 0x80
msg.DATA[4] = 0x00
msg.DATA[5] = 0x00
msg.DATA[6] = 0x00
msg.DATA[7] = 0x00
#
result = objPCAN.Write(channel, msg)
if result != PCAN_ERROR_OK:
	# An error occured, get a text describing the error and show it
	#
	result = objPCAN.GetErrorText(result)
	print(result)
else:
	print("Message sent successfully")
	
	# All initialized channgels are released
	#
	result = objPCAN.Uninitialize(PCAN_NONEBUS)
if result != PCAN_ERROR_OK:
	# An error occured, get a text describing the error and show it
	#
	result = objPCAN.GetErrorText(result)
	print(result)
else:
	print("PCAN-USBx was UN-initialized")

Re: Python CAN-ID Message Problem

Posted: Fri 11. Dec 2020, 14:33
by M.Heidemann
Hello,

Thank you for your request,

You used the TPCANMessageType "PCAN_MESSAGE_STANDARD"(0x00) which represents a standard CAN-Message (11-bit ID).
You have given an CAN-ID too large for this message type and it will resolve to CAN-ID 0x101h,
when using 29-bit CAN ID, please use the TPCANMessageType "PCAN_MESSAGE_EXTENDED"( 0x02).
PCANBasicMessagetype.PNG
PCANBasicMessagetype.PNG (45.07 KiB) Viewed 613 times
I have corrected your code and the message will be received as expected:

Code: Select all

from PCANBasic import *
channel = PCAN_USBBUS1
baud = PCAN_BAUD_500K

## The Plug & Play Channel (PCAN-USB) is initialized
##
#objPCAN =# The Plug & Play Channel (PCAN-USB) is initialized
#
objPCAN = PCANBasic()
result = objPCAN.Initialize(channel, baud)
if result != PCAN_ERROR_OK:
	# An error occured, get a text describing the error and show it
	#
	result = objPCAN.GetErrorText(result)
	print(result[1])
	channel = PCAN_USBBUS2
	objPCAN = PCANBasic()
	result = objPCAN.Initialize(channel, baud)
	if result != PCAN_ERROR_OK:
		# An error occured, get a text describing the error and show it
		#
		result = objPCAN.GetErrorText(result)
		print(result[1])
	else:
		print("PCAN-USBx was initialized")
else:
	print("PCAN-USBx was initialized")

	# Check the status of the USB Channel
	#
	result = objPCAN.GetStatus(channel)
if result == PCAN_ERROR_BUSLIGHT:
	print("PCAN-USB (Ch-x): Handling a BUS-LIGHT status...")
elif result == PCAN_ERROR_BUSHEAVY:
	print("PCAN-USB (Ch-x): Handling a BUS-HEAVY status...")
elif result == PCAN_ERROR_BUSOFF:
	print("PCAN-USB (Ch-x): Handling a BUS-OFF status...")
elif result == PCAN_ERROR_OK:
	print("PCAN_USB (Ch-x): Status is OK")
else:
	# An error occured, get a text describing the error and show it
	#
	result = objPCAN.GetErrorText(result)
	print(result[1])

msg = TPCANMsg()
msg.ID = 0x610901
msg.MSGTYPE = PCAN_MESSAGE_EXTENDED
msg.LEN = 8
msg.DATA[0] = 0x00
msg.DATA[1] = 0x00
msg.DATA[2] = 0x00
msg.DATA[3] = 0x80
msg.DATA[4] = 0x00
msg.DATA[5] = 0x00
msg.DATA[6] = 0x00
msg.DATA[7] = 0x00
#
result = objPCAN.Write(channel, msg)
if result != PCAN_ERROR_OK:
	# An error occured, get a text describing the error and show it
	#
	result = objPCAN.GetErrorText(result)
	print(result)
else:
	print("Message sent successfully")
	
	# All initialized channgels are released
	#
	result = objPCAN.Uninitialize(PCAN_NONEBUS)
if result != PCAN_ERROR_OK:
	# An error occured, get a text describing the error and show it
	#
	result = objPCAN.GetErrorText(result)
	print(result)
else:
	print("PCAN-USBx was UN-initialized")
PythonMessageTypeTest.PNG
PythonMessageTypeTest.PNG (22.03 KiB) Viewed 613 times
Please report back to me if this solved your issue.

Best Regards

Marvin

Re: Python CAN-ID Message Problem

Posted: Fri 11. Dec 2020, 14:49
by Bobity
Thanks Marvin - that worked a treat :)