Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Basic UART loopback communication issues

Tue Mar 21, 2017 1:45 pm

Hi All,

This is my first post here and am relatively new to the RPi, I am currently doing a college project and i need to be able to communicate between my RPi 3 and a dsPIC30F4011 microcontroller and have been trying all sorts to get this working, currently i am just trying to get the most basic communication going and have followed a code written on by someone else on this site, basically a loopback code to make sure that the RPi is actually working but i seem to be having problems with this!

I have carried out the following:

Code: Select all

sudo nano /boot/config.txt
inserted at the bottom:
enable_uart=1
dtoverlay=pi3-miniuart-bt


I then done:

Code: Select all

sudo nano /boot/cmdline.txt
and deleted:
console=ttyAMA0,115200

Then:

Code: Select all

sudo systemctl disable hciuart
Then ran this code:

Code: Select all

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# serial_port_loopback3.py
# Will also work on Python2.
# Serial port testing on a RaspberryPi 3B with Bluetooth disabled.

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"]

for port in port_list:

    try:
        serialPort = serial.Serial(port, 115200, timeout = 2)
        serialPort.flushOutput()
        serialPort.flushInput() # Syntax may change in new version of python3-serial
        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:
        #except IOError:
        print ("Failed at", port, "\n")

# That's it!
But anytime i run this i get the following:

Failed at /dev/serial0

Failed at /dev/ttyAMA0

Failed at /dev/serial0

Failed at /dev/ttyAMA0


Hardware wise i just have TXD (pin8) and RXD (pin10) shorted out.

Can anyone tell me if i have done anything wrong or if this should be working for me?

Any help would be greatly appreciated.

dgordon42
Posts: 764
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Basic UART loopback communication issues

Tue Mar 21, 2017 4:20 pm

Ennud wrote:and deleted:
console=ttyAMA0,115200
If you found "console=ttyAMA0,115200" in your '/boot/cmdline.txt' file, then you are running an outdated version of Raspbian on your Pi.
Run:

Code: Select all

uname-a
to check which Linux kernel your running.
The current version is:

Code: Select all

Linux RaspberryPi 4.4.50-v7+ #970 SMP Mon Feb 20 19:18:29 GMT 2017 armv7l GNU/Linux
Get yours up to date with:

Code: Select all

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
and follow the prompts, and then reboot.

Now, remove "console=serial0,115200" (it it's there) from '/boot/cmdline.txt'. The "console=tty1" phrase should remain in the line.
Do not split the line when editing, and do no try to add comments to the file.
Ennud wrote:inserted at the bottom:
enable_uart=1
dtoverlay=pi3-miniuart-bt
"dtoverlay=pi3-miniuart-bt" DOES NOT disable Bluetooth, try "dtoverlay=pi3-disable-bt" instead.
There are details of all the 'dtoverlay' commands in the '/boot/overlays/README' file on yout Pi.
You should keep the "enable_uart=1" in there.
You should probably run "sudo systemctl disable hciuart" again after the above changes.

Reboot, and try the loopback test again. Sometimes this code will report a failure when first run after a reboot. If this happens, run it again.

Hope this helps,
Dave.

Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Re: Basic UART loopback communication issues

Tue Mar 21, 2017 7:34 pm

Hi Dave,

Thanks for your reply and you are right my version was outdated which i have now updated but it is still given me the same result? It is definitely pin 8 and pin 10 that i should be shorting out? or do you know anything else that could be causing me these issues? Thanks again for your help.

dgordon42
Posts: 764
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Basic UART loopback communication issues

Tue Mar 21, 2017 9:53 pm

The Pi's Serial Port uses GPIO physical pins 8 & 10 as per this page. Join these two pins together.

The "Failed at ..." error message from that loopback script usually means that the port has not been created.
This is sometimes caused by old or conflicting UART commands in '/boot/config,txt'.
Can you post the output of these three commands:

Code: Select all

ls -lh /dev/serial[0,1]
grep uart /boot/config.txt
grep dtoverlay /boot/config.txt


Dave.

klricks
Posts: 6609
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Basic UART loopback communication issues

Wed Mar 22, 2017 12:27 am

Ennud wrote:Hi Dave,

Thanks for your reply and you are right my version was outdated which i have now updated but it is still given me the same result? It is definitely pin 8 and pin 10 that i should be shorting out? or do you know anything else that could be causing me these issues? Thanks again for your help.
Before attempting to run code I would do a simple loopback test with a serial terminal program such as minicom

Code: Select all

sudo apt-get update
sudo apt-get install minicom
Use Interfacing Options menu in sudo raspi-config to configure serial
Answer <NO> to login shell and <Yes> to configure serial hardware enable.

Connect pin 8 and 10 together with Dupont wire or 'berg' drive jumper.

Run minicom

Code: Select all

minicom -D /dev/serial0
Typed characters should appear on the screen.
(Make sure local echo is off. If local echo is on the you should see double characters: aabbcc......)

To verify RS232 adapter is working then remove the jumper and install TTL to RS232 module to pin 8 &10 & GND.
Short pins 2 and 3 of the 9 pin D connector or serial cable.

Typed characters should be echoed as before.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Re: Basic UART loopback communication issues

Wed Mar 22, 2017 9:44 am

Hi Dave,

Code: Select all

ls -lh /dev/serial[0,1]
returns:

lrwxrwxrwx root root 7 Mar 21 20:25 /dev/serial0 -> ttyAMA0
lrwxrwxrwx root root 5 Mar 21 20:25 /dev/serial1 -> ttyS0

Code: Select all

grep uart /boot/config.txt
returns:

enable_uart=1

Code: Select all

grep dtoverlay /boot/config.txt
returns:

#dtoverlay=lirc-rpi
dtoverlay=pi3-disable-bt

Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Re: Basic UART loopback communication issues

Wed Mar 22, 2017 10:23 am

@klricks

I installed mincom and followed your steps but are you saying once mincom is running from using the following:

Code: Select all

minicom -D /dev/serial0
I just type on the screen and it should echo back? when i have local echo on i receive as you said aabbcc etc. but when its off if i type anything on the screen i receive nothing back?

Am i missing something here? my screen just shows a command window with the following:

Welcome to mincom 2.7

OPTIONS: I18n
Complied on Jan 12 2014, 05:42:53.
Port /dev/serial0, 10:17:20

Press CTRL-A Z for help on special keys


below this i just type random stuff like 'abcd' and hit enter and nothing happens?

Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Re: Basic UART loopback communication issues

Wed Mar 22, 2017 12:05 pm

@klricks

I think i might have been getting confused..

When i have tx and rx shorted the words i type are appearing on the screen and when i take out the short the text doesn't appear on the screen, so im assuming that means my communication is working correctly??

DirkS
Posts: 9966
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Basic UART loopback communication issues

Wed Mar 22, 2017 12:32 pm

Ennud wrote:When i have tx and rx shorted the words i type are appearing on the screen and when i take out the short the text doesn't appear on the screen, so im assuming that means my communication is working correctly??
Yes

klricks
Posts: 6609
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Basic UART loopback communication issues

Wed Mar 22, 2017 1:14 pm

Ennud wrote:@klricks

I think i might have been getting confused..

When i have tx and rx shorted the words i type are appearing on the screen and when i take out the short the text doesn't appear on the screen, so im assuming that means my communication is working correctly??
Sorry I may have confused you with the echo option.
When echo is on then it can lead you to believe serial is working when it is not.

Working serial result:
Echo OFF: abc.....
Echo ON: aabbcc....

Broken serial result:
Echo OFF: <nothing>
Echo ON: abc....
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

dgordon42
Posts: 764
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Basic UART loopback communication issues

Wed Mar 22, 2017 1:15 pm

DirkS wrote:Yes
... and the Python loopback test should now work.

Dave.

Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Re: Basic UART loopback communication issues

Wed Mar 22, 2017 2:06 pm

Dave,

Unfortunately it still doesn't work and i just dont know why? any other ideas?

On a side note both RX and TX pins are constantly HIGH (3.3V) maybe i am wrong but i would of just assumed the RX pin would be LOW??

dgordon42
Posts: 764
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Basic UART loopback communication issues

Wed Mar 22, 2017 3:06 pm

It won't work if either Minicom is running on the port, or if "console=serial0,115200" is still in '/boot/cmdline.txt'.

Run it twice after a reboot, and post the results.

Dave.

Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Re: Basic UART loopback communication issues

Wed Mar 22, 2017 3:24 pm

Dave,

I reboot the Pi and checked that "console=serial0,115200" is still not in the '/boot/cmdline.txt'. and it still fails

I have however managed to get the RPi to communicate with the micro controller through the minicom which was my end goal.

I still have work ahead of me where i need my code written in a python file so i would still like to get this loopback working if possible. Is it possible i am missing a header file like 'import serial' although i would assume i would get an error if this was missing?

dgordon42
Posts: 764
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Basic UART loopback communication issues

Thu Mar 23, 2017 11:43 am

Ennud wrote:Is it possible i am missing a header file like 'import serial' although i would assume i would get an error if this was missing?
If you were missing the Python Serial Module, Python would fail with a "No module names serial" message.

It's possible that the Bluetooth software re-enabled itself when you were updating Raspbian. Run:

Code: Select all

systemctl status hciuart
It should report that the service is loaded but inactive (dead) and disabled.


Dave.

Ennud
Posts: 13
Joined: Tue Mar 21, 2017 1:16 pm
Location: Ireland

Re: Basic UART loopback communication issues

Fri Mar 24, 2017 3:51 pm

Hi Dave,

Thanks for your help its working now i ran the below instruction again and the code now works:

Code: Select all

sudo systemctl disable hciuart
and when i run:

Code: Select all

systemctl status hciuart
it says:

Active: inactive (dead)

Thanks very much for your help, appreciate it.

mungojerry
Posts: 2
Joined: Tue Sep 18, 2018 10:48 pm

Re: Basic UART loopback communication issues

Tue Sep 18, 2018 11:03 pm

the following steps worked for me. i did not need to disable hciuart.

1. Add to the end of

Code: Select all

/boot/config.txt
the following:

Code: Select all

# Enable UART
enable_uart=1
2. Change

Code: Select all

cmdline.txt
by removing

Code: Select all

console=serial0,115200
don't remove anything else. this is what mine is now:

Code: Select all

dwc_otg.lpm_enable=0 console=tty1 root=PARTUUID=37102d70-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
3. reboot, i.e.

Code: Select all

sudo reboot
4. Short pins 8 and 10, this creates a loopback so anything you type in will be displayed. run the terminal emulator screen, install it if you need to:

Code: Select all

sudo screen /dev/serial0 9600
5. connect your serial device, if the previous step works, at 9600 baud and it should work (mine does).

note that

Code: Select all

systemctl status hciuart
reported

Code: Select all

Active: active (running) since Tue 2018-09-18 21:42:43 UTC; 1h 9min ago
unlike the post above, i did nothing with it.

dgordon42
Posts: 764
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Basic UART loopback communication issues

Wed Sep 19, 2018 12:37 pm

@mungojerry:
The method you have posted will work on all models of Pi releases to date running an up to date Raspbian OS by default.
However, @Ennud above was not running his Pi in a default configuration, he was running a Bluetooth equipped model with Bluetooth disabled, hence his need to disable 'hciuart' in order to use his serial port.

Hope this helps,
Dave.

Return to “Troubleshooting”