Pi RS485&CAN Module User Manual - V1.3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Design Service,Production Service

www.inno-maker.com

Raspberry Pi RS485&CAN Module User


Manual

Support: [email protected] wiki.inno-maker.com 1


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

1.General Description:

RS485&CAN Module is an industrial communication module for Raspberry Pi, on board 2*RS485
Bus and 1*CAN Bus communication interface via SPI interface.

CAN bus and RS485 bus powered through separated isolation power module, signal between the
transceiver and the controller is isolated , ESD protection for the communication port, ensure
your Raspberry Pi can be used in more strictly industrial sites

2.Features:
2.1 Compatible with Raspberry Pi Zero/Zero W/2B/3B/3B+/4. Only a small amount of GPIO are
used and the remaining pins allow to work for other extended function

2.2 Power supply and signal transmission isolation, Build-in surge and ESD protection.

2.3 CAN function: on-board CAN controller MCP2515 via SPI interface, high speed CAN
transceiver, digital isolation ADUM1201BRZ, and communication Rates 20Kbps-1Mpbs can be
programmed arbitrarily.

2.4 RS485 function: on-board controlled via UART, half-duplex communication, supports
automatically TX/RX switch without extra programming, on-board SPI to RS485 SC16IS1752
Electrical data isolation with ADI ADM2483.

2.5 On-board individual 120 Ohm terminal resistance, Impedance matching and guarantee the
ability to drive. On-board User ATC24C32 EEPROM,

Support: [email protected] wiki.inno-maker.com 2


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

3.Hardware Description
3.1 Overview

Support: [email protected] wiki.inno-maker.com 3


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

3.2 Output Innterface Desceiption

Item Function Description


1 RS485_A1 RS485-1 A Signal
2 GND_485 RS485-1 GND
3 RS485_B1 RS485-1 B Signal
4 RS485_A2 RS485-2 A Signal
5 GND_ISO RS485-2 GND
6 RS485_B2 RS485-2 B Signal
7 CANH CAN-H Signal
8 CAN_GND_ISO CAN Isolated GND
9 CANL CAN-L Signal

Support: [email protected] wiki.inno-maker.com 4


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

3.3 Pins map of GPIO header on the Raspberry Pi

PIN Symbol Description


+5V Supply Pin,connected to the main 5V supply of the
2,4 +5V
Raspberry Pi
IIC_SDA and
3,5 IIC Used for EEPROM( U4 AT24C32)
IIC_SCL
19 GPIO_10 SPI0_MOSI
21 GPIO_9 SPI0_MISO
23 GPIO_11 SPI0 _SCLK
RS485 Transfer Chip
24 GPIO_8 Selection

26 GPIO_7 CAN Transfer Chip

Support: [email protected] wiki.inno-maker.com 5


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

Selection
18 GPIO_24 RS485 Interrupt
22 GPIO_25 CAN Interrupt
Reserved for an ID EEPROM on the Raspberry Pi.These pins
ID SCL and
27,28 are always reserved and should never be used to connect
ID SDA
external components
6,9,14,20,25,30,34,3 Ground Pin, connected to the main system Ground of the
GND
9 Raspberry Pi
The remaining pins are unused, You can use them for your other hardware boards.

3.4 Extended Function

(1) ID EEPROM: (U5, No soldering on-board )

Pin 27 and 28 are always reserved for an ID EEPROM on the Raspberry Pi. Independently
which card you use. It’s useless for most application. If you want to use this function, you
need to solder the IC, resistance and capacitance by yourself.

Support: [email protected] wiki.inno-maker.com 6


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

(2) USER EEPROM: (U4 )

(3) On-board 120 Ohm Termination Resistor

Support: [email protected] wiki.inno-maker.com 7


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

For more information about GPIO of Raspberry PI, please refer to below link:

https://www.raspberrypi-spy.co.uk/2012/06/simple-guide-to-the-rpi-gpio-header-and-pins/#pret
tyPhoto

Support: [email protected] wiki.inno-maker.com 8


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

4. Run 485&CAN Test Demo


There are two ways to run 485&CAN test demo. One is download our prepared image from our
wiki page:
wiki.inno-maker.com/display/RS485CANMODULE
or https://www.jianguoyun.com/p/DTxr8-EQpdSrBxi0irsB

The other way is update the drive file to your image.Please refer to the 4.6 Chapter.

4.1 Preparatory Work.


(1) Load the image file onto a SD card, using the instructions provided on the Raspberry Pi
website for Linux, Mac or PC:
https://www.raspberrypi.org/documentation/installation/installing-images/README.md

(2) Prepare two RS485&CAN modules, two TF cards and two Raspberry Pi boards. Connect the
two connectors in proper order. Connect the UART debug port of Raspberry Pi to your computer
(or using remote login).

Support: [email protected] wiki.inno-maker.com 9


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

