Sending and decoding data over UART
Posted: Thu Feb 27, 2020 12:21 pm
Hello,
I'm pretty new here with micropython and I'm facing a problem that I can not solve.
Basically, I use the Pyboard to read a Pentiometer on Input X19 and I wanto to send the read value via UART to a Host (PC, or, later a Raspberry PI) where a Python script is polling the serial interface.
PyBoard Code is:
-------
This code woks fine. When I use Putty to open a REPL on pyboard I see the correct values (from 0 to 4095, depending on the Potentiometer setting). E.g. the output is:
385
385
386
...
....
-----
I use the following code to open the corresponding COM port on the PC:
-------
The point is, that I found no way of reading a correct string on the PC. I mean that I was not able to find a way of the reading the "right" numbers of bytes...
In the specific case the result will be:
3
8
5
3
8
5
3
8
6
.
.
-------------
The python routine on the PC should be able to understand that the measurement could vary from "0" to "4095". How??
Any help will be greatly appreciated.
I'm pretty new here with micropython and I'm facing a problem that I can not solve.
Basically, I use the Pyboard to read a Pentiometer on Input X19 and I wanto to send the read value via UART to a Host (PC, or, later a Raspberry PI) where a Python script is polling the serial interface.
PyBoard Code is:
Code: Select all
# main.py -- put your code here!
from pyb import Pin, ADC, UART, LED
import time
led = LED(4)
adc = ADC(Pin("X19"))
uart = UART(1,9600)
uart.init(9600, bits=8,parity=None, stop=1)
while True:
a=adc.read()
uart.write(str(a))
print(a)
pyb.delay(250)
led.toggle()
This code woks fine. When I use Putty to open a REPL on pyboard I see the correct values (from 0 to 4095, depending on the Potentiometer setting). E.g. the output is:
385
385
386
...
....
-----
I use the following code to open the corresponding COM port on the PC:
Code: Select all
#!/usr/bin/python3
import serial
sp=serial.Serial("COM8", 9600, timeout=1)
while True:
data=sp.read()
# print(data)
# print(data.decode())
print ((data.decode().strip()))
The point is, that I found no way of reading a correct string on the PC. I mean that I was not able to find a way of the reading the "right" numbers of bytes...
In the specific case the result will be:
3
8
5
3
8
5
3
8
6
.
.
-------------
The python routine on the PC should be able to understand that the measurement could vary from "0" to "4095". How??
Any help will be greatly appreciated.