Hi, 

I have programmed (in C) UART4 read in thread with blocking read, the code 
as below: I confirmed that the UART receive port does have data through 
oscilloscope, but somehow there is no data in read( ), the return count 
always zero ?  

The strange thing is if minicom is open on the Uart4, then the read( ) in 
receive thread started to run as normal.  However, the regular file write 
become weird.  The string won't be able to be written into file ?! 

Btw, my BBB version is 3.8.13-bone47


#include<stdio.h>
#include<stdlib.h>        // required for fopen, exit()
#include<fcntl.h>
#include<termios.h>
//#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include "readerComm.h"


pthread_t uartRxT , uartTxT;
int file, file2;

int main (int argc, char* argv[])
{
    int count;
 
    pthread_attr_t attr;
    void *status;
    FILE *fdata;


    if(argc != 2)
    {

        printf("Invalid number of arguments, exiting!");
        return -2;

    }

    if((file = open("/dev/ttyO4", O_RDWR | O_NOCTTY | O_NDELAY ))< 0)
    {
        perror("UART: Failed to open the file");
        return -1;
    }

    fcntl(file, F_SETFL, 0);     // blocking read
    struct termios options; //oriopt
    tcgetattr(file, &options);
    options.c_cflag = B9600 | CS8 | CREAD | CLOCAL;
    options.c_iflag = IGNPAR | ICRNL;

    options.c_iflag &= ~IGNBRK;
    options.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

    options.c_cc[VMIN]  = 2;    // Non Canonical input mode (as we didnt 
set ICANON for c_lflag)
    options.c_cc[VTIME] = 10;    // 1 sec timeout


    tcflush(file, TCIFLUSH);

    tcsetattr(file, TCSANOW, &options);


    unsigned char cmd[] = {0x11, 0x00, 0x32};    //"Tx From Beaglebone ";

    if ((count = write(file, (char*)cmd, 3))<0){
          perror("Failed to write to the output\n");
          return -1;
     }else{
         printf("Byte sent: %d \n", count);
     }

    fdata = fopen("capture.dat", "w");
    if (fdata == NULL) {
            printf("I couldn't open capture.dat for writing.\n");
            exit(0);
    }

    // save in file ( cannot print to file ??)
    fprintf(fdata, "%s", "test");
    perror("UART: Test comment");


    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&
attr, PTHREAD_CREATE_JOINABLE);

    // Create UArt Tx thread
    if(pthread_create(&uartTxT, &attr, &uartTxWait, NULL)){
        perror("GPIO: Failed to create the tx thread");

        return -1;
    }

    // Create Uart Rx thread
    if(pthread_create(&uartRxT, &attr, &uartRxWait, NULL)){
        perror("GPIO: Failed to create the rx thread");

        return -1;
    }

    pthread_attr_destroy(&attr);
    pthread_join(uartRxT,&status);
    pthread_join(uartTxT,&status);

    close(file);    // close first file descriptor

    // capture.dat file close
    fclose(fdata);

    return 0;

}

// UART RX thread
void* uartRxWait(void *param){

    unsigned char receive[100];
    int count, n;
    fd_set input;
    struct timeval timeout;

    //initialize input set
    FD_ZERO(&input);
    FD_SET(file, &input);

    timeout.tv_sec=0;
    timeout.tv_usec=0;

    while(1){
       
            if ((count = read(file, (void *)receive, 100))<0)
            {
                perror("Failed to read from input \n");
                printf("count = %d\n", count);
            }else{

                if(receive[2]==0x32){
                    printf("Header received \n"); 
                }
                    printf("Byte received [%d] \n", count);
                    printf("Value of Pi = 0x%.2x\n", receive[0]);

          }
 
    }

    pthread_exit(0);
}


void* uartTxWait(void *param){

    usleep(1000000);
    pthread_exit(0);
}

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beagleboard/5fe05fee-b58d-470e-97b4-79d9c4f21844%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to