Template: RS485 CAN HAT B Rasp

From Waveshare Wiki
Jump to: navigation, search

Working With Raspberry Pi

Install Libraries

BCM2835

#Open the terminal of the Raspberry Pi and run the following command
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.71.tar.gz
tar zxvf bcm2835-1.71.tar.gz 
cd bcm2835-1.71/
sudo ./configure && sudo make && sudo make check && sudo make install
# For more information, please refer to the official website: http://www.airspayce.com/mikem/bcm2835/

WiringPi

#Open the terminal of the Raspberry Pi and run the following command
cd
sudo apt-get install wiringpi
#For Raspberry Pi systems after May 2019 (not done before the previous one) may require an upgrade:
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio -v
# Running gpio -v will appear version 2.52, if there is no description installation error

#The Bullseye branch system uses the following command:
git clone https://github.com/WiringPi/WiringPi
cd WiringPi
./build
gpio -v
# Running gpio -v will appear version 2.60, if there is no description installation error

python

sudo apt-get update
sudo apt-get install python-serial
sudo pip install python-can

Install python3 library:

sudo apt-get update
sudo apt-get install python3-pip
sudo pip3 install RPi.GPIO
sudo apt-get install python3-serial

Driver Configuration

Please insert the module into the Raspberry Pi and change the start-up script "config.txt".

sudo nano /boot/config.txt

Add the following content to the last command:

dtparam=spi=on
dtoverlay=mcp2515-can0,oscillator=16000000,interrupt=25,,spimaxfrequency=1000000
dtoverlay=sc16is752-spi1,int_pin=24

Save and exit, and reboot the Raspberry Pi:

sudo reboot

Check Whether The Configuration Is Correct

After rebooting, the drivers of SC16IS752 and mcp251x will be loaded into the system kernel, run the command to check whether the initialization is successful:

dmesg | grep -i '\(can\|spi\)'

RS485 CAN HAT cry014.png
If you do not connect to the module, you may be prompted as follows:
RS485 CAN HAT cry015.png
Please check if the module is connected, whether the SPI, and MCP2515 kernel is enabled and whether it is restarted.

RS485

Also, you can run the following commands:

ls /dev

Check whether the RS485 bus is correctly configured. If the configuration is correct, the following devices will be added:
2-CH-RS485-HAT-3.png
In the 2020-05-27 Raspberry Pi system gpiochip3 has become gpiochip2 without it.

CAN

Open CAN

sudo ip link set can0 up type can bitrate 1000000
sudo ifconfig can0 txqueuelen 65536
  • For more CAN kernel commands, please refer to:
https://www.kernel.org/doc/Documentation/networking/can.txt
  • Check ifconfig:
ifconfig

RS485 CAN HAT cry16.png

How To Use CAN

Hardware Connection

This demo uses a Raspberry Pi, an RS485 CAN HAT (B) module and a USB-CAN-A module.
Python and c language programs are provided.

RS485 CAN HAT B Rasp001.jpg

Simple Test

  • Open CAN
sudo ip link set can0 up type can bitrate 1000000
sudo ifconfig can0 txqueuelen 65536
  • For more CAN kernel commands, please refer to:
https://www.kernel.org/doc/Documentation/networking/can.txt
  • Check ifconfig:
ifconfig

RS485 CAN HAT cry16.png

  • Install can-utils:
sudo apt-get install can-utils
  • Receive:

The command for inputting the receiving data in the terminal:

candump can0

The receiving tool is blocked. When running the tool without parameters, it will always be in the receiving state. Use Ctrl+C to exit.
A description of the parameters of the tool can be used:

candump -h
  • Sending

Input the command for sending the data in the terminal:

cansend can0 000#11.22.33.44

The result is as shown below:
RS485 CAN HAT B Rasp0112.png

  • Self-test

Input the command in the terminal:

cangen can0 -I i -L 8
#-I sets the device ID, 'i' is the incremental mode; -L sets the data length

At this time, data with the increasing device ID, length 8, and random content will be continuously sent to CAN. At the same time, this command enables loopback mode by default and can be run in another terminal window:

candump can0

After running, you can also receive the data sent above:
RS485 CAN HAT B Rasp0114.png
After the test is over, you can use Ctrl+C in the two terminals to end the operation.

Download The Demo

Run in the terminal of the Raspberry Pi:

sudo apt-get install unzip
wget https://www.waveshare.com/w/upload/9/92/RS485_CAN_HAT_B.zip
unzip RS485_CAN_HAT_B.zip
sudo chmod 777 -R RS485_CAN_HAT_B/
cd RS485_CAN_HAT_B

C

Note:

  1. Please make sure the hardware connection is correct, that is, H-H and L-L connection.
  2. The baud rate of both sides must be the same and the default demo setting is 1Mbps.
  3. If the data loses frame due to the long time transmission, you can try to lower the baud rate.
  • Send, the Raspberry Pi open the terminal, and run:
cd RS485_CAN_HAT_Code/CAN/wiringPi/send/
make clean
make 
sudo ./can_send
  • Blocking receiving, the Raspberry Pi opens the terminal and runs:
cd RS485_CAN_HAT_Code/CAN/wiringPi/receive/
make clean
make
sudo ./can_receive

The receiving program is blocked until the data is read.
Note: This program can only receive data whose frame ID is 123. If you need to receive other ID data, you can modify the program yourself. RS485 CAN HAT B Rasp10.png
Take USB to CAN debug tool as an example:
RS485 CAN HAT B Rasp013.png

Python

The Raspberry Pi opens the terminal and runs:

#the sending terminal:
sudo python can_send.py
cd RS485_CAN_HAT_B/CAN/python/
#Run the receiving program before sending the data from your computer
sudo python can_reveive.py

The effect is shown below:
RS485 CAN HAT B Rasp1014.png