(3) You also can connect two RS485 port directly of one module and connect the CAN port to
our USB2CAN module(http://www.inno-maker.com/product/usb-can/) for testing.

4.2 Run C test Demo for RS485


(1) Visit the folder named ‘485CANC’ on the desktop.

Support: [email protected] wiki.inno-maker.com 10


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

(2) Execute following commands on both Raspberry Pi, And you will see the data interaction.
./rs485_test -d/dev/ttySC0 -s abcdefghigklmnopqrstuvwxyz -e &

4.3 Run C test Demo for CAN

(1) Visit the folder named ‘485CANC’ on the desktop.

(2)Set one Raspberry Pi as receiver. Now this Raspberry pi is blocked.

Support: [email protected] wiki.inno-maker.com 11


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

(3) Set the other one as senderand You should see that the receiver has received the packet.

./can0_send

(4) Check whether receiver received the packet.

4.4 Run Python test Demo for RS485


(1) Visit the folder named ‘485PYTHON’ on the desktop.

(2) Set one Raspberry Pi as receiver


sudo python recieive.py

Support: [email protected] wiki.inno-maker.com 12


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

(3) Set the other Raspberry Pi as sender.


sudo python send.py

(4) View received result.

4.5 Run Python test Demo for CAN

(1) Visit the folder named ‘CANPYTHONC’ on the desktop.

(2) Set one Raspberry Pi as receiver.


sudo python receive.py

Support: [email protected] wiki.inno-maker.com 13


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

(3) Set the other Raspberry Pi as sender.


sudo python send.py

(5) View received result.

(6) If you’re use our USB2CAN module for testing, please change the CAN device ‘can0’ to ‘can1’
in either ‘send.py’ or ‘receive.py’ file.

4.6 How to use 485&CAN module in a new Raspbian System

(1) Insert a TF cart into your computer, load your image of Raspbian to it
(2) Download the file named 'rs485_at.dtbo' from our wiki and copy it to /boot/overlays of the TF
card.

(3) Open ‘config.txt’ and add below two lines in the end.
dtoverlay=rs485_at
dtoverlay=mcp2515-can1,oscillator=16000000,interrupt=25

Support: [email protected] wiki.inno-maker.com 14


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

(4) Insert the TF card into the Raspberry Pi and power on.
(5) Download test codes and copy the folder to the Raspberry Pi by USB flash Dsik or remote
login.
(6) Go into the folders and change the permission. Otherwise you can't run the Demo.
chmod -R a+x *

(7) Check the kernel log to see if RS485 was initialized successfully.
ls -l /dev/ttyS*

(8) Check the kernel log to see if CAN was initialized successfully.
Support: [email protected] wiki.inno-maker.com 15
Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

------------------If you don’t want to use Python, you needn’t do below these steps.----------------------

(9)Check the Python version of your Raspbian. Python 3.7.3 default in 2019-07-10-Raspbian.img.
Our Demo can run on any Python3 version.
python3 -V

(10)If you can’t find the Python3 in system. Install the Python3
sudo apt-get install python-pip
sudo apt-get install python3 idle3 nano

Support: [email protected] wiki.inno-maker.com 16


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

(11)Install Python CAN library.


sudo pip install python-can

Support: [email protected] wiki.inno-maker.com 17


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

5.Software Description

5.1 Linux C Programing


Now with previous demo’s code to show you how to program socket can in linux. The socket
can is an implementation of CAN protocols(Controller Area Network) for Linux. CAN is a
networking technology which has widespread use in automation, embedded devices, and
automotive fields. While there have been other CAN implementations for Linux based on
character devices, Socket CAN uses the Berkeley socket API, the Linux network stack and
implements the CAN device drivers as network interfaces. The CAN socket API has been designed
as similar as possible to the TCP/IP protocols to allow programmers, familiar with network
programming, to easily learn how to use CAN sockets.

5.1.1 C For CAN

For more detail about Socket CAN please refer to below link:
https://www.kernel.org/doc/Documentation/networking/can.txt

(1) For Sender’s code:


a: Create the socket, If an error occurs then the return result is -1.

b: Locate the interface to “can0” or other name you wish to use. The name will show when you
execute ‘./ifconfig –a’.

c: Bind the socket to ‘can0’.

Support: [email protected] wiki.inno-maker.com 18


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

d: Disable sender’s filtering rules,this program only send message do not receive packets.

e: Assembly data to send.

f: Send message to the can bus.You can use the return value of write() to check whether all data
has been sent successfully .

g: Close can0 device and disable socket.

Support: [email protected] wiki.inno-maker.com 19


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

(2) For Receiver’code:


Step a and step b is same as Sender’s code.

c: It’s different from Sender’s

d:Define receive filter rules, we can set more than one filters rule.

e: Read data back from can bus.

5.2.1 C For RS485

a: Open RS485 Device and set baud rate.

Support: [email protected] wiki.inno-maker.com 20


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

b: Set RS485 parity mode and flow control.

c: Set RS485 work mode.

d: Send data through RS485 bus.

e: Get data from RS485 bus.

5.2 Linux Python Programing

5.2.1 Python For CAN


a: Import
import os
The OS module in Python provides a way of using operating system dependent functionality. The
functions that the OS module provides allows you to interface with the underlying operating
system that Python is running on – be that Windows, Mac or Linux. We usually use os.system()
function to execute a shell command to set CAN.

import can
The python-can library provides Controller Area Network support for Python, providing common
abstractions to different hardware devices, and a suite of utilities for sending and receiving
messages on a CAN bus.
For more information about python-can, please to below link:
https://python-can.readthedocs.io/en/stable/index.html
Support: [email protected] wiki.inno-maker.com 21
Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

b: simple common functions

(1) Set bitrate and start up CAN device.


os.system('sudo ip link set can0 type can bitrate 1000000')
os.system('sudo ifconfig can0 up')

(2) Bind the socket to ‘can0’.


can0 = can.interface.Bus(channel = 'can0', bustype = 'socketcan_ctypes')

(3) Assembly data to send.


msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3], extended_id=False)

