POCKETBEAGLE: UART Help - Garbage data along with requisite data

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;
}

Which Pins are you using for UART4 rcv and have you set the PIN mux correctly for that PIN?

Hi Graham
Thanks for replying
I am using UART 4 , i.e ttyO4 P2.5 and P2.7.
Further, as I understand, ttyO4 is enabled on my device by default since I am able to see it on device tree.

When the string to send is “HIGH”, I am receiving strings such as

IGH
@IGH
H

And so on at each read. I have shorted Rx and Tx so possibility of hardware noise is ruled out. What else can I look at?

Help would be deeply appreciated
Regards

       
       bytes_written = write(file, &transmit,6);

  You are writing 6 bytes even though "HIGH\n" is only 5 characters (and
"LOW\n" is only 4!). That means you have 1 or 2 bytes of "garbage"
(whatever was in memory -- and since it appears the buffers are being
allocated on the stack that could mean anything). I'm also not sure of that
&transmit -- I thought char arrays automatically pass as the address of the
array.

       sleep(1);
       read_code= read(file,receive,10);

  Here you are asking to read 10 bytes, even though you are only writing
4 or 5.

       if(bytes_written>0 && read_code>0 )

  Here you only check that at least 1 byte was written and received...
And a 1 byte receive IS possible (I haven't found any documentation that
indicates how read() handles a serial port that does not have data in it...
It's possible that the outgoing port may still be sending [the
"bytes_written" may only indicate that the kernel buffered that many for
the sending device]).

  I'd probably change the write() to specify strlen(transmit) AND confirm
that bytes_written = strlen(transmit). Similar for the read() operation --
check how many bytes were received.