", line 1, in File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line _树莓派isb转ttl">
当前位置:   article > 正文

树莓派使用USB To TTL 收发数据(Python版本)_树莓派isb转ttl

树莓派isb转ttl

1.出现问题:

>>> import serial
>>> ser= serial.Serial('/dev/ttyUSB0',115200,timeout=0.5)
Traceback (most recent call last):
  File "<stdin>", 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:~$
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2.树莓派如何使用USB转串口功能

1、插上USB转TTL模块,如CH340、PL2303、CP2102等,用lsusb命令查看usb设备
2、树莓派默认安装了python 和pyserial, 使用pip命令查看一下有没有 命令行输入pip list,查看到安装pyserial 版本号为3.2.1
3、python脚本测试串口,打开串口,设置波特率115200,把串口的TX和RX短路接到一起,发送123456,接收到123456
代码如下:
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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

3.树莓派学习:连接USB转串口模块CH340使用

1.USB转串口模块CH340,插入树莓派USB口,命令行 lsusb 查看设备是否识别。
2.查看识别成的串口号 ls -l /dev/tty*,通过列表可知识别为ttyUSB0
3.查看串口波特率 stty -F /dev/ttyUSB0
4.查看串口连接信息,命令行输入 dmesg | grep ttyUSB0

4.出现问题

‘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 -*-
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

5.ROS学习总结1-自定义消息并实现话题通讯(python)(其中有 关于python 文件的.修改package.xml.修改CMakeLists.txt)

2.修改package.xml

因为需要message_generation生成C++或Python能使用的代码,所以向package.xml文件中添加如下两行:

<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
  • 1
3.修改CMakeLists.txt

CMakeLists.txt中有四个地方需要修改:

(1)向find_package()中添加message_generation

find_package(
  catkin REQUIRED COMPONENTS
  message_generation
  rospy
  std_msgs
)
  • 1
  • 2
  • 3
  • 4
  • 5

(2)向add_message_files()中添加complex.msg

add_message_files(
  FILES
  complex.msg
)
  • 1
  • 2
  • 3

(3)去掉generate_messages()注释

generate_messages(
  DEPENDENCIES
  std_msgs
)
  • 1
  • 2
  • 3

(4)向catkin_package()添加message_runtime

catkin_package(
  CATKIN_DEPENDS
  message_runtime
  )
  • 1
  • 2
  • 3

至此完成新消息的创建,回到工作空间执行catkin_make后即可在python程序中引入新消息。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/893168?site
推荐阅读
相关标签