Page 1 of 1

ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Sun Jan 29, 2017 11:11 am
by AcidBurns87
there has been a lot of trouble with Rasbpian Jessie and the RPi 3 if you want to use the internal serial port for other communications besides Bluetooth or a login console.

However I am struggling to get the Serial to work with my old B+ and the newest Jessie OS (20170111 or so).

What I did until now:
- read almost every tutorial on the net about it, but most are for wheezy.
- edited cmdline.txt to remove the login console ("console=ttyAMA0,115200 ") and rebooted
- disabled the serial port via raspi-config (results in a disappearing of ttyAMA0)
- messed with config.txt (enable_uart=1), ttyAMA0 reappeared but is still not interfaceable with minicom
- tried to kill the login service, but for me there is no [email protected] somewhere

What I did to test:
connect TxD to RxD and tried to open /dev/ttyAMA0 on minicom with a baudrate of 9600 and try to see if the text I am sending is echoed back.

Code: Select all

minicom -b 9600 -D /dev/ttyAMA0
What did I miss or what do I have to do from a fresh Jessie to get ttyAMA0 free?

Sincerely,

Julian

Re: ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Sun Jan 29, 2017 5:28 pm
by dgordon42
Check the '/boot/config.txt' file for UART commands with:

Code: Select all

grep uart /boot/config.txt
This command should return one line: "enable_uart=1"
If it returns more then one line, or returns any other UART commands, or returns a line with spaces in the command, use a text editor to comment out or delete these commands. '/boot/config.txt' should contain just one instance of "enable_uart=1" (note no spaces) to get the Serial Port working on all Pi models.

Use a text editor to examine the '/boot/cmdline.txt' file. If you find an entry "console=ttyAMA0,115200" or "console=serial0,115200", remove them. Be carefull when editing this file, do not split the line in the file when editing, the file must only contain 1 line. The file normally contains an entry "console=tty1", this is normal, leave it alone. This action prevents Raspbian from sending boot messages to the Serial Port, and expecting a log-in from the Serial Port.

The 'raspi-config' utility makes changes to the above mentioned files, so do not use it to change the Serial Port settings after making the above checks.

Reboot your Pi.

You should refer to your Serial Port as "/dev/serial0", not "/dev/ttyAMA0". This will ensure that your code will work in all models of the Pi, and will work with future updates to Raspbian. That's serialZero, not serialOh.

I don't have minicom, but you can check that your Serial port is working by joining the Tx and Rx pins together (power off first!) and running this Python script:

Code: Select all

#!/usr/bin/env python
# serial_port_loopback.py
# Will also work on Python3.
# Serial port testing for a RaspberryPi.

from __future__ import print_function
import serial

test_string = "Testing 1 2 3 4".encode('utf-8')
#test_string = b"Testing 1 2 3 4" ### Will also work

port_list = ["/dev/serial0", "/dev/ttyAMA0", "/dev/serial1", "/dev/ttyS0"]

for port in port_list:

    try:
        serialPort = serial.Serial(port, 9600, timeout = 2)
        serialPort.flushInput()
        serialPort.flushOutput()
        print("Opened port", port, "for testing:")
        bytes_sent = serialPort.write(test_string)
        print ("Sent", bytes_sent, "bytes")
        loopback = serialPort.read(bytes_sent)
        if loopback == test_string:
            print ("Received", len(loopback), "valid bytes, Serial port", port, "working \n")
        else:
            print ("Received incorrect data", loopback, "over Serial port", port, "loopback\n")
        serialPort.close()
    except IOError:
        print ("Failed at", port, "\n")

On a RaspberryPi B+, it should pass on 'serial0' and 'ttyAMA0', and fail on 'serial1' and 'ttyS0'.

I'm not sure if the 'python-serial' module is part of Jessie Lite, if you get an error about no module named serial, install it with:

Code: Select all

sudo apt-get update
sudo apt-get install python-serial
Good Luck,
Dave.

Re: ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Tue Jan 31, 2017 9:43 pm
by AcidBurns87
okay, tried everything.
cmdline.txt and config.txt already were to your specifications.

on the emonpi image (based on raspbian jessie lite, but highly modified):
However I encountered something really strange: on all my 3 Pi B+ the /dev/ttyAMA0 are recieving invalid data (even though I just unboxed one of them). serial0, serial1 and ttyS0 are non existent.

