RedPitaya: No data in UART - can't read buffer

百般思念 提交于 2020-06-27 06:05:37

问题


I am trying to read out a Sensor on a RedPitaya (running Ubuntu).

The sensor (RM08 rotary encoder by RLS) works with the SSI protocol. I connected the Clock wire to a Digital Output Pin and the Data wire to my Uart RX-PIN.

What I want my program to do now is: After transmitting the clock-signal by my function "clock_signal()" I expect the sensor sending 8 bits of data to my UART via the RX-Pin.

But when reading the RX Buffer with the following Code I only get the message "Again!" which means that it can't read the buffer as the Variable rx_length seems to be <0.

Hope you can help me with this problem - thanks in advance!

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h> //Used for UART
#include <unistd.h> //Used for UART
#include <fcntl.h>  //Used for UART
#include "redpitaya/rp.h"

static int release();
static int uart_read();
static int clock_signal();

int uart_fd = -1;

#define bitsToRead 8

static int set_interface_attribs(int uart_fd, int speed)
{
    struct termios tty;

    if (tcgetattr(uart_fd, &tty) < 0) {
        printf("Error from tcgetattr: %s\n", strerror(errno));
        return -1;
    }

    cfsetospeed(&tty, (speed_t)speed);
    cfsetispeed(&tty, (speed_t)speed);

    tty.c_cflag |= (CLOCAL | CREAD);    /* ignore modem controls */
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;         /* 8-bit characters */
    tty.c_cflag &= ~PARENB;     /* no parity bit */
    tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
    tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */

    /* setup for non-canonical mode */
    tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
    tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    tty.c_oflag &= ~OPOST;

    /* fetch bytes as they become available */
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 1;

    if (tcsetattr(uart_fd, TCSANOW, &tty) != 0) {
        printf("Error from tcsetattr: %s\n", strerror(errno));
        return -1;
    }
    return 0;
}

void set_mincount(int uart_fd, int mcount)
{
    struct termios tty;

    if (tcgetattr(uart_fd, &tty) < 0) {
        printf("Error tcgetattr: %s\n", strerror(errno));
        return;
    }

    tty.c_cc[VMIN] = mcount ? 1 : 0;
    tty.c_cc[VTIME] = 5;        /* half second timer */

    if (tcsetattr(uart_fd, TCSANOW, &tty) < 0)
        printf("Error tcsetattr: %s\n", strerror(errno));
}

static int clock_signal()
{

    // Initialization of API
    if (rp_Init() != RP_OK) 
    {
        fprintf(stderr, "Red Pitaya API init failed!\n");
        return EXIT_FAILURE;
    }
    printf("clock started \n");

    //Set PIN DIO1_N to OUTPUT
    rp_DpinSetDirection(RP_DIO1_N, RP_OUT);


    //The first high/low transition (point 1) stores the current position data in a parallel/serial
converter and the monoflop is triggered

    rp_DpinSetState(RP_DIO1_N,1);
    usleep(50);
    rp_DpinSetState(RP_DIO1_N,0);
    usleep(0.2);

    for(int i=1; i<=bitsToRead; i++)
    {
        rp_DpinSetState(RP_DIO1_N,1); //Setzt Digitalen Pin1 auf High
        usleep(0.2);                    
        rp_DpinGetState(RP_DIO1_N, &state1);
        printf("%i DIO1 high %u \n", i,state1);     //Checkt ob State geaendert

        rp_DpinSetState(RP_DIO1_N,0);  //Setzt DPin1 auf LOW
        usleep(0.2);
        rp_DpinGetState(RP_DIO1_N, &state1);
        printf("%i DIO1 low %u \n", i, state1);

        //Get BIT
        //rp_DpinGetState(RP_DIO2_N, &state2);
        //printf("BIT %i: %u \n", i, state2);
        //Get AI1-Value 

    }

    //3 auf 4: Transmit data to UART
    rp_DpinSetState(RP_DIO1_N,1); //Setzt Digitalen Pin1 auf High
    usleep(15);                   //Delay for Controller to read positional data (Datasheet), 15microsec
    printf("Data sent to UART \n"); 


    return 0;
}



static int uart_read() //can't read
{

    // Don't block serial read 
    fcntl(uart_fd, F_SETFL, FNDELAY);


    if(uart_fd == -1){
        fprintf(stderr, "Failed to read from UART.\n");
        return -1;
    }

    unsigned char rx_buffer[bitsToRead];

    int rx_length = read(uart_fd, (void*)rx_buffer, bitsToRead);

    if (rx_length < 0){

        // No data yet avaliable, check again 
        if(errno == EAGAIN){
            fprintf(stderr, "AGAIN!\n");
        // Error differs 
        }else{
            fprintf(stderr, "Error!\n");
            return -1;
        }

    }else if (rx_length == 0){
        fprintf(stderr, "No data waiting\n");
    // Print data and exit while loop 
    }else{
        rx_buffer[rx_length] = '\0';
        printf("%i bytes read : %s\n", rx_length, rx_buffer);

    }

    return 0;
}



static int release(){

    tcflush(uart_fd, TCIFLUSH);
    close(uart_fd);

    return 0;
}



int main()
{
    char *portname = "/dev/ttyPS1";

    uart_fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (uart_fd < 0) {
        printf("Error opening %s: %s\n", portname, strerror(errno));
        return -1;
    }
    /*baudrate 115200, 8 bits, no parity, 1 stop bit */
    set_interface_attribs(uart_fd, B115200);

    //set_mincount(uart_fd, 0);                /* set to pure timed read */

    /*Flush Buffer  
    sleep(2); //required to make flush work, for some reason
    tcflush(uart_fd,TCIOFLUSH);
    */

    // CLOCK
    clock_signal();


    // Sample read 
    if(uart_read() < 0)
    {
        printf("Uart read error\n");
        return -1;
    }


    /* CLOSING UART */
    release();

    return 0;
}


来源:https://stackoverflow.com/questions/58916573/redpitaya-no-data-in-uart-cant-read-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!