I am connected to my Raspberry PI 3 B+ running Raspbian through an SSH terminal, and I am trying to make an application that uses the UART through the GPIO.
Whenever I try to open the port "/dev/serial0" I get in the case where I should not be getting, according to the tutorial I am following 1. I do not know exactly what means the the output of the function open()
, but I am trusting the tutorial :
#include <stdio.h>
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
void setup_uart(){
int uart0_filestream = -1;
uart0_filestream = open("/dev/serial0", O_RDWR | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
}
...
}
void hello_uart(){
//----- TX BYTES -----
unsigned char tx_buffer[20];
unsigned char *p_tx_buffer;
p_tx_buffer = &tx_buffer[0];
*p_tx_buffer++ = 'H';
*p_tx_buffer++ = 'e';
*p_tx_buffer++ = 'l';
*p_tx_buffer++ = 'l';
*p_tx_buffer++ = 'o';
if (uart0_filestream != -1)
{
int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
if (count < 0)
{
printf("UART TX error\n");
}
}
}
int main(){
setup_uart();
hello_uart();
}
I have not used the port ttyS0 before (at least not consciously in the program I am working on), and I suspect that the SSH may be the problem, if it is using the miniUART (in serial0). Moreover, when I try to use the port serial1, I do not it compiles and runs without problems, but I can not test if it is working or not.
How can I investigate what is causing it and, even better, do you have any suggestions for how could I use UART through the GPIO?
Would it be a good idea do try to remap the UART PL011 to the GPIO ports 8 and 10, taking the miniUART out from there, or is there anything more straightforward?