I am using a TTL serial communication via the terminal with an Orange Pi One and a PIC16F628A. The program that is in the microcontroller is simple: if it receives the character '1' puts a bit in high logic level, thus turning on the LED and receiving the character '0' puts a bit in low level and in both states it will give the feedback "Led lit \ n \ r" and "Led off \ n \ r", respectively, and I need it for future applications. I've been using the Putty and Minicom terminals for communication and I got exactly what I wanted. But when I created a program using the serial with the termios API in C for the Mini PC. The program opens the serial port sends the character '1' and soon closes, in question super simple ... but for some reason, the LED lights up and after undefined time (lasting milliseconds or even a few minutes) goes off, happening only in the program I created, and it seems that some process, Ubuntu ARM or the API in question is throwing some junk for the serial. I've been trying to figure out what's going on and fix it for a week.
Running correctly: Runningerroneously:
ThistabshownintheminicomhappenedassoonastheprogramwascreatedandIthenruntheminicomtoseewhathappened.Wheneverthistabappears,thisproblemoccursasdescribedabove.
Ilookedforseveralexamplesandtheoryonhowitworksbuttheerroralwaysremains.Thisisthelinkthatgotthecode:raspberry-projects.com/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>//Declaravariavelintuart0_filestream=-1;intmain(){//AbrindoaUART//FlagO_RDWRpermitequetenhaumaconexaodeenvioerecebimentodedados//O_NONBLOCK==O_NDelaymodononblockingativouart0_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;
}