Hi,
I want send a message through the PCAN-USB every 50ms using Python. Cycle time should not change or fluctuate. Which code I should use?
Thanks for reply.
Constant Cycle time using Python
-
- Sales & Support
- Posts: 1083
- Joined: Fri 20. Sep 2019, 13:31
Re: Constant Cycle time using Python
Hello,
Are you using the PCAN-Basic API?
If you use something else, can you tell us what exactly?
Best Regards
Marvin
Are you using the PCAN-Basic API?
If you use something else, can you tell us what exactly?
Best Regards
Marvin
---
Marvin Heidemann
PEAK-Support Team
Marvin Heidemann
PEAK-Support Team
Re: Constant Cycle time using Python
Yes, I am using PCAN-Basic API for linux.
I try this code :
It stops after 200 seconds. How can i send an infinite number of messages?
I try this code :
Code: Select all
import logging
import time
import can
from can.interface import Bus
can.rc['interface']='pcan'
can.rc['channel']='PCAN_USBBUS1'
can.rc['bitrate']=500000
bus=Bus()
logging.basicConfig(level=logging.INFO)
def simple_periodic_send(bus):
"""sends a message every 20ms with no explicit timeout
sleeps for 2 seconds then stops the task"""
print("starting to send a message every 200ms for 2s")
msg=can.Message(arbitration_id=0x123,data=[1,2,3,4,5,6],extended_id=False)
task=bus.send_periodic(msg,0.20)
assert isinstance(task,can.CyclicSendTaskABC)
time.sleep(200)
task.stop()
print("Stopped cyclic send")
return task
simple_periodic_send(bus)
It stops after 200 seconds. How can i send an infinite number of messages?
-
- Sales & Support
- Posts: 1083
- Joined: Fri 20. Sep 2019, 13:31
Re: Constant Cycle time using Python
Hello,
Since "simple_periodic_send" returns the task,
how about commenting out:
?
Assign the return value to a variable and trigger the
task to stop externally by variable.stop()?
furthermore, this is python-can, which we are not affiliated with in any way.
my suggestion is based off what i can see, i am not familiar with python-can.
Please report back to me if this was able to solve your issue.
Since "simple_periodic_send" returns the task,
how about commenting out:
Code: Select all
time.sleep(200)
task.stop()
Assign the return value to a variable and trigger the
task to stop externally by variable.stop()?
furthermore, this is python-can, which we are not affiliated with in any way.
my suggestion is based off what i can see, i am not familiar with python-can.
Please report back to me if this was able to solve your issue.
---
Marvin Heidemann
PEAK-Support Team
Marvin Heidemann
PEAK-Support Team
Re: Constant Cycle time using Python
Okay,
I think I partially solve the problem. When I comment out just the time.sleep(1) , It doesn't give me exact cycle time but it gives me kindly constant value. (around 65ms, instead of 200ms)

On the other hand, When I don't comment out the time.sleep(1), It gives me perfect cycle time but it freezes the camera. (I am using object detection program that it sends message when the program detects the people)
My code:
What do you mean with trigger the task to stop?
How can I solve the problem without freezes the camera?
I think I partially solve the problem. When I comment out just the time.sleep(1) , It doesn't give me exact cycle time but it gives me kindly constant value. (around 65ms, instead of 200ms)

On the other hand, When I don't comment out the time.sleep(1), It gives me perfect cycle time but it freezes the camera. (I am using object detection program that it sends message when the program detects the people)
My code:
Code: Select all
import jetson.inference
import jetson.utils
import time
import os
import can
from can.interface import Bus
import logging
can.rc['interface']='pcan'
can.rc['channel']='PCAN_USBBUS1'
can.rc['bitrate']=500000
bus=Bus()
logging.basicConfig(level=logging.INFO)
net=jetson.inference.detectNet("ssd-mobilenet-v2",threshold=0.5)
camera=jetson.utils.gstCamera(224,224,"0")
display=jetson.utils.glDisplay()
def simple_periodic_send(bus):
"""sends a message every 20ms with no explicit timeout sleeps for 2 seconds then stops the task"""
print("starting to send a message every 200ms for 2sc")
msg=can.Message(arbitration_id=0x123,data=[1,2,3,4,5,6],extended_id=False)
task=bus.send_periodic(msg,period=0.2)
assert isinstance(task,can.CyclicSendTaskABC)
time.sleep(1)
task.stop()
print("stopped cyclic send")
return task
while display.IsOpen():
img,width,height=camera.CaptureRGBA()
detections=net.Detect(img,width,height)
display.RenderOnce(img,width,height)
display.SetTitle("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))
for detection in detections:
if detection.ClassID==1:
simple_periodic_send(bus)
What do you mean with trigger the task to stop?
How can I solve the problem without freezes the camera?
-
- Sales & Support
- Posts: 1083
- Joined: Fri 20. Sep 2019, 13:31
Re: Constant Cycle time using Python
Hello,
Please read the comments in the code you have provided...
This pauses the application:
Not the same, you do not want to control the transmit-cycle by pausing your application.
i do not see how you end up with 65ms (Should be ~20ms) cycle time, but then again i do not know your setup exactly.
There is a "task.stop()" which stops the sending task, the sending task however is returned by the "simple_periodic_send"-function,
which means you can assign the return value of that function to a variable, which you then can use to call the "Stop"-method.
This means you could use another function, a condition to call "Stop()" on the variable, which you assigned the returned task to.
And - even though i have already mentioned it - you are using python-can, we did not develop python-can and therefore
cannot provide proper support for it. We know how our PCANBasic API works, python-can uses PCANBasic as an interface, that does not mean that we know how python-can handles things.
Best Regards
Marvin
Please read the comments in the code you have provided...
Code: Select all
import logging
import time
import can
from can.interface import Bus
can.rc['interface']='pcan'
can.rc['channel']='PCAN_USBBUS1'
can.rc['bitrate']=500000
bus=Bus()
logging.basicConfig(level=logging.INFO)
def simple_periodic_send(bus):
"""sends a message every 20ms with no explicit timeout
sleeps for 2 seconds then stops the task"""
print("starting to send a message every 200ms for 2s")
msg=can.Message(arbitration_id=0x123,data=[1,2,3,4,5,6],extended_id=False)
task=bus.send_periodic(msg,0.20)
assert isinstance(task,can.CyclicSendTaskABC)
time.sleep(200)
task.stop()
print("Stopped cyclic send")
return task
simple_periodic_send(bus)
This is the cycle time:"""sends a message every 20ms with no explicit timeout
sleeps for 2 seconds then stops the task"""
Code: Select all
task=bus.send_periodic(msg,0.20
Code: Select all
time.sleep(1)
i do not see how you end up with 65ms (Should be ~20ms) cycle time, but then again i do not know your setup exactly.
There is a "task.stop()" which stops the sending task, the sending task however is returned by the "simple_periodic_send"-function,
which means you can assign the return value of that function to a variable, which you then can use to call the "Stop"-method.
This means you could use another function, a condition to call "Stop()" on the variable, which you assigned the returned task to.
And - even though i have already mentioned it - you are using python-can, we did not develop python-can and therefore
cannot provide proper support for it. We know how our PCANBasic API works, python-can uses PCANBasic as an interface, that does not mean that we know how python-can handles things.
Best Regards
Marvin
---
Marvin Heidemann
PEAK-Support Team
Marvin Heidemann
PEAK-Support Team