Why is the frequency of this Python-CAN script is shifted?

130 views Asked by At

In the script below, I am sending the data [0x01, 0x0a] to the address 0x0050000b and then search for an answer "01 0a" at the address 0x0790000b. The mesage needs to be sent every 100ms.

The problem is that for some reason the message I am sending is shifted ~1.3ms which cause my CAN device not able to receive it.

I added a screenshot of CAN messages below,

Any help is appreciated!

import can
import time
enable_use_server = True

bus = can.interface.Bus(bustype='neovi', channel='1', bitrate=500000)

# Set Control Mode = Buck and Control Type = V_LOW_I_LIM (new mode)
#send(can_id=0x00500000, ext_id=1, dlc=2, one_shot=1, rtr=0, data=[0x01, 0x0A], wait=100)

msg = can.Message(arbitration_id=0x0050000b, data=[0x01, 0x0a], extended_id=True)
bus.send(msg)
time.sleep(.1)
print('Enter Vlow Ilim Mode sent')

#Verifv
SearchID = str(hex(0x0790000b))
SearchID = SearchID.strip('0x')
found = False

while not found:
    mg = str(bus.recv(0.05))
    if re.search(SearchID, mg):
        print('rcv:  ' + mg)
        if re.search("01 0a", mg):
            print(msg)
            print("Vlow Ilim Mode was Successful")
            found = True
        else:
            print("Vlow Ilim Mode was NOT Successful")
            msg = can.Message(arbitration_id=0x0050000b, data=[0x01, 0x0a], extended_id=True)
            bus.send(msg)
            time.sleep(0.1)

here is the CAN log screenshot

1

There are 1 answers

0
Michael Butscher On

The code itself needs some time to execute, too. Better write the sleep in the loop like:

t = time.time_ns()
while True:
    ...
    time.sleep((100000 - (time.time_ns() - t)) / 1000000)
    t = time.time_ns()

Maybe some adjustments are necessary yet.