Send and receive on the same bus (different scripts)

Issue #89 new
Thiago Andrade created an issue

Hi, I'm sending frames from a file to a bus on one script, on the other one I'm receiving these packets. The problem is when I run both, the data don't show up on the receiving code. The idea is to simulate two nodes, one to send and the other to receive, and for that I split the code into two so I can open two terminals and run them simultaneously.

The sending part will send in burst for each line of the file and then wait a second:

from __future__ import print_function
import csv
import time
import can

can.rc['interface'] = 'virtual'
can.rc['channel'] = 'vcan0'

from can.interfaces.interface import Bus
bus = Bus()

def send_from_file():
    with open('frame.txt', 'rb') as f:
        reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE)
        for row in reader:
            for i in range(7):
               id, tam, dado = row[i].split()
               #provavelmente data ta errado
               msg = can.Message(arbitration_id=id, data=bytearray(dado))
               if bus.send(msg) < 0:
                   print("Message sent on {}".format(bus.channel_info))
               else:
                   print("Message NOT sent")
               recvMsg = bus.recv(timeout=None)
               if(recvMsg is not None):
                   print (recvMsg.arbitration_id, recvMsg.data)

               if i == 6:
                   time.sleep(1)


if __name__ == "__main__":
    send_from_file()

The receiving part:

import can
can.rc['interface'] = 'virtual'
can.rc['channel'] = 'vcan0'

from can.interfaces.interface import Bus
bus = Bus()

recvMsg = bus.recv(timeout=None)
if(recvMsg is not None):
    print ("Ok")
else:
    print ("None")

Could anybody point out what I'm doing wrong? If I join both codes, it shows OK but not when I separate them. The receive will run and print nothing.

Comments (7)

  1. Travis Travelstead

    I have not tested this or use vcan, so this is first glance feedback.

    The message is removed from the bus when you receive it. The first script seems to send, then receive right away removing any message that was put on the bus.

    I have always used actual hardware so it is possible that multiple scripts cannot connect to the same vcan socket. Also the vcan socket I believe just echos any messages put on the bus. You will need to send as many messages as you want to receive.

    If you want to try both the main script and the receive only script then you might need to send more than one message out.

  2. Thiago Andrade reporter

    I see. I've tried removing the recv part from the sender and no success. Also tried spamming frames on the bus and still nothing. Guess I'll have to join both into one and print from there.

  3. Thiago Andrade reporter

    Another question, should my sent frames show up when I use command candump vcan0? I've tested now and nothing is received.

  4. Travis Travelstead

    You might need to read more about vcan which is part of SocketCAN. SocketCAN is part of the linux kernel and not part of this library.

    I personally moved past vcan as soon as I could as my concern was making sure the hardware worked. This python-can is very user friendly so getting code to work and then abstracting it was the easy part.

  5. Brian Thorne repo owner

    Hi Thiago,

    The "virtual" interface is different from the socketcan vcan interface - I haven't used it outside of testing this library. It is a pure python virtual bus that will only work from the same python process - it doesn't use system level inter-process communication.

    I think you want to be using socketcan as the interface, and vcan0 as the channel. Then as you expected:

    candump vcan0
    

    Will show the frames you have sent. Note you will also have to setup the virtual socketcan bus yourself - some notes are in the docs here

  6. Log in to comment