'How do i make my virtual can-slave/client responds to messages sent by the user?

me and my group are new to the Can protocol in general so if we^ve asked a dumb question i appolgise in advance.

We have tried to make a connection between the can master and slave codes, the one below is the client that we have written. The problem with this is that we do get a connection, but it somehow resets right after. what are we doing wrong?

This is what we get if we run the client now

#!/usr/bin/env python



from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import canopen
import time
import logging
import os
import signal

logging.basicConfig(level=logging.DEBUG)

#Use terminal to add arguments and parse them
def get_options():
    parser = ArgumentParser(description="Canopen instrument simulator",
                            formatter_class=ArgumentDefaultsHelpFormatter)

    parser.add_argument("-e", "--edsfile", required=True,
                        help="The EDS file for the instrument")
    parser.add_argument("-i", "--interface", required=True,
                        help="The name of the canbus interface")
    parser.add_argument("-n", "--nodeid", type=int, required=True,
                        help="The canopen node ID")

    return parser.parse_args()


def initialize_CAN(args):
    config = vars(args)
    print(config)
    network = canopen.Network()
    network.connect(bustype='socketcan', channel=args.interface)
    can_slave = network.create_node(args.nodeid, args.edsfile)
    print(f"Canopen Slave with id {can_slave.id} has started and is connected to {args.interface}")
    # Finished with initialization so set to pre-operational state
    can_slave.nmt.state = 'PRE-OPERATIONAL'
    #--------------------------------------------------------

def main():

    args = get_options()

    # Opens a new terminal that runs candump
    os.system(f"gnome-terminal -e 'bash -c \"candump -tz {args.interface}\"'")  # -l logging sets -s 2 by default, -s silentmode 0 (OFF)
    logging.basicConfig(level=logging.INFO)
    initialize_CAN(args)
    print("-----Terminate by pressing ctrl+c-----")

    try:
        while True:
            time.sleep(50.0 / 1000.0) #Idles the program

    except KeyboardInterrupt:
        os.kill(os.getppid(), signal.SIGHUP) #close application

if __name__ == '__main__':
    main()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source