>>> import serial
>>> ser= serial.Serial('/dev/ttyUSB0',115200,timeout=0.5)
Traceback (most recent call last):
File "" , line 1, in <module>
File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 240, in __init__
self.open()
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 268, in open
raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
serial.serialutil.SerialException: [Errno 13] could not open port /dev/ttyUSB0: [Errno 13] Permission denied: '/dev/ttyUSB0'
>>>
KeyboardInterrupt
>>> exit()
bruce@bruce-desktop:~$
bruce@bruce-desktop:~$
bruce@bruce-desktop:~$
解决方法
bruce@bruce-desktop:~$ sudo chmod 666 /dev/ttyUSB0
bruce@bruce-desktop:~$
代码如下:
bruce@bruce-desktop:~$
bruce@bruce-desktop:~$ python
Python 2.7.15+ (default, Oct 7 2019, 17:39:04)
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> ser = serial.Serial('/dev/ttyUSB0',115200,timeout=0.5)
>>> ser.isOpen()
True
>>> ser.write("12345")
5
>>> ser.inWaiting()
5
>>> ser.read(4)
'1234'
>>> ser.read(5)
'5'
>>> ser.read(5)
''
>>> exit()
‘import: not authorized `os' @ error/constitute.c/WriteImage/1028’
解决方法:
#!/usr/bin/env python这一行表明这是一个python程序,将发送给python解释器,如果没有这一行,会出现‘import: not authorized `os' @ error/constitute.c/WriteImage/1028’这个错误代码。
一般开头会有两行,都加上最好
#!/usr/bin/env python
# -*- coding: utf-8 -*-
因为需要message_generation生成C++或Python能使用的代码,所以向package.xml文件中添加如下两行:
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
CMakeLists.txt中有四个地方需要修改:
(1)向find_package()中添加message_generation
find_package(
catkin REQUIRED COMPONENTS
message_generation
rospy
std_msgs
)
(2)向add_message_files()中添加complex.msg
add_message_files(
FILES
complex.msg
)
(3)去掉generate_messages()注释
generate_messages(
DEPENDENCIES
std_msgs
)
(4)向catkin_package()添加message_runtime
catkin_package(
CATKIN_DEPENDS
message_runtime
)
至此完成新消息的创建,回到工作空间执行catkin_make后即可在python程序中引入新消息。