This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

read a packet with UART_read



Hello,

I download TI-RTOS and I compile H52C1 - Example Projects - UART Echo.

I open "uartecho.c" file and I found this snippet

    UART_write(uart, echoPrompt, sizeof(echoPrompt));

    /* Loop forever echoing */
    while (TRUE) {
        UART_read(uart, &input, 1);
        UART_write(uart, &input, 1);
    }

ok: the code wait for only one received UART char and echoes,

but I have this problem: I want to listen for an incoming packet,

decode and answer.

The incoming packet is a sequence of multiple chars without any pause.

The code has to detect the end of the packet and return the packet (chars)

I suspect You have to use one timer like this

#define TIMEOUT 10 //ms

Char incoming_packet[100];
Char* ptr = incoming_packet;

do
{
UART_read(uart, ptr++, 1);
TIMER_reset();
} while (TIMER_get_ms()<TIMEOUT);

int packet_size = (int)(ptr-incoming_packet);

Is there a better way ?

Can someone help me ?

By by

  • Mauro,

    which version of TI-RTOS are you using?

    You could tell UART_read to read in more than just byte. When you call UART_open(), one of the parameters is a read timeout. If the timeout occurs, it should still return the number of bytes that were received up until the timeout. You could use it like this:

    bytesRead = UART_read(uart, buffer, sizeof(buffer));
    processData(buffer, bytesRead);

  • I'm using 1.10.0.23,

    If I use readTimeout is in usec, msec, or systick ?

  • I try to use timeout but my problem is that I dont' know when the packet comes to the dsp because this is the slave, the master is a PC.

  • ok, I resolve the problem like this

    UInt8 req_buf[20];
    UInt8 answ_buf[100];
    
    Void modbusFxn(UArg arg0, UArg arg1)
    {
        UART_Handle uart;
        UART_Params uartParams;
    
        //create UART
        UART_Params_init(&uartParams);
        uartParams.writeDataMode  = UART_DATA_BINARY;
        uartParams.readDataMode   = UART_DATA_BINARY;
        uartParams.readReturnMode = UART_RETURN_FULL;
        uartParams.readEcho       = UART_ECHO_OFF;
        uartParams.readTimeout    = 10;//ms ?
        uartParams.baudRate       = 57600;
        uart = UART_open(Board_UART, &uartParams);
    
        while(TRUE)
        {
        	//receive
        	Int pck_len=UART_read(uart, (Char*)req_buf, sizeof(req_buf));
    
        	//create buffers
        	BUFFER request = {req_buf,sizeof(req_buf),pck_len,0};
    	    BUFFER answer = {answ_buf,sizeof(answ_buf),0,0};
    
        	//decode
        	modbus_decode(0x13, IN &request, OUT &answer);
    
        	//answer
    		UART_write(uart, (Char*)answer.buffer, answer.w_ptr);
        }
    }

    if no data UART receive "pck_len" is equal to 0 and modbus_decode function return no answer