On the RPi3 the ttyAMA0 also is invalid (it says recieved invalid bytes), but ttyS0 works. serial0 and serial1 are non existent.

On some really really old 1st Gen Pi B's (one of them probably was under the first 20 pi's sold to germany) ttyAMA0 is working as expected. again ttyS0 serial1 and serial0 non existent.

on the raspbian jessie lite image (stock, just apt-get update && apt-get upgrade && apt-get dist-upgrade after password change):
serial0 and ttyAMA0 are present, however both yield incorrect data from the python script on a RPi B+. the python script crashes (hangs) on ttyAMA0 on a RPi3.

So what is going on here? Shouldn't ttyAMA0 be consistently working/non working with the same SD card?

Re: ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Tue Jan 31, 2017 11:29 pm
by dgordon42
AcidBurns87 wrote:Shouldn't ttyAMA0 be consistently working/non working with the same SD card?
Basically no. There are hardware differences between the Pi 3B and the previous models. By default, '/dev/ttyS0' is the UART used in the Pi 3B, and '/dev/ttyAMA0' is the UART used on the other models.
The answer I gave previously was for a Pi B+ running up to date Raspbian Jessie, as that is the model Pi you asked about in your first post.
AcidBurns87 wrote:On some really really old 1st Gen Pi B's (one of them probably was under the first 20 pi's sold to germany) ttyAMA0 is working as expected. again ttyS0 serial1 and serial0 non existent.
This is expected behaviour on Pi's running Raspbian Wheezy on an old (pre Feb 2016) copy of Raspbian Jessie.

'/dev/ttyS0' was introduced in the Pi 3B which launched on 29 Feb 2016, and the aliases '/dev/serial0' and '/dev/serial1' followed on a month or so after that. If you wish to use the same SD Card in your different model Pi's, it must contain Raspbian Jessie dated later than March 2016, and idlealy be running Jessie dated 15 Dec 2016 or later.
Check your version's date with:

Code: Select all

uname -a
Update Jessie with:

Code: Select all

sudo apt-get update
sudo apt-get upgrade
The latest Pi 3B updates are not available for Wheezy.
Don't use 'rpi-update' to update(as suggested on some sites), it loads an it-test version of Raspbian which may cause you problems.

On up to date Raspbian Jessie:-
Use '/dev/serial0' to access the Serial Port
On a default Pi 3B this will point to '/dev/ttyS0'
On other models this will point to '/dev/ttyAMA0'
But it will always point to the correct UART regardless of which Pi you use.
You can check the above with:

Code: Select all

ls -lh /dev/serial0
Pi 3B returns wrote:lrwxrwxrwx 1 root root 5 Jan 29 19:51 /dev/serial0 -> ttyS0
Pi B+ returns wrote:lrwxrwxrwx 1 root root 7 Jan 29 19:06 /dev/serial0 -> ttyAMA0
If the above command returns a error like "ls: cannot access /dev/serial0: No such file or directory", and you are running up to date Jessie, check your '/boot/config.txt' file with the 'grep' command in my previous post.
It is normal to see the above error on Wheezy, or old versions of Jessie.
Check your OS name with:

Code: Select all

cat /etc/os-release
Hope this helps,
Dave.

Re: ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Tue Jan 31, 2017 11:35 pm
by AcidBurns87
I edited the post above...

I am trying to run emonpi (an up to date jessie image), but even with the stock raspbian jessie lite I got some problems on all of my Pis. I changed the jumper wire and also unboxed a new Pi3.

on the raspbian jessie lite image (stock, just apt-get update && apt-get upgrade && apt-get dist-upgrade after password change):
serial0 and ttyAMA0 are present, however both yield incorrect data from the python script on a RPi B+. the python script crashes (hangs) on ttyAMA0 on a RPi3.

uname -a yields:

Code: Select all

Linux raspberrypi 4.4.38-v7+ #938 SMP Thu Dec 15 15:22:21 GMT 2016 armv7l GNU/Linux
and I tested all my pis with the same micro sd card, so all have the same operating system in place.

