树莓派采集RS485数据

1. RS485介绍

RS485是有线传输串行数据的标,支持同时传输485总线上的多点数据。传输速率为10Mbps,传输距离可达50英尺。传输速率为100Kbps时,传输距离可达4000英尺。

RS485总线通常为4芯或2芯,现在普遍采用2芯总线。一条总线最多支持32个设备。总线之间还可以串接,从而支持成百以上的节点。

RS485和树莓派的连接:使用树莓派GPIO口与485相连

  • 配置
    $ sudo apt-get update
    $ sudo apt-get install python-pip
    $ sudo pip install wiringpi
    Use pip to install WiringPi (WiringPi is designed for raspberry pi to behave similarly to that of the wiring library under Arduino. After this library is installed, c or shell or python can use the function to configure and control GPIOs directly.
    $ sudo apt-get install python-serial
    Installed the associated library files of serial ports.
    $ sudo apt-get install python-rpi.gpio

  • 测试
    Test whether the GPIO library and the serial library is installed or not:
    $ python
    $ import RPi.GPIO #注意RPi.PGIO中i是小写
    $ import serial
    If there is no error , then the two libraries are installed correctly.

We need to configure file ‘/boot/cmdline.txt’ to remove the kernel booting information and debug message:
$ sudo nano /boot/cmdline.txt

You can see the following information:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Remove “console=ttyAMA0,115200 kgdboc=ttyAMA0,115200″,
so that the information becomes:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Disable log in from the serial port:
$ sudo nano /etc/inittab
comment out ”T0: 23: respawn:/sbin/getty-L ​​ttyAMA0 115200 vt100″

Restart Raspberry Pi:
$ sudo reboot
Now you can use /dev/ttyAMA0 like the regular COM port.

2. 测试代码

Test code(serial_test.py) :

import serial
port = “/dev/ttyAMA0”

usart = serial.Serial(port,9600)
usart.flushInput()
print (“serial test: BaudRate = 9600”)

usart.write(“please enter the character:\r”)
while True:
 if( usart.inWaiting()>0 ) :
  receive = usart.read(1)
  print “receive:”,receive
  usart.write(“send:”)
  usart.write(receive)
  usart.write(“\r”)

3. Wiring硬件管脚对应关系:

RS485 Shield ‘A’ ——-> RS232-RS485 converter ‘T/R +’
RS485 Shield ‘B’ ——-> RS232-RS485 converter ‘T/R-’
RS485 Shield ‘GND’—> RS232-RS485 converter ‘GND’
RS485-RS232的转换器有3个接口,A、B接地。其中A口是TR+,B口是TR-。
管脚定义如下:


硬件连接图:

4. 基于RS232转RS485的测试例程

  • PC机通过USB转232连232模块,232模块通过转换后接入到RS485 shield。PC机上启动超级终端,输入字符,在树莓派上能看到,就说明RS485 Shield能正确处理输入信号。

  • Use X-CTU in our case, and set the baud rate to 9600


    After running serial_test.py, enter the characters in the X-CTU:

  • 8
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值