Facing issue in UART Communication on Raspberry Pi

Hello

I am using Raspberry Pi and I have downloaded the Raspberry Pi Host OS from BalenaOS download page.

Raspberry Pi Version =>
BalenaOS 2.36.0+rev2

I have to set up communication between External hardware with Raspberry Pi using the UART interface.

For that I have done the following settings in config.txt =>

enable_uart=1
core_freq=250
dtoverlay=pi3-miniuart-bt
dtoverlay=uart-ctsrts

using Balena Cli env add command

I am using pi3-miniuart-bt for switching the Port for swapping ttyAMA0 and ttyS0

uart-ctsrts are used to enable Hardware flow control.

I have copied miniuart-ctsrts.dtbo and uart-ctsrts.dtbo in the /mnt/boot/overlays directory.

I have enabled the UART and wrote a sample code for send and receiving the bytes over receiving.

Send function works properly But receiving function blocks after 2 to 3 seconds.

#!/usr/bin/env python
import time
import serial


ser = serial.Serial(
    
    port='/dev/serial0',
    baudrate = 9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)
counter=0


while 1:
    print "I am reading"
    x=ser.readline()
    print "I am after reading"
    print(x)


#!/usr/bin/env python
import time
import serial


ser = serial.Serial(
    
    port='/dev/serial0',
    baudrate = 9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=1
)
counter=0


while 1:
    print "I am here"
    ser.write('Write counter: %d \n'%(counter))
    time.sleep(10)
    counter += 1

Hi @Sharvin26,

Thank you for sharing so much context. Could you try out the steps explained in this thread?

I see that this user had similar issues and following these steps helped him.

If that doesn’t work, could you as well try using a production image?

Please let us know how it goes.

@gelbal
Thanks for the Solution.
It’s working now but now I want to enable the Hardware flow control ( CTS/RTS ) pins for UART communication. How can I achieve this?

Note

Hi,

You should add that via environment variables (RESIN_HOST_CONFIG_dtoverlay I think) in the balenaCloud dashboard so that the supervisor can write that file.
Otherwise the supervisor checks config.txt and things there is an extra parameter that it removes. Because it isn’t aware of the existence of this one.

Regards
ZubairLK

I am using open balena and I have added the RESIN_HOST_CONFIG_dtoverlay using the command balena add env command.

dtoverlay=pi3-miniuart-bt
dtoverlay_1=uart-ctsrts

One thing I doubt that is affecting is dtoverlay_1 naming ( please correct me if I am wrong ) It should be dtoverlay for uart-ctsrts also.

But I had to add dtoverlay_1 via Environment variable for uart-ctsrts as it was not taking dtoverlay as it was already assigned to pi3-miniuart-bt.

Hello @sharvin26 you need to use RESIN_HOST_CONFIG_dtoverlay and set it to "pi3-miniuart-bt","uart-ctsrts" (including the double quotes), see https://www.balena.io/docs/reference/OS/advanced/#setting-device-tree-overlays-dtoverlay-and-parameters-dtparam

Hello,

I have configured as you mentioned in the above thread.

➜ balena env add BALENA_HOST_CONFIG_dtoverlay "pi3-miniuart-bt","uart-ctsrts" -a MyApp

After that, I rebooted the raspberry pi.

root@a2ae423:/mnt/boot# cat config.txt
core_freq=250
dtoverlay=pi3-miniuart-bt,uart-ctsrts
enable_uart=1
dtoverlay_1=uart-ctsrts
disable_splash=1
dtparam=i2c_arm=on
dtparam=spi=on
dtparam=audio=on
gpu_mem=16
➜ balena envs -a MyApp --config
ID NAME                           VALUE
13 BALENA_HOST_CONFIG_core_freq   250
10 BALENA_HOST_CONFIG_dtoverlay   pi3-miniuart-bt,uart-ctsrts
9  BALENA_HOST_CONFIG_dtoverlay_2 i2c-rtc,ds1307
14 BALENA_HOST_CONFIG_enable_uart 1
3  BALENA_SUPERVISOR_LOCAL_MODE   1
4  RESIN_HOST_CONFIG_dtoverlay    pi3-miniuart-bt,uart-ctsrts
7  RESIN_HOST_CONFIG_dtoverlay_1  uart-ctsrts

But I am still unable to enable the Hardware flow control. I also tried to remove this
dtoverlay_1=uart-ctsrts using command balena env rm 7 --yes It didn’t give any error but it didn’t remove.

Note: I am using OpenBalena.

Can you please start fresh on a new application and set the vars again? Might worth trying on balenaCloud as well in order to use the Dashboard.

For that I need to remove the device from existing application and reregister to new application right ( Right now I would like to test it on openbalena. )

Can you share the commands for this purpose?

Is the device present locally with you? You can follow the Getting Started guide from here: https://www.balena.io/docs/learn/getting-started/raspberrypi3/nodejs/

yes, the device is present locally. The doc you shared is for BalenaCloud. Can you share a command line option?

Please use balenaCloud and the Dashboard to rule out a certain set of potential issues. (balenaCloud is free, but does require signup, obviously)

Actually right now I don’t want to move my device and application to BalenaCloud ( May be in the future ) I want to test it using OpenBalena. Can you please give a solution for this issue in terms of OpenBalena?

Thanks

OK. The Getting Started guide of openBalena explains how to create an app, push code and provision a device in detail.

Yes, I have configured the device using OpenBalena and for that, I have followed that guide.

My Question is How can I achieve this with OpenBalena.

Are there sets of command available for this purpose?

Thanks

I think my last comment above answers your question perfectly. The Getting Started guide has detailed instructions on how to create a new app and add a device. Which part is not clear and you need help with?