Hello,
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)
"""sends a message every 20ms with no explicit timeout
sleeps for 2 seconds then stops the task"""
This is the cycle time:
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