matteochini
Posts: 6
Joined: Tue Apr 10, 2018 8:48 pm

Need help using serial port - RPi 3 Model B

Tue Apr 10, 2018 9:28 pm

Hello,

I am struggling for a whole week trying to use the serial port. I have read many tutorial without outcomes.

I have a Raspberry 3 Model B with the mos recent version of the software. The output of what version of the kernel is:

Code: Select all

uname -r ---> 4.14.30-v7+
.

I need to connect a PM air quality monitor (Plantower 5003) but in general the issues are more related to the connection of the serial port. Indeed the wrong behavior is the same even the pin RX and TX are connected directly.

Using raspi-config I have enabled the serial and disabled the access using the console.
I don't need the Bluetooth so it was create an alias like this /dev/serial0 --->/dev/ttyAMA0. For clarity in /boot/config.txt there is

Code: Select all

enable_uart=1
. Indeed:

Code: Select all

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
#disable_overscan=1

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800

# Uncomment some or all of these to enable the optional hardware interfaces
dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on

# Uncomment this to enable the lirc-rpi module
#dtoverlay=lirc-rpi

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

dtoverlay=pi3-disable-bt
enable_uart=1



And even in /boot/cmdline.txt I remove the use of the console but at same time I am not totally sure of these code:

Code: Select all

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
If somebody can help me, I post some output with the cpde showed below:

Code: Select all

[email protected]:~ $ ls -lh /dev/serial*
lrwxrwxrwx 1 root root 7 apr 10 21:04 /dev/serial0 -> ttyAMA0
lrwxrwxrwx 1 root root 5 apr 10 21:04 /dev/serial1 -> ttyS0
[email protected]:~ $ sudo hexdump -C < /dev/serial0
bash: /dev/serial0: Permesso negato
[email protected]:~ $ ls -l /dev/ttyS0
crw-rw---- 1 root dialout 4, 64 apr 10 21:04 /dev/ttyS0
[email protected]:~ $ ls -l /dev/ttyAMA0
crw--w---- 1 root tty 204, 64 apr 10 23:10 /dev/ttyAMA0

The problem seems related to the possibility of read. Infact using these simple code I got a null value related to the value loopback.

Code: Select all

import serial

test_string = "1234".encode('utf-8')

    try:
        serialPort = serial.Serial('/dev/port/serial0', 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")
        
Similar when I cycle over the 4 ports using

Code: Select all

 port_list = ["/dev/serial0", "/dev/serial1", "/dev/ttyAMA0", "/dev/ttyS0"]
, I got
"Received incorrect data" for serial0, ttyAMA0 and connection failed for serial1, ttyS0.


Does any of you have any idea how to solve?

Thank you


**** edit: I made a mistake because I have RPi 3 Model B not Model B+, sorry guys***
Last edited by matteochini on Wed Apr 11, 2018 1:42 pm, edited 1 time in total.

rkn704
Posts: 38
Joined: Sun Dec 30, 2012 4:25 pm

Re: Need help using serial port - RPi 3 Model B +

Wed Apr 11, 2018 1:35 pm

Just tried your loopback test with a RS232 to USB converter and it worked fine (after sorting the indent for the try/except)

If you use a RS232 to USB converter the port needs to be "/dev/ttyUSB0" or similar appropriate for the USB port you are using.

Are you sure you do not have a hardware problem? Have you put the loop back link on the RS232 port or have you connected the RX to TX on the 40 way I/O connector?

When you connect the 40 way I/O pins to the RS232 module are you sure you have the RX and TX around the right way. I don't have a module here with me to try at the moment but I think when I tried one it was RX to RX and TX to TX between the 40 way I/O pins and the RS232 module.

If you are putting a link on the RS232 9 pin connector for loopback are you sure you are linking 2 and 3 together and not 3 and 4 by mistake.

matteochini
Posts: 6
Joined: Tue Apr 10, 2018 8:48 pm

Re: Need help using serial port - RPi 3 Model B

Wed Apr 11, 2018 1:49 pm

Hi rkn704! Thank you for your reply.

The first time, as you can see from the edited title of the thread, I have a model B not model B+. I don't why I placed a +.

At this moment I don't have a RS232 to USB converter. The only thing that I tried is to connect directly the pins.

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

Re: Need help using serial port - RPi 3 Model B

Wed Apr 11, 2018 3:14 pm

@matteochini:
You have used the DT Overlay "dtoverlay=pi3-disable-bt" in your '/boot/config.txt' file.
When you use this overlay, you also need to run the commands:

Code: Select all

sudo systemctl stop hciuart
sudo systemctl disable hciuart
These commands prevent SystemD from attempting to use the UART to talk to the Bluetooth chip, which can cause the type of errors you are seeing.
You only need to run these commands once.

Hope this helps,
Dave.

matteochini
Posts: 6
Joined: Tue Apr 10, 2018 8:48 pm

Re: Need help using serial port - RPi 3 Model B

Wed Apr 11, 2018 4:44 pm

Thank you dgordon42! :)

