ttyS0 Serial Communication Error

Asked

Viewed 160 times

2

I’m utilizing terminal serial TTL communication with an Orange Pi One and a PIC16F628A. The program that is on the microcontroller is simple: if it receives the character '1' puts a bit at a high logical level, thus connecting the LED and receiving the character '0' puts a bit at a low level and in both states will give the feedback "Led on n r" and "Led off n r"respectively, and need for future applications. I’ve been using Putty and Minicom terminals for communication, and I got exactly what I wanted. But when I created a program using serial with the C termios API for the Mini PC. The program opens the serial port sends the character '1' and then closes, in super simple question... but for some reason, the LED lights up and after indefinite time (lasting milliseconds or even a few minutes) goes out, happening only in the program I created, and it seems that some process, of the Ubuntu ARM or the API in question is throwing some garbage into the serial. I’ve been trying for a week to figure out what’s going on and fix it.

Functioning properly: Usando o Minicom 2.7 com o Putty SSH e funcionando corretamente Erroneously working: Usando o Minicom 2.7 com o Putty SSH e com tabulação

This so-called tabulation shown in the minicom happened as soon as the program was created and then run the minicom to observe what happened. Every time this tab appears, this problem described above occurs.

I looked for several examples and theory about how it works but the error always remains. This is the link I took the code: Raspberry-Projects . with/pi/Programming-in-c/uart-serial-port/using-the-uart

#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>    //write
#include<string.h> //strcat
#include<fcntl.h> //flags
#include<termios.h>



//Declara variavel
int uart0_filestream = -1;


int main() 
{

//Abrindo a UART
//Flag O_RDWR permite que tenha uma conexao de envio e recebimento de dados
//O_NONBLOCK == O_NDelay modo nonblocking ativo
uart0_filestream = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (uart0_filestream == -1)
{
    printf("Erro: Nao foi possivel abrir a porta serial. Verifique se ha alguma outra aplicacao usando-a.\n");
}

//Atribui flags
struct termios options;
tcgetattr(uart0_filestream, &options);
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; //Atribui o baut rate
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;

tcflush(uart0_filestream, TCIFLUSH);
tcsetattr(uart0_filestream, TCSANOW, &options);


unsigned char tx_buffer[20];
unsigned char *p_tx_buffer;

p_tx_buffer = &tx_buffer[0];
*p_tx_buffer++ = '1';
*p_tx_buffer++ = 10;
*p_tx_buffer++ = 13;

if (uart0_filestream != -1)
{
    printf("%c\n", tx_buffer[0]);
    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");
    }
}

close(uart0_filestream);
return 0; 
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.