Ragnos
Posts: 5
Joined: Sat Sep 10, 2016 6:29 am

RPi 3 UART for RS232

Sat Sep 10, 2016 2:38 pm

Dear community,

I just got my RPi and after some time toying around I started a project. But I encountered some problems with the useage of the UART at the RPi 3.

Because of this problems and the fact that I found different methods on the web depending on which packages are used I used apt-get update + dist-upgrade. If i now type "uname -a" I get the following output:
Linux raspberrypi 4.4.13v7+ #894 SMP Mon Jun 13 ...

What I want to do is communicate with a device over RS232 protocol at a baud rate of 9600.

As I understood this there are 2 UARTs on the RPi 3. One connected to bluetooth and a different one (mini UART).
There are overlays to switch the UART which is connected to bluetooth back to the pins 14 and 15.

But do I really need to do this if I use a baud rate of only 9600?
If not is the mini UART the device ttyAMA0 or the other UART?
Does anybody know an example program + setup for RPi 3 with the current version or some kind of To-Do list for working with the UART?

I tried different configurations with the program from http://www.raspberry-projects.com/pi/pr ... g-the-uart but could not get any output at the Pins 14 and 15. I looked at the pins with a logic analyzer.

As I said I'm new to the RPi and if i missed something important please tell me.

Best Regards,
Ragnos

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

Re: RPi 3 UART for RS232

Sat Sep 10, 2016 5:02 pm

To use the serial port on the Pi, add:

Code: Select all

enable_uart=1
to the end of the '/boot/config.txt' file. (Ensure that the '/boot/config.txt' file does not contain the line "enable_uart=0", if it does, fix it)

If you do not want a log-in on the serial port (unlikely, unless you are connecting a Terminal), remove the phrase:

Code: Select all

console=serial0,115200
from the file '/boot/cmdline.txt'. This file must contain only 1 line, so don't split the line up when editing, and don't try to add any comments.

Reboot after the above changes.
Refer to the serial port as "/dev/serial0" in code. That's serialZero, not serialOh!
Ragnos wrote:Because of this problems and the fact that I found different methods on the web depending on which packages are used I used apt-get update + dist-upgrade. If i now type "uname -a" I get the following output:
Linux raspberrypi 4.4.13v7+ #894 SMP Mon Jun 13 ...
Good move, there have been lots of changes made to the serial port software in Raspbian since the Pi 3B's launch, and you need to stay up to date to avoid problems.
Ragnos wrote:As I understood this there are 2 UARTs on the RPi 3. One connected to bluetooth and a different one (mini UART).
There are overlays to switch the UART which is connected to bluetooth back to the pins 14 and 15.

But do I really need to do this if I use a baud rate of only 9600?
If not is the mini UART the device ttyAMA0 or the other UART?
Yes, there are 2 UART's on all model Pi's, some details here.
The trick these days is to refer to your serial port as "/dev/serial0". On any model Pi running up-to-date Raspbian, the OS will take care of which UART is connected to which device or port, and everything should 'just work' without the need for overlays.
Ragnos wrote:I tried different configurations with the program from http://www.raspberry-projects.com/pi/pr ... g-the-uart but could not get any output at the Pins 14 and 15. I looked at the pins with a logic analyzer.
First, follow the procedure at the top of the post to get your serial port active, then change every occurrence of "/dev/ttyAMA0" to "/dev/serial0" in the code you referenced, and it should work. (It looks like it will work, I have not tested it)

Finally, never forget that the Pi's GPIO pins, including the serial port pins 8 & 10, work at 3.3Volts. They are not 5V tolerant, so connecting them directly to a 5V, TTL or RS232 serial port will destroy your Pi.

Hope this helps,
Dave.

Ragnos
Posts: 5
Joined: Sat Sep 10, 2016 6:29 am

Re: RPi 3 UART for RS232

Sun Sep 11, 2016 1:36 pm

Thanks for the answer. This explained a lot to me. I'm glad that the other device also works with 3.3V logic levels, so I don't need any intermediate circuitry.

But I still have a problem when I try to open the UART filestream.

I did the following:
  1. Added "enable_uart = 1" to the config.txt (with sudo nano /boot/config.txt)
  • Removed the phrase: "console = serial0, 115200" fron file "/boot/cmdline.txt
  • sudo reboot
  • Then I tried the following program:
To be on the safe side I also tried to execute the program with "sudo ./uart" and started geany with "gksudo geany". But I always get the message "Error - Unable to open UART. Ensure it is not in use by another application" which means the file stream was not opened. I also tried the full example program from http://www.raspberry-projects.com/pi/pr ... g-the-uart, because I thought that maybe it would work anyway. But I can not detect any changes on the TxD pin (pin 8 on header, 14 BCM GPIO) with my logic analyzer. Logic Analyzer works fine if I use the wiringPi library to toggle the pin.

Code: Select all

#include <stdio.h>
#include <unistd.h>			
#include <fcntl.h>			
#include <termios.h>		

int main(int argc, char **argv)
{
    int uart0_filestream = -1;
    uart0_filestream = open("/dev/serial0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (uart0_filestream == -1)
        {
	    printf("Error - Unable to open UART.  Ensure it is not in use by another application\n");
	}
    close(uart0_filestream);
    return 0;
}
Did I do something wrong in the open command, or maybe something is still accessing the UART. But serial0 refers to the mini UART on the RPi 3, so what could be accessing it?

EDIT: If I look at the pin with the Logic Analyzer after booting the level of the pin is mostly high. But there are many slots of always 41ns low times. The output of the Logic analyzer is shown below. But these are no UART transmissions as I would know them. Maybe this has something to do with my problem. If I run my GPIO.c test program which toggles the pin low--> high --> low, it stays low constantly, even after the program has finished. That is why I didn't recognized this behaviour right from the beginning.

Image

Best regards,
Ragnos

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

Re: RPi 3 UART for RS232

Sun Sep 11, 2016 2:03 pm

Can you verify that the UART's have been created correctly by Raspbian by posting the results of:

Code: Select all

ls -lh /dev/ttyS0
ls -lh /dev/ttyAMA0
ls -lh /dev/serial0
ls -lh /dev/serial1
Dave.

Ragnos
Posts: 5
Joined: Sat Sep 10, 2016 6:29 am

Re: RPi 3 UART for RS232

Sun Sep 11, 2016 2:09 pm

Thanks for the quick reply. These are the results:

[email protected]:~ $ ls -lh /dev/ttyS0
ls: cannot access /dev/ttyS0: No such file or directory
[email protected]:~ $ ls -lh /dev/ttyAMA0
crw-rw---- 1 root dialout 204, 64 Sep 11 13:56 /dev/ttyAMA0
[email protected]:~ $ ls -lh /dev/serial0
ls: cannot access /dev/serial0: No such file or directory
[email protected]:~ $ ls -lh /dev/serial1
lrwxrwxrwx 1 root root 7 Sep 11 13:56 /dev/serial1 -> ttyAMA0

Looks like the mini UART has not been created. Is there a way to fix this?

EDIT: Somebody had a similar problem because he was missing the /etc/udev/rules.d/99-com.rules which creates the serial0 and serial1 as I understand this. Anyway I have the 99-com.rules.

Best Regards,
Ragnos

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

Re: RPi 3 UART for RS232

Sun Sep 11, 2016 2:44 pm

Rangos wrote:Looks like the mini UART has not been created.
I suspected as much.
Have you used the 'raspi-config' utility, either in the GUI or through the command line, at any time to disable the login on the serial port?

Things to do to get your Serial Port back:
Check that the serial port is enabled (and/or the login is enabled) in 'raspi-config', either in the GUI, or at the command line.
Verify that the '/boot/config.txt' file only contains the line "enable_uart=1" with the:

Code: Select all

grep uart /boot/config.txt
command. If there are any other non-commented lines containing "uart", comment them out, and report them here.
The above steps will probably put "console=serial0,115200" back into '/boot/cmdline.txt' Check, and remove if necessary.

Reboot

Check the status of "/dev/ttyS0" and /dev/serial0" again with the 'ls -lh' commands. "/dev/ttyS0" should now exist, and "/dev/serial0" should point to it. If it does, try your serial port code again.

Hope this helps,
Dave.

Ragnos
Posts: 5
Joined: Sat Sep 10, 2016 6:29 am

Re: RPi 3 UART for RS232

Sun Sep 11, 2016 3:03 pm

Thanks!!! You safed my day! It works now!

Yes, I used it (rasp-config), but I reinstalled raspbian (with noobs) in the meantime. I thought, that this would revert this change. At least I can't remember doing it after the new installation, because I started this thread and wanted everything back to standard.

After I activated "Serial" in rasp-config I found again a line saying "enable_uart = 0" right above my "enable_uart=1" at the end of config.txt. I commented this line out.

I also deleted "console=serial0,115200" again from cmdline.txt

After this everything worked as intended.

Thanks again.

Best Regards,
Ragnos

robertg
Posts: 3
Joined: Wed Apr 04, 2018 5:39 am

Re: RPi 3 UART for RS232

Tue May 01, 2018 4:47 am

Just to confirm, also trying to get serial comms working with both Bluetooth and serial port via GPIO 14/15. Reading mixed things, some say you can't and reading above you can.
Have tried various combo's of the above but can't seem to get both ports working concurrently.
Any insight would be great, just running around in circles getting nowhere. :?

Thanks,

Rob.

Ragnos
Posts: 5
Joined: Sat Sep 10, 2016 6:29 am

Re: RPi 3 UART for RS232

Tue May 01, 2018 8:40 am

Hi,

I don't know if they changed anything in the software since then. But back then you had to switch one of the two off via the config file. Therefore you could only use one of the two at a time.

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

Re: RPi 3 UART for RS232

Tue May 01, 2018 1:19 pm

There is no problem using both Bluetooth and the Serial Port simultaneously on the model 3, 3+, or Zero W RaspberryPi.

Ensure that you do not have the phrase "console=serial0,115200" anywhere in your '/boot/cmdline.txt' file.
Ensure that you do have the line "enable_uart=1" in your '/boot/config.txt' file, and that there are no other active lines in there with uart commands in them.
Reboot if you have made any changes.
Refer to your Serial Port as "/dev/serial0" in code, thats serialZero, not serialOh.
These instructions work on all models running up to date Raspbian.

Hope this helps,
Dave.

Return to “Troubleshooting”