ultrasonic bananna
ultrasonic bananna

Reputation: 193

mcu UART, reading serial data from a sensor (rx pin) and displaying (Tx pin) on PC

I am working with the UART on an MCU. Below is a example code of the mcu printing 'hello world' over a serial cable to the PC.

#define BANNER ("Hello, world!\n\n")
int main(void)
{
ti_uart_write_buffer(TI_UART_0, (uint8_t *)BANNER,
                 sizeof(BANNER));
    return 0;
}

This is the ti_uart_write_buffer definition in the header file.

/**
 * Perform a write on the UART interface. This is a blocking
 * synchronous call. The function will block until all data has
 * been transferred.
 *
 * @brief UART multi-byte data write.
 * @param [in] uart UART index.
 * @param [in] data Data to write to UART.
 * @param [in] len Length of data to write to UART.
 * @return ti_rc_t ti_RC_OK on success, error code otherwise.
 */
ti_rc_t ti_uart_write_buffer(const ti_uart_t uart, const uint8_t *const data,
                 uint32_t len);

I'm using a sensor with a serial interface. This is from the datasheet.

Pin 5 Serial Output: The sensor has a TTL outputs. The output is an ASCII capital “R”, followed by four ASCII character digits representing the range in millimeters, followed by a carriage return (ASCII 13).

I'm trying to read in the sesor data on the Rx pin and transmit it to the PC on the Tx pin.

This is the uart read function from the header file that I gather I will need to use.

/**
 * Perform a single character read from the UART interface.
 * This is a blocking synchronous call.
 *
 * @brief UART character data read.
 * @param [in] uart UART index.
 * @param [out] data Data to read from UART.
 * @return qm_uart_status_t Returns UART specific return code.
 */
ti_uart_status_t ti_uart_read(const ti_uart_t uart, uint8_t *data);

My questions are:

  1. I've created a loop in the main() which keeps reading the serial and adding new data to an array until the end marker is reached. Does this make sense?
  2. Given the hello world example above , how could I print out the array?

typedef uint8_t byte;
const byte numChars = 32;
char receivedChars[32]; 
bool newData = false;

void recvWithEndMarker();

int main(void)
{
        do{  
    recvWithEndMarker();
        }
        while (1);
    return 0;
}

void recvWithEndMarker()
{
     static byte ndx = 0;
     char endMarker = '\r';
     char rc;
     while (newData == false) {
        rc = ti_uart_read_non_block(QM_UART_0);  
        if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
                        }
                }
            else {
                receivedChars[ndx] = '\0'; 
                ndx = 0;
                newData = true;
        }
    }
}

Upvotes: 0

Views: 906

Answers (1)

user3629249
user3629249

Reputation: 16540

to answer your question:

Yes, the data read, for each message from the sensor should be saved, byte by byte into an array.

when a complete message is received from the sensor, then call the write function.

Upvotes: 1

Related Questions