Hello,
I do not use any API in comparison to PCANBasicExample.
The PCANBasicExample won't work for me. I could not install tix, I think I have to build it myself or modify the example.
This is the code which I am using under Windows 10 and Debian both with python 3 (Anaconda):
Code: Select all
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 19 21:45:13 2019
@author: https://github.com/christiansandberg/canopen
Modified for Win10 Python 3.7.0 by Mario
"""
import canopen
import time
# Start with creating a network representing one CAN bus
network = canopen.Network()
# Add some nodes with corresponding Object Dictionaries
node = canopen.RemoteNode(1, '750-348_08.eds') #Wago 750-348
network.add_node(node)
# Connect to the CAN bus
# Arguments are passed to python-can's can.interface.Bus() constructor
# (see https://python-can.readthedocs.io/en/latest/bus.html).
# network.connect()
# network.connect(bustype='socketcan', channel='can0')
# network.connect(bustype='kvaser', channel=0, bitrate=250000)
# network.connect(bustype='ixxat', channel=0, bitrate=250000)
# network.connect(bustype='vector', app_name='CANalyzer', channel=0, bitrate=250000)
# network.connect(bustype='nican', channel='CAN0', bitrate=250000)
# following line for Windows 10
network.connect(bustype='pcan', channel='PCAN_USBBUS1', bitrate=125000)
# following line for Debian
#network.connect(bustype='pcan', channel='/dev/pcanusb32', bitrate=125000)
# Reset network
node.nmt.state = 'RESET COMMUNICATION'
time.sleep(5)
# Read a variable using SDO
device_name = node.sdo['manufacturer device name'].raw
vendor_id = node.sdo[0x1018][1].raw
print("Device name: ", device_name)
print("Vendor ID: ", vendor_id)
# Change state to operational (NMT start)
node.nmt.state = 'OPERATIONAL'
time.sleep(5)
# Change state to stopped (NMT stop)
node.nmt.state = 'PRE-OPERATIONAL'
# Disconnect from CAN bus
network.disconnect()
The code works under Windows 10.
The output (IPhyton console from Spyder 3.3.1) is:
Code: Select all
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 6.5.0 -- An enhanced Interactive Python.
runfile('D:/Daten/Zwischenspeicher/00_Projekte/01_Prüfstand/03_Python/CANtest-Win10.py', wdir='D:/Daten/Zwischenspeicher/00_Projekte/01_Prüfstand/03_Python')
Device name: 750-348
Vendor ID: 33
On Debian Linux (again Anaconda) I get:
Code: Select all
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
runfile('/home/bnutzer/python/CANtest-Debian.py', wdir='/home/bnutzer/python')
Traceback (most recent call last):
File "<ipython-input-1-bc252302081c>", line 1, in <module>
runfile('/home/bnutzer/python/CANtest-Debian.py', wdir='/home/bnutzer/python')
File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "/usr/lib/python3/dist-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/bnutzer/python/CANtest-Debian.py", line 34, in <module>
network.connect(bustype='pcan', channel='/dev/pcanusb32', bitrate=125000)
File "/usr/local/lib/python3.5/dist-packages/canopen/network.py", line 109, in connect
self.bus = can.interface.Bus(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/can/interface.py", line 128, in __new__
return cls(channel, *args, **config)
File "/usr/local/lib/python3.5/dist-packages/can/interfaces/pcan/pcan.py", line 97, in __init__
self.m_objPCANBasic = PCANBasic()
File "/usr/local/lib/python3.5/dist-packages/can/interfaces/pcan/basic.py", line 334, in __init__
self.__m_dllBasic = cdll.LoadLibrary("libpcanbasic.so")
File "/usr/lib/python3.5/ctypes/__init__.py", line 425, in LoadLibrary
return self._dlltype(name)
File "/usr/lib/python3.5/ctypes/__init__.py", line 347, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libpcanbasic.so: cannot open shared object file: No such file or directory
Now I did the steps described in the manual for checking the installation of the driver again:
Code: Select all
bnutzer@i7:~$ sudo dmesg | grep pcan
[ 4.922756] pcan: Release_20181116_n (le)
[ 4.922758] pcan: driver config [mod] [par] [usb]
[ 5.043621] pcan: PCAN-USB channel device number=255
[ 5.043623] pcan: usb device minor 32 found
[ 5.043677] usbcore: registered new interface driver pcan
[ 5.043682] pcan: major 245.
The devices:
Code: Select all
ls -l /dev/pcan* | grep "^c"
crw-rw-rw- 1 root root 245, 32 Feb 6 20:45 /dev/pcanusb32
/dev/pcan*:
Code: Select all
ls -l /dev/pcan*
lrwxrwxrwx 1 root root 9 Feb 6 20:45 /dev/pcan32 -> pcanusb32
crw-rw-rw- 1 root root 245, 32 Feb 6 20:45 /dev/pcanusb32
/dev/pcan-usb:
insgesamt 0
drwxr-xr-x 2 root root 60 Feb 6 20:45 0
tree /dev/pcan-usb:
Code: Select all
bnutzer@i7:~$ tree /dev/pcan-usb
/dev/pcan-usb
└── 0
└── can0 -> ../../pcanusb32
1 directory, 1 file
Code: Select all
bnutzer@i7:~$ cat /proc/pcan
*------------- PEAK-System CAN interfaces (www.peak-system.com) -------------
*------------- Release_20181116_n (8.7.0) Feb 8 2019 21:21:45 --------------
*------------- [mod] [isa] [pci] [pec] [dng] [par] [usb] [pcc] --------------
*--------------------- 1 interfaces @ major 244 found -----------------------
*n -type- -ndev- --base-- irq --btr- --read-- --write- --irqs-- -errors- status
32 usb -NA- ffffffff 000 0x001c 00000000 00000000 00000000 00000000 0x0000
Code: Select all
bnutzer@i7:~$ cat /sys/class/pcan/version
8.7.0
bnutzer@i7:~$ tree -a /sys/class/pcan
/sys/class/pcan
├── pcanusb32 -> ../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/pcan/pcanusb32
└── version
1 directory, 1 file
Code: Select all
bnutzer@i7:~$ ls -l /sys/class/pcan/pcanusb32/
insgesamt 0
-r--r--r-- 1 root root 4096 Feb 10 16:31 adapter_name
-r--r--r-- 1 root root 4096 Feb 10 16:16 adapter_number
-r--r--r-- 1 root root 4096 Feb 10 16:31 adapter_version
-r--r--r-- 1 root root 4096 Feb 10 16:31 base
-r--r--r-- 1 root root 4096 Feb 10 16:31 btr0btr1
-r--r--r-- 1 root root 4096 Feb 10 16:31 bus_state
-r--r--r-- 1 root root 4096 Feb 10 16:31 clk_drift
-r--r--r-- 1 root root 4096 Feb 10 16:31 clock
-r--r--r-- 1 root root 4096 Feb 10 16:16 ctrlr_number
-r--r--r-- 1 root root 4096 Feb 10 16:31 dev
lrwxrwxrwx 1 root root 0 Feb 10 16:31 device -> ../../../2-1.2:1.0
-rw-r--r-- 1 root root 4096 Feb 10 16:16 devid
-r--r--r-- 1 root root 4096 Feb 10 16:31 errors
-r--r--r-- 1 root root 4096 Feb 10 16:16 hwtype
-r--r--r-- 1 root root 4096 Feb 10 16:31 init_flags
-r--r--r-- 1 root root 4096 Feb 10 16:31 irq
-r--r--r-- 1 root root 4096 Feb 10 16:31 irqs
-r--r--r-- 1 root root 4096 Feb 10 16:31 minor
-r--r--r-- 1 root root 4096 Feb 10 16:31 nom_bitrate
-r--r--r-- 1 root root 4096 Feb 10 16:31 nom_brp
-r--r--r-- 1 root root 4096 Feb 10 16:31 nom_sample_point
-r--r--r-- 1 root root 4096 Feb 10 16:31 nom_sjw
-r--r--r-- 1 root root 4096 Feb 10 16:31 nom_tq
-r--r--r-- 1 root root 4096 Feb 10 16:31 nom_tseg1
-r--r--r-- 1 root root 4096 Feb 10 16:31 nom_tseg2
drwxr-xr-x 2 root root 0 Feb 10 16:31 power
-r--r--r-- 1 root root 4096 Feb 10 16:31 read
-r--r--r-- 1 root root 4096 Feb 10 16:31 rx_error_counter
-r--r--r-- 1 root root 4096 Feb 10 16:31 rx_fifo_ratio
-r--r--r-- 1 root root 4096 Feb 10 16:31 status
lrwxrwxrwx 1 root root 0 Feb 10 16:31 subsystem -> ../../../../../../../../../class/pcan
-r--r--r-- 1 root root 4096 Feb 10 16:31 ts_fixed
-r--r--r-- 1 root root 4096 Feb 10 16:31 tx_error_counter
-r--r--r-- 1 root root 4096 Feb 10 16:31 tx_fifo_ratio
-r--r--r-- 1 root root 4096 Feb 10 16:31 type
-rw-r--r-- 1 root root 4096 Feb 10 16:16 uevent
-r--r--r-- 1 root root 4096 Feb 10 16:31 write
For the next step I prepared the script test.sh containing the commands, because they did not work direcly typed into the terminal.
Code: Select all
#!/bin/sh
# PCAN-Driver-Linux_UserMan_eng.pdf
# Seite 21
for f in /sys/class/pcan/pcanusb32/*
do
[ -f $f ] && echo -n "`basename $f` =" && cat $f
done
The result:
Code: Select all
bnutzer@i7:~$ ./test.sh
adapter_name =PCAN-USB
adapter_number =0
adapter_version =2.8
base =0x0
btr0btr1 =0x001c
bus_state =0
clk_drift =0
clock =8000000
ctrlr_number =0
dev =244:32
devid =255
errors =0
hwtype =11
init_flags =0x00000000
irq =0
irqs =0
minor =32
nom_bitrate =500000
nom_brp =1
nom_sample_point =8750
nom_sjw =1
nom_tq =125
nom_tseg1 =13
nom_tseg2 =2
read =0
rx_error_counter =0
rx_fifo_ratio =0.00
status =0x0000
ts_fixed =0
tx_error_counter =0
tx_fifo_ratio =0.00
type =usb
uevent =MAJOR=244
MINOR=32
DEVNAME=pcanusb32
write =0
Code: Select all
bnutzer@i7:~/peak-linux-driver-8.7.0/driver$ ./lspcan -T -t -i
dev name port irq clock btrs bus
[PCAN-USB 0]
|_ pcanusb32 CAN1 - 8MHz 500k CLOSED
And now the receivetest.
The receivetest is startet and the Wago 750-348 field bus coupler is powered up.
Code: Select all
bnutzer@i7:~/peak-linux-driver-8.7.0/driver$ receivetest -f=/dev/pcanusb32 -b=0x031c -e
receivetest Version "Release_20150611_n" (www.peak-system.com)
------- Copyright (C) 2004-2009 PEAK System-Technik GmbH ------
receivetest comes with ABSOLUTELY NO WARRANTY. This is free
software and you are welcome to redistribute it under certain
conditions. For details see attached COPYING file.
receivetest: device node="/dev/pcanusb32"
Extended frames are accepted, init with BTR0BTR1=0x031c
receivetest: driver version = Release_20181116_n
3625672273.844 receivetest: x - 0x00000001 4 00 00 00 00
receivetest: pending CAN status 0x0020 read.
3625680369.895 receivetest: m s 0x00000701 1 00
3625680371.4 receivetest: m s 0x00000081 8 00 50 81 00 01 00 00 00
^Creceivetest: type = usb
Serial Number = 0xffffffff
Device Number = 0
count of reads = 3
count of writes = 0
count of errors = 1
count of irqs = 33
last CAN status = 0x0000
last error = 0
open paths = 1
driver version = Release_20181116_n
receivetest: finished (0): 3 message(s) received
The receivetest is working fine. I get the same result with PCAN-View under Windows.
Thank you up to here.
Best wishes,
Mario