In past I have already used these commands but I will try another time because maybe I forgot something.
Should these commands be launched at each start or just once?

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

Re: Need help using serial port - RPi 3 Model B

Wed Apr 11, 2018 5:27 pm

The commands only need to be run once.
You can check the status of the 'hciuart' daemon with:

Code: Select all

systemctl status hciuart
It should report "disabled" if the commands ran successfully, and should be remembered over a reboot or power cycle.

Dave.

matteochini
Posts: 6
Joined: Tue Apr 10, 2018 8:48 pm

Re: Need help using serial port - RPi 3 Model B

Fri Apr 13, 2018 11:42 am

Hi Dave,

the output of the status is

Code: Select all

[email protected]:~ $ sudo systemctl status hciuart
● hciuart.service - Configure Bluetooth Modems connected by UART
   Loaded: loaded (/lib/systemd/system/hciuart.service; disabled; vendor preset:
   Active: inactive (dead)
It seems disabled but the problem of serial is the same. Can you confirm that is disable?
I am not sure that the serial port configured in /boot/cmdline.txt is right, can I ask you to check?

Thank you

matteochini
Posts: 6
Joined: Tue Apr 10, 2018 8:48 pm

Re: Need help using serial port - RPi 3 Model B

Fri Apr 13, 2018 12:03 pm

I am quite sure that hciuart is disabled as you can see from:

Code: Select all

[email protected]:~ $ sudo systemctl is-enabled hciuart
disabled

matteochini
Posts: 6
Joined: Tue Apr 10, 2018 8:48 pm

Re: Need help using serial port - RPi 3 Model B

Fri Apr 13, 2018 3:28 pm

I prefer to add some information not only related to my problem but maybe help someone in the future.

Using PySerial package i have:

Code: Select all

[email protected]:~ $ python -m serial.tools.list_ports -v
/dev/ttyAMA0        
    desc: ttyAMA0
    hwid: 3f201000.serial
1 ports found
Theoretically /dev/ttyAMA0 is /dev/serial0, from the output of

Code: Select all

ls -l /dev

And I found this:

Code: Select all

[email protected]:~ $ ps aux | grep serial0
root       531  0.0  0.1   3972  1824 ?        Ss+  16:59   0:00 /sbin/agetty --keep-baud 115200,38400,9600 serial0 vt220
pi        1172  0.0  0.0   4372   564 pts/2    S+   17:29   0:00 grep --color=auto serial0
[email protected]:~ $ ps aux | grep /dev/serial0
pi        1175  0.0  0.0   4372   532 pts/2    S+   17:29   0:00 grep --color=auto /dev/serial0
[email protected]:~ $ ps aux | grep ttyAMA0
root      1032  0.0  0.3   7300  3452 ?        S    17:13   0:00 sudo python -m serial.tools.miniterm /dev/ttyAMA0
root      1036 92.6  0.7  29284  7328 ?        Sl   17:13  14:58 python -m serial.tools.miniterm /dev/ttyAMA0
root      1054  0.0  0.3   7300  3308 ?        S    17:15   0:00 sudo python -m serial.tools.miniterm /dev/ttyAMA0
root      1058 97.9  0.7  29284  7392 ?        Sl   17:15  14:35 python -m serial.tools.miniterm /dev/ttyAMA0
pi        1177  0.0  0.0   4372   548 pts/2    S+   17:30   0:00 grep --color=auto ttyAMA0
[email protected]:~ $ ps aux | grep /dev/ttyAMA0
root      1032  0.0  0.3   7300  3452 ?        S    17:13   0:00 sudo python -m serial.tools.miniterm /dev/ttyAMA0
root      1036 92.7  0.7  29284  7328 ?        Sl   17:13  15:16 python -m serial.tools.miniterm /dev/ttyAMA0
root      1054  0.0  0.3   7300  3308 ?        S    17:15   0:00 sudo python -m serial.tools.miniterm /dev/ttyAMA0
root      1058 97.9  0.7  29284  7392 ?        Sl   17:15  14:54 python -m serial.tools.miniterm /dev/ttyAMA0
pi        1179  0.0  0.0   4372   572 pts/2    S+   17:30   0:00 grep --color=auto /dev/ttyAMA0

Return to “Python”