Raspberry pi2串口通信

来源:互联网 发布:淘宝跳失率多少正常 编辑:程序博客网 时间:2024/06/10 16:39

安装pyserial的串口库,$ wget  http://downloads.sourceforge.net/project/pyserial/pyserial/2.7/pyserial-2.7.tar.gz

下载后,对压缩文件解压:

$ tar zxvf  ./pyserial-2.7.tar.gz

$ cd pyserial

$ sudo python setup.py  install

用命令查找串口设备

$ dmesg | grep tty*

打开python交互运行环境

 $ python

>>> import serial

>>> ser = serial.Serial('/dev/ttyUSB0',115200)

>>> while 1:

.... ser.readlin()


>>>ser.write('this is string')


连接到PC端串口调试助手,打印

Communication with ROS through USART Serial Port   

$ cd ~/catkin_ws/src$ catkin_create_pkg r2SerialDriver std_msgs rospy roscpp$ cd ~/catkin_ws$ catkin_make

To add the workspace to your ROS environment you need to source the generated setup file:

$ . ~/catkin_ws/devel/setup.bash$ cd ~/catkin_ws/src/r2SerialDriver/src

Add a file: "r2Serial.cpp", Source code:



Modify the CMakeList.txt file:

$ cd ~/catkin_ws/src/r2SerialDriver$ gedit CMakeList.txt

add/modify these lines as below:

[plain] view plain copy
print?在CODE上查看代码片
  1. find_package(catkin REQUIRED COMPONENTS  
  2.   roscpp  
  3.   rospy  
  4.   std_msgs  
  5. )  
  6.   
  7. include_directories(  
  8.   ${catkin_INCLUDE_DIRS}  
  9. )  
  10.   
  11. add_executable(r2SerialDriver src/r2Serial.cpp)  
  12.   
  13. add_dependencies(r2SerialDriver ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})  
  14.   
  15. target_link_libraries(r2SerialDriver  
  16.    ${catkin_LIBRARIES}  
  17.  )  

Save and Build the package again:

$ cd ~/catkin_ws$ catkin_make -DCMAKE_BUILD_TYPE=Release 

He also offered a simple launch file that interfaces to two microcontrollers using two serial connections, put the code into
  "~/catkin_ws/src/r2SerialDriver/launch/r2SerialDriver.launch" file:

[html] view plain copy
print?在CODE上查看代码片
  1. <launch>  
  2.   <node pkg="r2SerialDriver" type="r2SerialDriver" name="r2Serial0" args="0 /dev/ttyUSB0 9600" output="screen" >  
  3.   <remap from="ucCommand" to="uc0Command" />  
  4.   <remap from="ucResponse" to="uc0Response" />  
  5.   </node>  
  6.   
  7.   <node pkg="r2SerialDriver" type="r2SerialDriver" name="r2Serial1" args="1 /dev/ttyUSB1 9600" output="screen" >  
  8.   <remap from="ucCommand" to="uc1Command" />  
  9.   <remap from="ucResponse" to="uc1Response" />  
  10.   </node>  
  11.   
  12. </launch>  

Usage:

$ rosrun r2SerialDriver r2SerialDriver 0 /dev/ttyUSB0 9600

or

$ roslaunch r2SerialDriver r2SerialDriver.launch

try:

$ rostopic pub -r 1 /uc0Command std_msgs/String hello_my_serial$ rostopic echo /uc0Response
You may also need to remap the /dev/ttyUSB* to some name you like cuz you may have several usb-serial devices.
If so, just Ref:重新指派usb转串口模块在linux系统中的设备调用名称(English Version: remap your usb-serial devices' names in Linux )

But Kevin mentioned a question: "Interesting, but a lot of my serial stuff is send a message and receive a response back. Instead of separate publish/subscribes have you tried a service? That seems like it would work nicely for this."

I also noticed a problem: The Linux's System RAM and CPU will be much costed by r2serial_driver when it is running.

Next time, we'll see how to use Kevin's service to play ROS serial communication.

2. Use Service to play ROS-Serial communication

see this blog brother below...

http://blog.csdn.net/sonictl/article/details/51372534


0 0