文章目录
环境
Ubuntu16.04
ROS Kinetic
PC与开发板1串口通信示例
#usr/bin/python3
# -*- coding: utf-8 -*-
import rospy
from std_msgs.msg import String
import serial
import time
def gbk_2_number(strsrc):
result = []
for i in range(0,len(strsrc),2):
tmp = strsrc[i:i+2]
result.append(tmp)
#print(tmp, result)
tmp1 = ''
for i in range(len(result)):
if result[i]== '2e':
tmp1 += '.'
else:
tmp1 += str(int(result[i]) - 30)
#print('tmp: ',tmp1)
return tmp1
pub = rospy.Publisher('test_talker', String, queue_size = 10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10)
ser = serial.Serial('/dev/ttyUSB0', 57600, timeout=0.5)
# 选择串口,并设置波特率
while not rospy.is_shutdown():
if ser.is_open:
print("port open success")
# hex(16进制)转换为bytes(2进制),应注意Python3.7与Python2.7此处转换的不同
send_data = bytes.fromhex('65 0d 05 00 FF FF FF FF') # 发送数据转换为b'\xff\x01\x00U\x00\x00V'
ser.write(send_data) # 发送命令
time.sleep(0.1) # 延时,否则len_return_data将返回0,此处易忽视!!!
len_return_data = ser.inWaiting()
if len_return_data:
#print("enter receive data: ")
return_data = ser.read(len_return_data) # 读取缓冲数据
print('16hex rx: ',return_data.hex())
rospy.loginfo(hello_str)
pub.publish(hello_str)
# bytes(2进制)转换为hex(16进制),应注意Python3.7与Python2.7此处转换的不同,并转为字符串后截取所需数据字段,再转为10进制
str_return_data = str(return_data.hex())
#十六进制字符转换为汉字(str_return_data)
rece_Chinese = bytes.fromhex(str_return_data).decode('gbk')
print('rx: ', rece_Chinese)
start_l = str_return_data.find('22')
end_r = str_return_data.rfind('22')
number_x = str_return_data[start_l+2:end_r]
number_result = gbk_2_number(number_x)
print('number_result: ',number_result)
feedback_data = int(str_return_data[-6:-2], 16)
#print(feedback_data)
else:
print("port open failed")
PC与开发板2串口通信示例
#!/usr/bin/env python
#coding=utf-8
import rospy
import serial
from geometry_msgs.msg import Twist
import time
def main():
ser = serial.Serial('/dev/ttyUSB1', 256000, timeout=0.5)
rospy.loginfo("Received a OBD message!")
if ser.is_open:
while(True):
print("port open success")
send_data = 'AT+DS049,DS041,DS048,DS098,DS123'
ser.write(send_data) # 发送命令
time.sleep(0.1) # 延时,否则len_return_data将返回0,此处易忽视!!!
len_return_data = ser.inWaiting()
if len_return_data:
#print("enter receive data: ")
readline = ser.readline()
print('len readline: ',len(readline))
print(readline)
if len(readline) == 11:
send_data = 'AT+ISO15765-4STD_500K'
ser.write(send_data) # 发送命令
time.sleep(0.1) # 延时,否则len_return_data将返回0,此处易忽视!!
if len(readline)==55:
print(readline)
rospy.loginfo("readline:[%s]"%readline)
str_return_data = str(readline.encode('hex')) #str(return_data.encode('hex'))
#print('rx: ',str_return_data)
feedback_data = int(str_return_data[-6:-2], 16)
#print(feedback_data)
else:
print("no data received!")
time.sleep(3)
else:
print("port open failed")
if __name__=="__main__":
main()
测试USB接口
1、连接设备并检查串口连接状态
dmesg | grep ttyUSB*
2、通过cutecom串口助手检查串口设备是否能进行通讯,若因权限问题无法连接,运行以下命令给与用户权限
sudo chmod 666 /dev/ttyUSB*
编译运行
chmod u+x ROS_Serial.py //将python文件改为执行文件
回到ROS的根工作目录
catkin_make //编译
roscore //这个开启一次即可
另起一个终端:
source devel/setup.dash //配置工作空间,用于刷新环境,必不可少,将当前的工作空间配置了
rosrun test ROS_Serial.py
接收到的结果显示: