acm为什么不能用python_OSError:[Errno 13]权限被拒绝:'/ dev / ttyACM0'-使用从Python到Arduino的pyserial...

Environment

Linux Mint 17.1

Python 2.7

pyserial 2.7

Arduino UNO rv3

Desired Behaviour

I'm trying to send three values from a Python application to Arduino.

It works when doing the following from terminal:

$ python

$ import serial

$ import struct

$ ser = serial.Serial('/dev/ttyACM0', 9600)

$ ser.write(struct.pack('>3B', 255, 0, 0))

Current Behaviour

It doesn't work when using the same code in a Python file ie:

import serial

import struct

ser = serial.Serial('/dev/ttyACM0', 9600)

ser.write(struct.pack('>3B', red_value, green_value, blue_value))

Error Message

$ sudo tail -100 /var/log/apache2/error.log

OSError: [Errno 13] Permission denied: '/dev/ttyACM0'

Troubleshooting

Permissions

Application file:

$ ls -l

-rwxr-xr-x 1 myname mygroupname 114146 Jan 9 19:16 my_application.py

ttyACM0:

ls -l /dev/ttyACM0

crw-rw---- 1 root dialout 166, 0 Jan 9 20:12 /dev/ttyACM0

Groups

Groups the owner is a member of:

$ groups

mygroupname adm dialout cdrom sudo dip plugdev lpadmin sambashare

Due to various suggestions on the internet I also added the owner to the tty group via System Settings > Users and Groups. This had no effect.

Serial Ports Available

$ dmesg | grep tty

[ 0.000000] console [tty0] enabled

[ 3390.614686] cdc_acm 3-2:1.0: ttyACM0: USB ACM device

Update

I can force it to work under the following conditions:

01. Permissions for world must be set to rw ie:

sudo chmod 666 /dev/ttyACM0

02. Arduino IDE serial monitor needs to be open.

However these conditions are not sustainable as:

Permissions are reset each time the USB is connected.

The Arduino IDE serial monitor shouldn't need to be open.

解决方案

The following fleshes out some of the ideas in the first answer (I tried to add this content to that answer and accept it, but the edits were rejected). I'm not an expert in the area, so please just use this information to support your own research.

You can do one of the following:

01. Alter the permissions on /dev/ttyACM0 so that world has read and write priviliges (something you may not want to do) - although you may find they reset each time the device is plugged in eg:

sudo chmod 666 /dev/ttyACM0

02. Create a rule in /etc/udev/rules.d that will set the permissions of the device (a restart will be required):

# navigate to rules.d directory

cd /etc/udev/rules.d

#create a new rule file

sudo touch my-newrule.rules

# open the file

sudo vim my-newrule.rules

# add the following

KERNEL=="ttyACM0", MODE="0666"

This also sets permissions for world to read and write, which you may not want to do.

For more information about this approach, see these answers:

03. The third option, which is the option I implemented, adds the Apache user to the dialout group so that if the script is being run by Apache, then it can access the device.

a) Find the location of your Apache config file, then search for the User setting within that file:

# open file in editor

sudo vim /etc/apache2/apache2.conf

# search for User setting

/User

You may find something like:

# These need to be set in /etc/apache2/envvars

User ${APACHE_RUN_USER}

Group ${APACHE_RUN_GROUP}

b) Quit vim and search for APACHE_RUN_USER in /etc/apache2/envvars (if the above scenario applies):

# open file in editor

sudo vim /etc/apache2/envvars

# search for APACHE_RUN_USER

/APACHE_RUN_USER

You may find something like:

export APACHE_RUN_USER=www-data

c) Add the User www-data to the dialout group:

sudo usermod -a -G dialout www-data

d) Restart.

As the Apache user has been added to the dialout group, the script should now be able to access the device.

Further Reading

How to find the location of the Apache config file:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值