(4) Send data.


can0.send(msg)

(5) Receive data.


msg = can0.recv(30.0)

(6) Close CAN device


os.system('sudo ifconfig can0 down')

Support: [email protected] wiki.inno-maker.com 22


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

5.2.1 Python For RS485

a:Enable RS485

# RS485 ioctls
TIOCGRS485 = 0x542E
TIOCSRS485 = 0x542F
SER_RS485_ENABLED = 0b00000001
SER_RS485_RTS_ON_SEND = 0b00000010
SER_RS485_RTS_AFTER_SEND = 0b00000100
SER_RS485_RX_DURING_TX = 0b00010000

buf = array.array('i', [0] * 8) # flags, delaytx, delayrx, padding


ser = serial.Serial("/dev/ttySC0",115200)
fcntl.ioctl(ser, TIOCGRS485, buf)

buf[0] |= SER_RS485_ENABLED|SER_RS485_RTS_AFTER_SEND
buf[1] = 0
buf[2] = 0
fcntl.ioctl(ser, TIOCSRS485, buf)

b: Send data packet

command = ['a','b','c','d']
n = ser.write(command)

c: Receive data packet

str = ser.read(ser.inWaiting())

Support: [email protected] wiki.inno-maker.com 23


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

6.Modbus Description
Modbus is a serial communication protocol developed by Modicon published by Modicon® in
1979 for use with its programmable logic controllers (PLCs). In simple terms, it is a method used
for transmitting information over serial lines between electronic devices. The device requesting
the information is called the Modbus Master and the devices supplying information are Modbus
Slaves. In a standard Modbus network, there is one Master and up to 247 Slaves, each with a
unique Slave Address from 1 to 247. The Master can also write information to the Slaves.

The official Modbus specification can be found at http://www.modbus.org/

Now we use two RS485 ports on-board to make a modbus demo with Python3. set one port as
modbus master and the other port as modbus slave in same shield.

6.1 Hardware Connection

Only need to connected the two RS485 ports.

Support: [email protected] wiki.inno-maker.com 24


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

6.2 Run Demo

Download the demo files named ‘modbus’ from our wiki:


wiki.inno-maker.com/display/RS485CANMODULE
or https://www.jianguoyun.com/p/DTxr8-EQpdSrBxi0irsB

There are two files in the folder, ’rtumaster.py’ and ‘rtuslave.py’. I just make a simple demo to
show you how to use ‘READ_HOLDING_REGISTERS’ command to read data with modbus
viaRS485.

Demo is base on the one release by luc.jean (https://github.com/ljean/modbus-tk). May I


suggest that you’d better read his codes and documents carefully, because it can help you
have a better understanding of Modbus with coding in Python.

(1) To install dependencies:


sudo pip3 install modbus-tk

(2) Set slave:


sudo python3 rtuslave.py

Support: [email protected] wiki.inno-maker.com 25


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

(3) Set the values to salve_1 for test.


‘set_values’ command format: slave_id + name + address + values
set_values 1 0 0 0
set_values 1 0 1 1
set_values 1 0 3 3

(4) Open a new terminal to set the master


sudo python3 rtumaster.py

(5) You can see the result read from master. If you not set values follow step 3, The result will be
0,0,0,0 default.

Support: [email protected] wiki.inno-maker.com 26


Bulk Price: [email protected]
Design Service,Production Service
www.inno-maker.com

Version Descriptions

Version Description Author Date E-mail


V1.0.0.0 First edition Calvin 2019.04.30 [email protected]
V1.0.0.1 Added Python, Calvin 2019.10.07 [email protected]
Added chapter 4.6
V1.0.0.2 Added description of 120 Calvin 2019.10.09 [email protected]
ohm
V1.0.0.3 Added description of Calvin 2019.11.26 [email protected]
Modbus

If you have any suggestions, ideas, codes and tools please feel free to email to me. I will update
the user manual and record your name and E-mail in list. Look forward to your letter and kindly
share.

Support: [email protected] wiki.inno-maker.com 27


Bulk Price: [email protected]

You might also like