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')
Sending float data type on UART using ESP32
-
- Posts: 9
- Joined: Tue Jan 22, 2019 12:00 pm
Re: Sending float data type on UART using ESP32
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
for a) you can create a string using string format
for b) you can use ustruct.pack
-
- Posts: 9
- Joined: Tue Jan 22, 2019 12:00 pm
Re: Sending float data type on UART using ESP32
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.
-
- Posts: 9
- Joined: Tue Jan 22, 2019 12:00 pm
Re: Sending float data type on UART using ESP32
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?
Re: Sending float data type on UART using ESP32
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'
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'
-
- Posts: 9
- Joined: Tue Jan 22, 2019 12:00 pm
Re: Sending float data type on UART using ESP32
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.
-
- Posts: 2
- Joined: Wed Jan 23, 2019 9:14 am
- Contact:
Re: Sending float data type on UART using ESP32
I have the same issue..
-
- Posts: 9
- Joined: Tue Jan 22, 2019 12:00 pm
Re: Sending float data type on UART using ESP32
Here is how i used it:
OUTPUT on TERMINAL:
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
[1.23, 4.55, 678.55, 347843.5]