11 months, 3 weeks ago.

UART communication in FRDM k64F

Hi, I tried sending values through uart.putc() command using he below code:

  1. include "mbed.h"
  2. include "DHT.h"

DHT sensor(PTC16, AM2302); Serial pc(USBTX, USBRX); Serial uart(D1, D0); int main() { char test; test = 'a'; int error; uint8_t farenheit = 0.0f; while(1) { wait(2); error = sensor.readData(); farenheit = sensor.ReadTemperature(FARENHEIT); printf("%u\n\r",farenheit); uart.putc(test); } }

But when I send numerical values, it goes as numeric. When I try to pass data values like "temperature" or any variables, it is being sent. But on the receiver side, I could see only numbers. ? If I wanted to send "Hello" using uart.putc() in the above code, how can I do? Any pointers would be helpful.

Thanks and regards Niranjan

Comment on this question

1 Answer

11 months, 3 weeks ago.

Hello Niranjan,

The putc() function sends only one int value (the value is internally converted to an unsigned char when written). When you'd like to send data which is longer than one char you have to send it byte by byte. So, you can send the "Hello" c-string for example as follows:

uart(USBTX, USBRX);

const char* c_string = "Hello";  // The compiler silently appends a '\0' char (i.e. a 0)

while (*c_string != 0) {    // while not at the end of c-string
    uart.putc(*c_string);   // send a char
    c_string++;             // move to the next char
}

ADVISE: To have a more readable code after pasting it here (to mbed pages) try to mark it up as below (with <<code>> and <</code>> tags, each on separate line at the begin and end of your code)

<<code>>
text of your code
<</code>>

Hi, it worked.

I modified the code:

uint8_t number = 5;
uart.putc(number); 

But on the receiver end, it is being read as 0. The values are not being read.

(the value is internally converted to an unsigned char when written) did you mean on the receiver end it will be a character or integer? I am confused about this.

posted by Niranjan Ravi 30 Oct 2018
  • What is your code on the receiver side? If the receiver is a serial terminal on your PC then beware that normally it displays ASCII characters. It means that because 5 does not represent any ASCII character nothing will be displayed. But if you send for example 53 (the ASCII code for digit 5) then a 5 will be displayed.
  • The putc(int character) function takes an int but this integer value is internally converted (truncated) to uint8_t (unsigned char) before sending (on the transmitter side). It means that if you for example pass it 32156 (0x7D9C) then only the less significant byte (0x9C) will be sent.
posted by Zoltan Hudak 30 Oct 2018