產(chǎn)品概述
RS485 CAN HAT 是微雪電子為樹(shù)莓派開(kāi)發(fā)的一款的帶 RS485 和 CAN 通信功能的擴(kuò)展板,具備 RS485登刺、CAN 通信功能。
特點(diǎn)
- 基于Raspberry Pi 40pin GPIO接口嗡呼,適用于Raspberry Pi系列主板
- 具備CAN功能纸俭,使用SPI接口CAN控制器MCP2515,搭配收發(fā)器SN65HVD230
- 具備RS485功能南窗,使用UART控制揍很,半雙工通訊,收發(fā)器為SP3485
- 板載TVS(瞬態(tài)電壓抑制管)万伤,RS485通訊可有效抑制電路中的浪涌電壓和瞬態(tài)尖峰電壓窒悔,防雷防靜電
- 預(yù)留控制接口,方便其他控制器控制
- 提供完善的配套資料手冊(cè)(提供wiringPi與python例程)
產(chǎn)品參數(shù)
- 工作電壓: 3.3V
- CAN控制芯片: MCP2515
- CAN收發(fā)器: SN65HVD230
- 485收發(fā)器: SP3485
- 產(chǎn)品尺寸: 65mmx30mm
- 固定孔通經(jīng): 3.0mm
接口說(shuō)明
- CAN總線
| 功能引腳 | 樹(shù)莓派接口(BCM) | 描述 |
| 3V3 | 3V3 | 3.3V電源正 |
| GND | GND | 電源地 |
| SCK | SCK | SPI時(shí)鐘輸入 |
| MOSI | MOSI | SPI數(shù)據(jù)輸入 |
| MISO | MISO | SPI數(shù)據(jù)輸出 |
| CS | CE0 | 數(shù)據(jù)/命令選擇 |
| INT | 25 | 中斷輸出 |
- RS485總線
| 功能引腳 | 樹(shù)莓派接口(BCM) | 描述 |
| 3V3 | 3V3 | 3.3V電源正 |
| GND | GND | 電源地 |
| RXD | RXD | 串口接收 |
| TXD | TXD | 串口發(fā)送 |
| RSE | 4 | 設(shè)置收發(fā) |
對(duì)于RSE引腳敌买,可以選擇不使用简珠,模塊出廠默認(rèn)使用的是硬件自動(dòng)接收與發(fā)送。
硬件說(shuō)明
CAN總線
CAN 模塊的功能是處理所有 CAN 總線上的報(bào)文接收和發(fā)送虹钮。報(bào)文發(fā)送時(shí)北救,首先將報(bào)文裝載到正確的報(bào)文緩沖器和控制寄存器中荐操。通過(guò) SPI 接口設(shè)置控制寄存器中的相應(yīng)位或使用發(fā)送使能引腳均可啟動(dòng)發(fā)送操作。通過(guò)讀取相應(yīng)的寄存器可以檢查通訊狀態(tài)和錯(cuò)誤珍策。 會(huì)對(duì)在 CAN總線上檢測(cè)到的任何報(bào)文進(jìn)行錯(cuò)誤檢查,然后與用戶定義的濾波器進(jìn)行匹配宅倒,以確定是否將報(bào)文移到兩個(gè)接收緩沖器中的一個(gè)攘宙。
由于樹(shù)莓派本身并不支持CAN總線,因此使用SPI接口的CAN控制器拐迁,搭配一個(gè)收發(fā)器完成CAN功能蹭劈。 Microchip 的 MCP2515 是一款CAN協(xié)議控制器,完全支持 CAN V2.0B 技術(shù)規(guī)范线召。該器件能發(fā)送和接收標(biāo)準(zhǔn)和擴(kuò)展數(shù)據(jù)幀以及遠(yuǎn)程幀铺韧。 MCP2515 自帶的兩個(gè)驗(yàn)收屏蔽寄存器和六個(gè)驗(yàn)收濾波寄存器可以過(guò)濾掉不想要的報(bào)文,因此減少了主單片機(jī)(MCU)的開(kāi)銷(xiāo)缓淹。MCU通過(guò)SPI接口與該器件連接,即樹(shù)莓派通過(guò)SPI接口連接芯片哈打,對(duì)于樹(shù)莓派使用該芯片不需要編寫(xiě)驅(qū)動(dòng),只需要打開(kāi)設(shè)備樹(shù)中的內(nèi)核驅(qū)動(dòng)即可使用讯壶。
更多詳細(xì)請(qǐng)參考數(shù)據(jù)手冊(cè)料仗;
SN65HVD230 是德州儀器公司生產(chǎn)的 3.3V CAN 收發(fā)器,該器件適用于較高通信速率伏蚊、良好抗干擾 能力和高可靠性 CAN 總線的串行通信立轧。SN65HVD230 具有高速、斜率和等待 3 種不同的工作模式躏吊。 其工作模式控制可通過(guò) Rs 控制引腳來(lái)實(shí)現(xiàn)氛改。CAN 控制器的輸出引腳 Tx 接到 SN65HVD230 的數(shù)據(jù) 輸入端 D,可將此 CAN 節(jié)點(diǎn)發(fā)送的數(shù)據(jù)傳送到 CAN 網(wǎng)絡(luò)中比伏;而 CAN 控制器的接收引腳 Rx 和 SN65HVD230 的數(shù)據(jù)輸出端 R 相連胜卤,用于接收數(shù)據(jù)。
RS485總線
SP3485接口芯片是一種RS-485驅(qū)動(dòng)芯片凳怨。用于RS-485通信的低功耗收發(fā)器瑰艘。 采用單一電源+3.3V工作,采用半雙工通訊方式肤舞。RO和DI端分別為接收器的輸出和驅(qū)動(dòng)器的輸入端紫新;(RE) ?和DE端分別為接收和發(fā)送的使能端,當(dāng)(RE) ?為邏輯0時(shí)李剖,器件處于接收狀態(tài)芒率;當(dāng)DE為邏輯1時(shí),器件處于發(fā)送狀態(tài)篙顺;A端和B端分別為接收和發(fā)送的差分信號(hào)端偶芍,當(dāng)A-B>+0.2V時(shí)充择,RO輸出邏輯1;當(dāng)A-B<-0.2V時(shí)匪蟀,RO輸出邏輯0椎麦。A和B端之間加匹配電阻,一般可選100Ω的電阻材彪。
其中:SP3485芯片的RE與DE管腳是設(shè)置接收與發(fā)送观挎;
本模塊默認(rèn)的出廠設(shè)置是采用硬件自動(dòng)的收發(fā),也可以選擇軟件上控制管腳來(lái)選擇發(fā)送與接收段化,可以通過(guò)焊接板上的0歐姆電阻來(lái)選擇控制方式嘁捷。
硬件自動(dòng)控制:
數(shù)據(jù)接收:P_TX此時(shí)為高電平, 為休閑狀態(tài)。這時(shí)候三級(jí)管導(dǎo)通显熏,SP3485芯片的RE引腳為低電平雄嚣,數(shù)據(jù)接收使能,RO開(kāi)始接收數(shù)據(jù)喘蟆,將485AB口接受到的數(shù)據(jù)傳到MCU缓升。
數(shù)據(jù)發(fā)送:P_TX會(huì)有一個(gè)下拉的電平,表示開(kāi)始發(fā)送數(shù)據(jù)履肃,此時(shí)三極管截止仔沿,DE引腳為高電平,數(shù)據(jù)發(fā)送使能尺棋。此時(shí)封锉,如果發(fā)送的數(shù)據(jù)為‘1’的時(shí)候,三極管會(huì)處于導(dǎo)通膘螟,雖然接收會(huì)變?yōu)橛行顟B(tài)但由于芯片在發(fā)送階段時(shí)是高阻狀態(tài)成福,所以還是保持發(fā)送狀態(tài),正常傳輸‘1’荆残。
注意:使用自動(dòng)收發(fā)由于三級(jí)管的通斷的速度問(wèn)題奴艾,會(huì)導(dǎo)致自動(dòng)收發(fā)的波特率無(wú)法做到太高,如果需要很高的波特率建議使用收動(dòng)收發(fā)内斯。
樹(shù)莓派使用
安裝庫(kù)
- 安裝BCM2835蕴潦, 打開(kāi)樹(shù)莓派終端,并運(yùn)行一下指令
- tar zxvf bcm2835-1.60.tar.gz
- cd bcm2835-1.60/
- sudo ./configure
- sudo make
- sudo make check
- sudo make install
- 更多的可以參考官網(wǎng):http://www.airspayce.com/mikem/bcm2835/
- 安裝wiringPi
- sudo apt-get install wiringpi
-
對(duì)于樹(shù)莓派4B可能需要進(jìn)行升級(jí):
- wget https://project-downloads.drogon.net/wiringpi-latest.deb
- sudo dpkg -i wiringpi-latest.deb
- gpio -v
- 運(yùn)行g(shù)pio -v會(huì)出現(xiàn)2.52版本俘闯,如果沒(méi)有出現(xiàn)說(shuō)明安裝出錯(cuò)
- python
- sudo apt-get update
- sudo apt-get install python-serial
- sudo pip install python-can
下載例程
在樹(shù)莓派終端運(yùn)行:
- sudo apt-get install p7zip-full
- wget http://www.waveshare.net/w/upload/d/de/RS485_CAN_HAT_Code.7z
- 7z x RS485_CAN_HAT_Code.7z -r -o./RS485_CAN_HAT_Code
- sudo chmod 777 -R RS485_CAN_HAT_Code/
CAN使用
本演示程序使用了兩個(gè)樹(shù)莓派以及兩個(gè)RS485 CAN HAT模塊
提供python與c語(yǔ)言程序
前置工作
將模塊插在樹(shù)莓派上潭苞,然后修改開(kāi)機(jī)腳本config.txt
- sudo nano /boot/config.txt
在最后一行加入如下:
- dtparam=spi=on
- dtoverlay=mcp2515-can0,oscillator=12000000,interrupt=25,spimaxfrequency=2000000
其中oscillator=12000000,是板載的晶振大小12M真朗,如下圖
- 如果購(gòu)買(mǎi)日期早于2019年8月份此疹,請(qǐng)使用下面的:
- dtparam=spi=on
- dtoverlay=mcp2515-can0,oscillator=8000000,interrupt=25,spimaxfrequency=1000000
保存退出后,重啟樹(shù)莓派:
- sudo reboot
重啟后蝗碎,運(yùn)行命令查看是否初始化成功:
- dmesg | grep -i '(can|spi)'
如果不接上模塊可能提示如下:
請(qǐng)檢查是否連接上模塊湖笨。是否開(kāi)啟SPI并開(kāi)啟MCP2515內(nèi)核驅(qū)動(dòng)。是否進(jìn)行重啟蹦骑。
確定好兩邊樹(shù)莓派都這樣處理了慈省,把兩個(gè)模塊的H與L對(duì)應(yīng)連接
如果使用的是其他的CAN設(shè)備,確定連線H-H,L-L即可
C
- 阻塞接收脊串,樹(shù)莓派打開(kāi)終端辫呻,運(yùn)行:
- cd RS485_CAN_HAT_Code/RS485_CAN_HAT_Code/CAN/wiringPi/receive/
- make clean
- make
- sudo ./can_receive
接收程序是阻塞的,直到讀取到數(shù)據(jù)就結(jié)束琼锋。
- 發(fā)送,樹(shù)莓派打開(kāi)終端祟昭,運(yùn)行:
- cd RS485_CAN_HAT_Code/RS485_CAN_HAT_Code/CAN/wiringPi/receive/
- make clean
- make
- sudo ./can_send
此時(shí)接收接收到對(duì)應(yīng)的id的報(bào)文:
python
樹(shù)莓派打開(kāi)終端缕坎,運(yùn)行:
- cd RS485_CAN_HAT_Code/RS485_CAN_HAT_Code/CAN/python/
-
先運(yùn)行接收:
- sudo python can_reveive.py
-
發(fā)送端:
- sudo python can_send.py
RS485使用
本演示程序使用了兩個(gè)樹(shù)莓派以及兩個(gè)RS485 CAN HAT模塊
提供python與wiringPi語(yǔ)言程序
前置工作
開(kāi)啟Uart接口
打開(kāi)樹(shù)莓派終端,輸入以下指令進(jìn)入配置界面
- sudo raspi-config
- 選擇Interfacing Options -> Serial篡悟,關(guān)閉shell訪問(wèn)谜叹,打開(kāi)硬件串口
</pre>
然后重啟樹(shù)莓派:
- sudo reboot
打開(kāi)/boot/config.txt文件,找到如下配置語(yǔ)句使能串口搬葬,如果沒(méi)有荷腊,可添加在文件最后面。
- enable_uart=1
對(duì)于樹(shù)莓派3B用戶急凰,串口用于藍(lán)牙女仰,需要注釋掉:
-
dtoverlay=pi3-miniuart-bt
然后重啟樹(shù)莓派:
- sudo reboot
確定好兩邊樹(shù)莓派都這樣處理了,把兩個(gè)模塊的A與B對(duì)應(yīng)連接
如果使用的是其他的485設(shè)備抡锈,確定連線A-A,B-B即可
C
- 阻塞接收疾忍,樹(shù)莓派打開(kāi)終端,運(yùn)行:
- cd RS485_CAN_HAT_Code/RS485_CAN_HAT_Code/485/WiringPi/send
- make clean
- make
- sudo ./485_receive
接收程序是阻塞的床三,直到讀取到數(shù)據(jù)就結(jié)束一罩。
- 發(fā)送,樹(shù)莓派打開(kāi)終端撇簿,運(yùn)行:
- cd RS485_CAN_HAT_Code/RS485_CAN_HAT_Code/485/WiringPi/send
- make clean
- make
- sudo ./485_send
python例程
- cd RS485_CAN_HAT_Code/RS485_CAN_HAT_Code/485/python/
-
先運(yùn)行接收:
- sudo python reveive.py
-
發(fā)送端:
- sudo python send.py
故障排查
如果485通信不正常聂渊,請(qǐng)分步調(diào)試:
- 確定樹(shù)莓派的硬件版本,如果是樹(shù)莓派ZERO/3B四瘫,則程序中的串口需要修改成/dev/ttyAMA0汉嗽;
- 確定485的A,B是否與控制的485設(shè)備A,B一一對(duì)應(yīng);
- 可以先使用USB to 485設(shè)備與RS485 CAN HAT通信莲组,保證樹(shù)莓派的設(shè)置沒(méi)有問(wèn)題诊胞;
原理圖
485Demo
receive.py
# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import serial
import time
#if use half-auto, EN_485 = LOW is Receiver, EN_485 = HIGH is Send
MODE = 0 #mode = 0 is full-guto, mode = 1 is half-auto
if MODE == 1:
EN_485 = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(EN_485,GPIO.OUT)
GPIO.output(EN_485,GPIO.HIGH)
ser = serial.Serial("/dev/ttyS0",115200,timeout=0.01) #receive data once every 0.01S
print ser.portstr
ser.flushInput()
data = ""
print("You can always receive data, press Ctrl + C to exit")
while 1:
while ser.inWaiting() > 0:
data = ser.readline()
if data != "":
print(data)
data = ""
send.py
# -*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import serial
#if use half-auto, EN_485 = LOW is Receiver, EN_485 = HIGH is Send
MODE = 0 #mode = 0 is full-guto, mode = 1 is half-auto
if MODE == 1:
EN_485 = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(EN_485,GPIO.OUT)
GPIO.output(EN_485,GPIO.HIGH)
ser = serial.Serial("/dev/ttyS0",115200,timeout=1)
print ser.portstr
command = "hello world"
print("send:"),command
len = ser.write(command)
print("len = "),len
print("You can always send data, press Ctrl + C to exit")
while 1:
strInput = raw_input('enter some words:')
ser.write(strInput)
ser.flush()
485_receive.c
#include <wiringSerial.h>
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h> //exit()
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
// if use half-auto, EN_485 = LOW is Receiver, EN_485 = HIGH is Send
#define MODE 0 //mode = 0 is full-guto, mode = 1 is half-auto
#define EN_485 4
#define UART_DEV "/dev/ttyS0"
int fd;
void Handler(int signo)
{
//System Exit
printf("\r\nHandler:serialClose \r\n");
serialClose(fd);
exit(0);
}
int main(void)
{
// Exception handling:ctrl + c
signal(SIGINT, Handler);
if(MODE == 1){
if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table
printf("set wiringPi lib failed !!! \r\n");
return 1;
} else {
printf("set wiringPi lib success !!! \r\n");
}
pinMode(EN_485, OUTPUT);
digitalWrite(EN_485,LOW);
}
if((fd = serialOpen(UART_DEV, 115200)) < 0) {
printf("serial err\n");
return -1;
}
printf("use device %s\r\n", UART_DEV);
printf("You can always receive data, press Ctrl + C to exit\r\n");
char str;
for (;;) {
str = serialGetchar(fd);
if(str < 128) // ascii
printf("%c", str);
fflush(stdout); // Empty the output buffer and output the contents of the buffer
}
return 0;
}
485_send.c
#include <wiringSerial.h>
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h> //exit()
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
// if use half-auto, EN_485 = LOW is Receiver, EN_485 = HIGH is Send
#define MODE 0 //mode = 0 is full-guto, mode = 1 is half-auto
#define EN_485 4
#define UART_DEV "/dev/ttyS0"
int fd;
void Handler(int signo)
{
//System Exit
printf("\r\nHandler:serialClose \r\n");
serialClose(fd);
exit(0);
}
int main(void)
{
if(MODE == 1){
if(wiringPiSetupGpio() < 0) { //use BCM2835 Pin number table
printf("set wiringPi lib failed !!! \r\n");
return 1;
} else {
printf("set wiringPi lib success !!! \r\n");
}
pinMode(EN_485, OUTPUT);
digitalWrite(EN_485,HIGH);
}
printf("use device %s\r\n", UART_DEV);
if((fd = serialOpen(UART_DEV, 115200)) < 0) {
printf("serial err\n");
return -1;
}
serialFlush(fd);
serialPrintf(fd,"\r");
char str[] = "hello world\n";
printf("send data:%s\r\n", str);
serialPuts(fd, str);
char str_buf[100];
printf("You can always receive data, press Ctrl + C to exit\r\n");
printf("You can enter up to %d characters\r\n", sizeof(str_buf));
for(;;){
scanf("%s", str_buf);
strcat(str_buf,"\n"); //Add newline
serialPuts(fd, str_buf);
}
serialClose(fd);
return 0;
}
can_receive.py
import os
import can
os.system('sudo ip link set can0 type can bitrate 100000')
os.system('sudo ifconfig can0 up')
can0 = can.interface.Bus(channel = 'can0', bustype = 'socketcan_ctypes')# socketcan_native
#msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False)
msg = can0.recv(10.0)
print msg
if msg is None:
print('Timeout occurred, no message.')
os.system('sudo ifconfig can0 down')
can_send.py
import os
import can
os.system('sudo ip link set can0 type can bitrate 100000')
os.system('sudo ifconfig can0 up')
can0 = can.interface.Bus(channel = 'can0', bustype = 'socketcan_ctypes')# socketcan_native
msg = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], extended_id=False)
can0.send(msg)
os.system('sudo ifconfig can0 down')
can_receive.c
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
int main()
{
int ret;
int s, nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
memset(&frame, 0, sizeof(struct can_frame));
system("sudo ip link set can0 type can bitrate 100000");
system("sudo ifconfig can0 up");
printf("this is a can receive demo\r\n");
//1.Create socket
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (s < 0) {
perror("socket PF_CAN failed");
return 1;
}
//2.Specify can0 device
strcpy(ifr.ifr_name, "can0");
ret = ioctl(s, SIOCGIFINDEX, &ifr);
if (ret < 0) {
perror("ioctl failed");
return 1;
}
//3.Bind the socket to can0
addr.can_family = PF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
if (ret < 0) {
perror("bind failed");
return 1;
}
//4.Define receive rules
struct can_filter rfilter[1];
rfilter[0].can_id = 0x123;
rfilter[0].can_mask = CAN_SFF_MASK;
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
//5.Receive data and exit
while(1) {
nbytes = read(s, &frame, sizeof(frame));
if(nbytes > 0) {
printf("can_id = 0x%X\r\ncan_dlc = %d \r\n", frame.can_id, frame.can_dlc);
int i = 0;
for(i = 0; i < 8; i++)
printf("data[%d] = %d\r\n", i, frame.data[i]);
break;
}
}
//6.Close the socket and can0
close(s);
system("sudo ifconfig can0 down");
return 0;
}
can_send.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
int main()
{
int ret;
int s, nbytes;
struct sockaddr_can addr;
struct ifreq ifr;
struct can_frame frame;
memset(&frame, 0, sizeof(struct can_frame));
system("sudo ip link set can0 type can bitrate 100000");
system("sudo ifconfig can0 up");
printf("this is a can send demo\r\n");
//1.Create socket
s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (s < 0) {
perror("socket PF_CAN failed");
return 1;
}
//2.Specify can0 device
strcpy(ifr.ifr_name, "can0");
ret = ioctl(s, SIOCGIFINDEX, &ifr);
if (ret < 0) {
perror("ioctl failed");
return 1;
}
//3.Bind the socket to can0
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
ret = bind(s, (struct sockaddr *)&addr, sizeof(addr));
if (ret < 0) {
perror("bind failed");
return 1;
}
//4.Disable filtering rules, do not receive packets, only send
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
//5.Set send data
frame.can_id = 0x123;
frame.can_dlc = 8;
frame.data[0] = 1;
frame.data[1] = 2;
frame.data[2] = 3;
frame.data[3] = 4;
frame.data[4] = 5;
frame.data[5] = 6;
frame.data[6] = 7;
frame.data[7] = 8;
printf("can_id = 0x%X\r\n", frame.can_id);
printf("can_dlc = %d\r\n", frame.can_dlc);
int i = 0;
for(i = 0; i < 8; i++)
printf("data[%d] = %d\r\n", i, frame.data[i]);
//6.Send message
nbytes = write(s, &frame, sizeof(frame));
if(nbytes != sizeof(frame)) {
printf("Send Error frame[0]!\r\n");
system("sudo ifconfig can0 down");
}
//7.Close the socket and can0
close(s);
system("sudo ifconfig can0 down");
return 0;
}