Re: ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Wed Feb 01, 2017 12:05 am
by dgordon42
AcidBurns87 wrote:on the raspbian jessie lite image (stock, just apt-get update && apt-get upgrade && apt-get dist-upgrade after password change):
serial0 and ttyAMA0 are present, however both yield incorrect data from the python script on a RPi B+. the python script crashes (hangs) on ttyAMA0 on a RPi3.
The Python script I posted was for a Pi B+, not a Pi 3B.
On a default Pi 3B, '/dev/serial1' and '/dev/ttyAMA0' are used by the Bluetooth device, so the Python script cannot access them and it hangs.
If you edit the script so that the line:

Code: Select all

port_list = ["/dev/serial0", "/dev/ttyAMA0", "/dev/serial1", "/dev/ttyS0"]
becomes

Code: Select all

port_list = ["/dev/serial0"]
it should run without error on the 3B and the B+ provided the SD Card Raspbian is up to date, which yours seems to be.
Note that I have seen this script fail sometimes on the first run after a reboot, if it fails try running it again.
If it fails twice, check the '/boot/config.txt' and '/dev/cmdline.txt' files as per my first post.

I don't know anything about 'emonpi', but I would expect the Serial Port to work as it does in Jessie.

Good Luck,
Dave.

Re: ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Tue Aug 21, 2018 12:09 pm
by gangwar
I am getting the following on running the test script :

Code: Select all

Opened port /dev/serial0 for testing:
Sent 15 bytes
Received incorrect data b'' over Serial port /dev/serial0 loopback

Opened port /dev/ttyAMA0 for testing:
Sent 15 bytes
Received incorrect data b'' over Serial port /dev/ttyAMA0 loopback
Why am I not receiving the test_string?

Re: ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Tue Aug 21, 2018 4:44 pm
by dgordon42
gangwar wrote:Why am I not receiving the test_string?
I would need to know your Pi model, your OS, your Python version, your wiring, and see the script you are running before I could even guess.

Dave.

Re: ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Mon Jul 22, 2019 3:01 pm
by hemanth2607
gangwar wrote:
Tue Aug 21, 2018 12:09 pm
I am getting the following on running the test script :

Code: Select all

Opened port /dev/serial0 for testing:
Sent 15 bytes
Received incorrect data b'' over Serial port /dev/serial0 loopback

Opened port /dev/ttyAMA0 for testing:
Sent 15 bytes
Received incorrect data b'' over Serial port /dev/ttyAMA0 loopback
Why am I not receiving the test_string?
Hello gangwar ,

I am a first time user of Raspberry PI and currently started with Raspberry PI 3 B+ and trying to receive UART message from Nucleo ST-link on Raspberry PI Tx and Rx pins

following the below procedure posted in the link.

[url]https://modis.io/blog/uart_interface/

i see a similar data b' ' on the console on raspberry PI B+.

Were you able to solve this issue in your case ? If so kindly share the solution.

Re: ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Mon Jul 22, 2019 3:02 pm
by hemanth2607
gangwar wrote:
Tue Aug 21, 2018 12:09 pm
I am getting the following on running the test script :

Code: Select all

Opened port /dev/serial0 for testing:
Sent 15 bytes
Received incorrect data b'' over Serial port /dev/serial0 loopback

Opened port /dev/ttyAMA0 for testing:
Sent 15 bytes
Received incorrect data b'' over Serial port /dev/ttyAMA0 loopback
Why am I not receiving the test_string?
Hello gangwar ,

I am a first time user of Raspberry PI and currently started with Raspberry PI 3 B+ and trying to receive UART message from Nucleo ST-link on Raspberry PI Tx and Rx pins

following the below procedure posted in the link.

[url]https://modis.io/blog/uart_interface/

i see a similar data b' ' on the console on raspberry PI B+.

Were you able to solve this issue in your case ? If so kindly share the solution.

Re: ttyAMA0 Serial on Rasbian Jessie @RPi B+

Posted: Tue Jul 23, 2019 2:26 pm
by dgordon42
@hemanth2607,

Two things:

First, put the following line in as the first line in your Python Code running on the Pi:

Code: Select all

#!/usr/bin/env python3
This line is not a comment, it's a 'shebang', and it ensures your code runs on Python3, rather than the default Python2

Second, you should replace all instances of "/dev/ttyAMA0" with "/dev/serial0" in your code running on the Pi. This will
ensure that your code will run on the WiFi and non WiFi Pi models. Thats 'SerialZero', not 'SerialOh'.

Hope this helps,
Dave.