树莓派如何采集RS数据.docx
《树莓派如何采集RS数据.docx》由会员分享,可在线阅读,更多相关《树莓派如何采集RS数据.docx(6页珍藏版)》请在冰豆网上搜索。
树莓派如何采集RS485数据
1、RS485介绍
RS485是有线传输串行数据的标,支持同时传输485总线上的多点数据。
传输速率为10Mbps,传输距离可达50英尺。
传输速率为100Kbps时,传输距离可达4000英尺。
RS485总线通常为4芯或2芯,现在普遍采用2芯总线。
一条总线最多支持32个设备。
总线之间还可以串接,从而支持成百以上的节点。
RS485主要技术规格如下:
RS485specificationoverview
Attribute
Specification
Cabling
Multi-drop
Numberofdevices
32transmitters
32receivers
Communicationsmodes
halfduplex
Maximumdistance
4000feet@100kbps
Maximumdatarate
10Mbps@50feet
Signalling
Balanced
Mark(data=1)
condition
1.5Vto5V(BgreaterthanA)
Space(data=0)
condition
1.5Vto5V(AgreaterthanB>
Driveroutputcurrentcapability
250mA
2、RS485和树莓派的连接
目前有一种RS485Shield的设备,可以直接连接到树莓派上。
硬件连接图如下:
3、配置
Updatesourcelist
$sudoapt-getupdate
Installpython-pip
$sudoapt-getinstallpython-pip
UsepiptoinstallWiringPi(WiringPiisdesignedforraspberrypitobehavesimilarlytothatofthewiringlibraryunderArduino. Afterthislibraryis installed,c orshellorpythoncanusethe functiontoconfigureandcontrolGPIOsdirectly. :
$sudopipinstallwiringpi
Installedthe associatedlibraryfilesofserialports:
$sudoapt-getinstallpython-serial
.TestwhethertheGPIOlibraryandtheseriallibraryisinstalledornot:
$python
$importRPi.GPIO
$importserial
Ifthereisnoerror,thenthetwolibrariesareinstalledcorrectly.
Weneedtoconfigure file‘/boot/cmdline.txt’toremovethekernelbootinginformationanddebugmessage:
$sudonano/boot/cmdline.txt
Youcanseethefollowinginformation:
dwc_otg.lpm_enable=0console=ttyAMA0,115200kgdboc=ttyAMA0,115200console=tty1root=/dev/mmcblk0p2rootfstype=ext4elevator=deadlinerootwait
Remove“console=ttyAMA0,115200kgdboc=ttyAMA0,115200″sothattheinformationbecomes:
dwc_otg.lpm_enable=0console=tty1root=/dev/mmcblk0p2rootfstype=ext4elevator=deadlinerootwait
Disableloginfromtheserialport:
$sudonano/etc/inittab
andcommentout”T0:
23:
respawn:
/sbin/getty-LttyAMA0115200vt100″
RestartRaspberryPi:
$sudoreboot
Nowyoucanuse/dev/ttyAMA0liketheregularCOMport.
4、测试代码
Testcode(serial_test.py) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
importserial
port=”/dev/ttyAMA0″
usart=serial.Serial(port,9600)
usart.flushInput()
print(“serialtest:
BaudRate=9600″)
usart.write(“pleaseenterthecharacter:
\r”)
whileTrue:
if(usart.inWaiting()>0):
receive=usart.read
(1)
print”receive:
”,receive
usart.write(“ send:
’”)
usart.write(receive)
usart.write(“‘\r”)
5、1xRaspberry Pi
6、1xRS485 shield forRaspberryPi
7、
8、3xMaletomalejumperwires
9、1x RS232-RS485converter
10、
1x 9DBmaleofserial-to-USBcable:
11、
12、Wiring:
13、RS485Shield‘A’->RS232-RS485converter‘T/R+‘
14、RS485Shield‘B’->RS232-RS485converter‘T/R-‘
15、RS485Shield‘GND’->RS232-RS485converter’GND‘
16、
17、
6、基于RS232转RS485的测试例程
硬件连接方式:
硬件管脚对应关系:
RS485Shield‘A’->RS232-RS485converter‘T/R+‘
RS485Shield‘B’->RS232-RS485converter‘T/R-‘
RS485Shield‘GND’->RS232-RS485converter’GND‘
Afterthewiringisdone,launchaserialterminal.WeuseX-CTUinourcase,andsetthebaudrate to9600
Afterrunningserial_test.py,enterthecharactersintheX-CTU:
18、Afterthewiringisdone,launchaserialterminal.WeuseX-CTUinourcase,andsetthebaudrate to9600:
19、
20、Afterrunningserial_test.py,enterthecharactersintheX-CTU:
21、
22、