I am implementing UART using Pocketbeagle wherein i am trying to send simple strings over UART . I have written a code for loopback testing of UART-4 in PB. for a logic Low on P2.1 i am sending String “LOW” and “HIGH” for vice versa. However i am getting garbage data along with the requisite strings. sometimes all the received data is missing or broken. I tried to work at various baud rates (600, 9600,115200) but of no help.
Am i doing any basic mistake. need guidance from community. (I worked with AVR and Arduino UART in the past and never faced such problem)
Further, i also tried to dump the received data in a text file( to rule out possibility of terminal baud rate mismatch), but results did not change.
My code is below:
#include
#include
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include<unistd.h> //for usleep
#include"GPIO.h"
using namespace exploringBB;
using namespace std;
int main()
{
GPIO outLogicSupply(60); outLogicSupply.setDirection(OUTPUT);
GPIO inLogicSupply(59); inLogicSupply.setDirection(INPUT);
int file,count;
char transmit[6];
char receive[10];
int bytes_written,read_code;int count1=1;
//////////////// OPENING UART4 ON POCKETBEAGLE /////////////////////
printf(“UART: Transmission Started.\n”);
if ((file = open("/dev/ttyO4", O_RDWR | O_NOCTTY | O_NDELAY))<0)
{
perror(“UART: Failed to open the device.\n”);
return -1;
}
else
{
printf(“UART: Opened file successfully\n”);
}
/////////////////////////////////////////////////////////////////////
/////////////// SETTING UP UART OPTIONS ///////////////////////////
struct termios options;
tcgetattr(file, &options);
options.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
options.c_iflag = IGNPAR | ICRNL;
tcflush(file, TCIFLUSH);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make reads non-blocking
tcsetattr(file, TCSANOW, &options);
cout << "The value of the input is: "<< inLogicSupply.getValue() << endl;
while(1)
{
if(inLogicSupply.getValue()==1)
{
strcpy(transmit,“HIGH\n”);
outLogicSupply.setValue(HIGH);
}
else
{
strcpy(transmit,“LOW\n”);
outLogicSupply.setValue(LOW);
}
bytes_written = write(file, &transmit,6);
sleep(1);
read_code= read(file,receive,10);
if(bytes_written>0 && read_code>0 )
{
receiveFile<<count1<<"sec - "<<receive;
cout<<count1<<"sec - "<<receive;
}
count1++;
sleep(0.01);
}
///////////////////////////////////////////////////////////////
receiveFile.close();
close(file);
return 0;
}