POCKETBEAGLE: UART Help - Garbage data along with requisite data

84 views
Skip to first unread message

Piyush Raj ae19m009

unread,
Mar 1, 2021, 9:30:04 PM3/1/21
to BeagleBoard
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<fstream>
#include<iostream>
#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;
}

Graham Stott

unread,
Mar 2, 2021, 1:12:05 AM3/2/21
to beagl...@googlegroups.com

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

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/99530b73-a474-410b-8583-4d0327d32d35n%40googlegroups.com.

Piyush Raj ae19m009

unread,
Mar 2, 2021, 6:05:18 AM3/2/21
to beagl...@googlegroups.com
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

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

You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/DhW9SvsyCPI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/000201d70f2a%24edd15100%24c973f300%24%40comcast.net.

Dennis Lee Bieber

unread,
Mar 2, 2021, 9:19:54 AM3/2/21
to Beagleboard
On Mon, 1 Mar 2021 18:30:04 -0800 (PST), in
gmane.comp.hardware.beagleboard.user Piyush Raj ae19m009
<ae19m009-p/GD3RyrBS6E...@public.gmane.org> wrote:


>
> 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.


--
Dennis L Bieber

Reply all
Reply to author
Forward
0 new messages