boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Serial communication Tx/Rx

Sun Jul 03, 2016 12:20 pm

I tried the following but I get a permission denied error in Python:

Code: Select all

import serial
port = serial.Serial("/dev/ttyAMA0", baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=3.0)
So how do you do this correctly.

I am a complete newbie to python by the way.

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 12:44 pm

Add your userid to the dialout group.
sudo usermod -G dialout $(whoami)
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 1:06 pm

Still get permission denied.

Do you need to restart the Pi or something for the change to take effect?

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 1:15 pm

boylesg wrote:Still get permission denied.

Do you need to restart the Pi or something for the change to take effect?
You need to log out and log back in for user permission changes to take effect.

If it doesn't work could you say what ls -l /dev/ttyAMA0 reports?

User avatar
bensimmo
Posts: 4622
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 1:29 pm

Is it not serial0 now, especially if using the Pi3?
Would the Bluetooth deny access to ttyAMA0 ?

Of course it could be the other Pi's?


I never had to alter any groups (but I am 'pi') to get serial working.

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 1:49 pm

bensimmo wrote:Is it not serial0 now, especially if using the Pi3?
Would the Bluetooth deny access to ttyAMA0 ?

Of course it could be the other Pi's?


I never had to alter any groups (but I am 'pi') to get serial working.
Pi 2 B+ I think it is.

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 1:55 pm

joan wrote:
boylesg wrote:Still get permission denied.

Do you need to restart the Pi or something for the change to take effect?
You need to log out and log back in for user permission changes to take effect.

If it doesn't work could you say what ls -l /dev/ttyAMA0 reports?
crw--w---- 1 root tty 204, 64 Jul 3 23:48 /dev/ttyAMA0

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 2:05 pm

sudo chown pi:pi /dev/ttyACM0

This did the trick.

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 2:23 pm

I have my arduino uno connected to my pi tx/rx pins on the pi and D5 and D6 on the uno.

I am using SoftwareSerial on the Uno. I seem to be getting a bit of garbage in ArduinIDE serial monitor. I am wondering if software serial on the UNO is a problem for the Pi - I was reading some where that software serial on the Pi is not possible because it is not a real time operating system.

All I want to do at this stage is write some text to the serial port in Python and then see that text in the ArduinoIDE serial monitor.

I am getting a serial write exception Errno 5 in python though.

But I am getting a rapsberry pi password request (as well as the garbage) in the arduino serial monitor.

WTF is going on here?

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 2:43 pm

boylesg wrote:sudo chown pi:pi /dev/ttyACM0

This did the trick.
That's an ugly temporary hack. Add user pi to the tty group or your program will break next time you reboot your RPi.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 3:07 pm

DougieLawson wrote:
boylesg wrote:sudo chown pi:pi /dev/ttyACM0

This did the trick.
That's an ugly temporary hack. Add user pi to the tty group or your program will break next time you reboot your RPi.
'sudo adduser pi dialout' does nothing to solve the problem in python.

The 'ugly hack' is the only thing that works at present.

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 3:29 pm

How about sudo adduser -G tty pi ? Or you've got to write a udev rule to change the ownership every time you reboot.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 3:32 pm

DougieLawson wrote:How about sudo adduser -G tty pi ? Or you've got to write a udev rule to change the ownership every time you reboot.
Option g is ambiguous (gecos, gid, group)

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 4:02 pm

Why is this so bloody difficult???
Raspberry Pi Serial GPIO Configuration

0. if you have not seen my article on how to remote access your Raspberry Pi, take a look here:

https://oscarliang.com/setup-raspberry- ... te-access/

1. In order to use the Raspberry Pi’s serial port, we need to disable getty (the program that displays login screen) by find this line in file /etc/inittab
Found the above except there no such file as /etc/inittab on my pi.

So what do I do?

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 4:10 pm

Looks like this ttyAMA0 business is bogus.
It appears as though the serial port is tty1 because port = serial.Serial('/dev/tty1', baudrate=9600) works in python for me.
Found tty1 in /boot/cmdline.txt
You are supposed to remove console=tty1 from the line of text in that file which, as I understand it, should prevent the RPi from sending the login rubbish to my arduino.

Does that sound right?

I have been reading this: https://oscarliang.com/raspberry-pi-and ... rial-gpio/

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 6:14 pm

Ah ha!

Found this for disabling pi login over serial.

http://www.instructables.com/id/Read-an ... pberry-Pi/

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 7:08 pm

Are you using?
  • the Pi's UART (/dev/ttyAMA0) which means you use pins 8/10 at the Pi or
  • an Arduino to Pi USB dongle (/dev/ttyACM0) which means you use USB at the Pi end.
That makes a difference.

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: Serial communication Tx/Rx

Sun Jul 03, 2016 7:30 pm

boylesg wrote:I have my arduino uno connected to my pi tx/rx pins on the pi and D5 and D6 on the uno.

I am using SoftwareSerial on the Uno. I seem to be getting a bit of garbage in ArduinIDE serial monitor. I am wondering if software serial on the UNO is a problem for the Pi - I was reading some where that software serial on the Pi is not possible because it is not a real time operating system.

All I want to do at this stage is write some text to the serial port in Python and then see that text in the ArduinoIDE serial monitor.

I am getting a serial write exception Errno 5 in python though.

But I am getting a rapsberry pi password request (as well as the garbage) in the arduino serial monitor.

WTF is going on here?
Try...
Hcorr = 'test'
ser.write(bytes(Hcorr.encode('ascii')))

You may need to add some delay after opening the port for the uno to settle.

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Mon Jul 04, 2016 9:12 am

Don't know how but it appears my serial port has renamed from ttyAMA0 to tty0.
And I still get permission denied when I try to open it in python.
Any further suggestions.

boylesg
Posts: 39
Joined: Fri Jan 15, 2016 3:23 pm

Re: Serial communication Tx/Rx

Mon Jul 04, 2016 9:45 am

boylesg wrote:Don't know how but it appears my serial port has renamed from ttyAMA0 to tty0.
And I still get permission denied when I try to open it in python.
Any further suggestions.
Actually what had happened here is that I disabled login over serial in raspi-config which apparently also disables the UART serial port as well.
So I went into config.txt and changed UART=0 to UART=1 and ttyAMA0 re-appeared.
And now when I open it in python it works!
And I can send text from the Python to the serial monitor of Arduino IDE.....cool.

If I send text the other way how does one view it in python?

Return to “Python”