Sending float data type on UART using ESP32

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
hetvishah08
Posts: 8
Joined: Tue Jan 22, 2019 12:00 pm

Sending float data type on UART using ESP32

Post by hetvishah08 » Wed Jan 23, 2019 11:29 am

I want to send data to other devices using ESP32 DevkitC through UART communication.I want to send float data type and int data type.I know that bytearray converts it into ASCII and i want to send data of float type.How do i do that?

from machine import UART
u2=UART(2,9600)
u2.init(9600,bits=8,parity=None,stop=1,tx=17,rx=16)
list1=[90,100]
list2=bytearray(list1)
u2.write(list2)
print(list2)


i get ouput on UART2 : [Z,d]
and on UART0 : bytearray(b'\x00Z\x00d')

User avatar
Roberthh
Posts: 1518
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Sending float data type on UART using ESP32

Post by Roberthh » Wed Jan 23, 2019 12:02 pm

Do you want the data to be sent a) human readable or b) binary.
for a) you can create a string using string format
for b) you can use ustruct.pack

hetvishah08
Posts: 8
Joined: Tue Jan 22, 2019 12:00 pm

Re: Sending float data type on UART using ESP32

Post by hetvishah08 » Wed Jan 23, 2019 12:37 pm

I want it in a human readable format.I am receiving that data through Modbus TCP and sending it on UART2.I stored the data in a list and want to send it as it is but UART cannot send int or float type.

hetvishah08
Posts: 8
Joined: Tue Jan 22, 2019 12:00 pm

Re: Sending float data type on UART using ESP32

Post by hetvishah08 » Wed Jan 23, 2019 12:42 pm

I want it in a human readable format.I am just sending numbers.I am receiving those numbers in int or float data type through Modbus TCP on ESP32 and want to send that data on UART2 to some other device.I want the data to be sent as it is. I am storing the data received through modbus tcp in a list and that list contains 25 elements.I want to send those numbers in int or float data type.How do i do it?

User avatar
Roberthh
Posts: 1518
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Sending float data type on UART using ESP32

Post by Roberthh » Wed Jan 23, 2019 1:34 pm

Look at string formatting. There exists either the % operator, or the .format method for strings, all well documented for Python3. You could also use the str function. Example:
Assume a floating point number f = 1.234
str(f) returns the string "1.234"
The same does ("%f" % f ) or "{}".format(f)
The % for .format methods allow to specifiy the format like the number of significant digits etc. much like printf() in C. The % operator mor or less follows the C printf formatting rules.
To convert the string into a bytes object, you can use encode()
so
str(f).encode() returns b'1.234'

hetvishah08
Posts: 8
Joined: Tue Jan 22, 2019 12:00 pm

Re: Sending float data type on UART using ESP32

Post by hetvishah08 » Thu Jan 24, 2019 7:03 am

i used str(f) example and it worked.I used the same with the list and was able to send those numbers and receive it as it is.Thank you.

shraddhamane
Posts: 2
Joined: Wed Jan 23, 2019 9:14 am
Contact:

Re: Sending float data type on UART using ESP32

Post by shraddhamane » Mon Jan 28, 2019 4:15 am

I have the same issue..

hetvishah08
Posts: 8
Joined: Tue Jan 22, 2019 12:00 pm

Re: Sending float data type on UART using ESP32

Post by hetvishah08 » Tue Jan 29, 2019 6:01 am

Here is how i used it:

Code: Select all

>>> from machine import UART
>>> list1 = [1.23,4.55,678.55,347843.484]
>>> list2=str(list1)
>>> u2=UART(2,9600)
I (104379) uart: ALREADY NULL
>>> u2.init(9600,bits=8,parity=None,stop=1, tx=17,rx=16)
>>> u2.write(list2)
30
OUTPUT on TERMINAL:
[1.23, 4.55, 678.55, 347843.5]

Post Reply