Open mattie47 opened 5 years ago
So I had another go:
#!/usr/bin/env python3
import threading
from multiprocessing import Queue
from pyvit.hw import socketcan
from pyvit.dispatch import Dispatcher
from pyvit.proto.isotp import IsotpInterface
dev = socketcan.SocketCanDev("vcan0")
disp = Dispatcher(dev)
recv_queue = Queue()
disp.add_receiver(recv_queue)
# set up an isotp interface Sender/Receiver arb id
sender = IsotpInterface(disp, 0x7DF, 0x7E8)
receiver = IsotpInterface(disp, 0x7E8, 0x7DF)
disp.start()
def vin():
payload = [0x49, 0x02, 0x01, 0x31, 0x41, 0x31, 0x4A, 0x43, 0x35, 0x34, 0x34, 0x34, 0x52, 0x37, 0x32, 0x35, 0x32, 0x33, 0x36, 0x37]
tx_thread = threading.Thread(target=sender.send, args=(payload,))
tx_thread.start()
resp = receiver.recv()
print(resp)
# resp
# tx_thread.join()
resp = []
print("finished")
while True:
CanFrameRecv = IsotpInterface.recv(receiver) # This works
print(CanFrameRecv)
if CanFrameRecv == [0x09, 0x02]:
print("It is!")
vin()
So the above "sort of" works, but still a tad buggy.
If I have tx_thread.join()
in the function, and send VIN Req but no Control Frame, then it holds up the while loop from all other received frames until the thread control frame timeout.
The other issue, is that if you send a control frame, followed by the Vin Req, it'll immediately send the follow up frames. I tried adding resp = []
into the function in hope that it would clear the queue, but after playing around, I see this doesn't appear to make a difference.
Anyone able to give me a hand with this?
Thanks,
Matt
Hi,
I was hoping someone here might be able to help with an example of how to use the dispatcher in a loop and if a condition is met, send a relevant response.
Currently I've just been interacting with Pyvit in a simple while loop with
dev.recv
. However I'm not sure how to interact with the dispatcher given things are threaded.Basically, I have a simple program using Pyvit acting as an ECU to send things such as RPM and Speed when requested by a connected device.
As an example:
As you can see, I'm "cheating" with the VIN, and just sending the frames out without actually checking if a frame control message is received. I want to improve this, and actually use the ISO-TP functionality.
My issue is I've struggled to get ISO-TP messages working nicely.
Using the
isotp_test
I was able to get an idea on how to send an ISO-TP frame, provided you send a frame control message.Example:
I'm just not sure how to interact with the Dispatcher send and receive methods/threads within a while loop.
I'm starting to get the impression my main loop should also be a thread but again not sure how to do this.
Sorry if this is an annoying question - I'm still pretty new to python.
Thanks,
Matt