Read Data over Serial From PC

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
adrianovrm
Posts: 2
Joined: Thu Jun 11, 2015 3:20 pm

Read Data over Serial From PC

Post by adrianovrm » Wed Mar 08, 2017 9:17 pm

Well ,

After hours and hours trying to make my Macbook Send Simple String data to my pyboard v1.1 . l ask aid here ... let me explaind in details so :

L wrote the following python program onto main.py
# main.py -- put your code here!
from pyb import UART
import os


uart = UART(1, 9600)
#uart.init(9600, bits=8, parity=None, stop=1)


#if (os.isfile('data.txt')):
#if (isfile('data.txt')):
# print('data.txt')
# f = open('data.txt', 'r')
# f2 = open('dcopy.txt', 'w')
# f2.write(f.read())
# f2.close()
#else:
try:
f = open('data.txt', 'r')
f2 = open('dcopy.txt', 'w')
f2.write(f.read())
f2.close()
except:
f = open('data.txt', 'w')
f.write('tentou ler e nao existia')

f.close()
led = pyb.LED(2)
led.on()
while (1 == 1):
# uart.write('a')
if (uart.any() != 0):
uart.read()
led4 = pyb.LED(4)
led4.on()
break

led3 = pyb.LED(3)
led3.on()


just learning how use it . l was expect that after receive some bytes . the blue led of the board comes on.. then l use de coolterm program to connect to the device and send some string to pyboard .. well, l ve checked all the paramters of serial, 9600 8 n1 , equal at both sides.. but when l push control ctrl-c onto keyboard of macbook , the coolterm shows an keyboard interrupted and keep the REPL onto screnn working. before the ctrl-d the collterm show tx led as it was sending comands but the pyboard doenst shows the blue led . if l remove the uart.any line, the blue led lights, so my conclusion is that l m not receiving any bytes over usb/serial and l dont lnow how to sokve it , at arduino this is works like a charm, l getting crazy to make this work at pyboard world, simples is a much more powerful enviroment and l wanna leran and use.

Another details to help , this is my boot.py

# boot.py -- run on boot-up
# can run arbitrary Python, but best to keep it minimal

import pyb
#pyb.main('main.py') # main script to run after this one
pyb.usb_mode('CDC+MSC') # act as a serial and a storage device
#pyb.usb_mode('CDC+HID') # act as a serial device and a mouse

so guys , any magical tips ?

L tried at windows too , after install the *.inf driver, but had the same issues ...

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Read Data over Serial From PC

Post by dhylands » Thu Mar 09, 2017 5:44 am

If I'm reading this correctly, you're trying to send data over the same link as the REPL.

On the pyboard, the USB-serial isn't a UART. You can use pyb.USB_VCP() to get access to the virtual port associated with the USB-serial link (from the pyboard side of things): http://docs.micropython.org/en/latest/p ... B_VCP.html

To use a UART, you need to connect a logic-level serial adapter to one of the UARTs. See http://micropython.org/resources/pybv10-pinout.jpg

Connect Tx from the serial adapter to Rx on the pyboard, Rx from the serial adapter to Tx on the pyboard and also connect GND from the serial adapter to the pyboard. An example of a logic level serial adapter is something like this:
https://www.sparkfun.com/products/9873

I wrote some code which talks between the host and the pyboard over the USB-serial link here: https://github.com/dhylands/json-ipc

adrianovrm
Posts: 2
Joined: Thu Jun 11, 2015 3:20 pm

Re: Read Data over Serial From PC

Post by adrianovrm » Thu Mar 09, 2017 1:05 pm

Yes, l trying do exactly that ..
Communicate with pyboard in same way l used to with arduino and its ide, cause l m a newbie at pyboard.
l will try yours tips so, and will return here to report you my results.. thanks for your attention in advance ..

picolearner
Posts: 2
Joined: Tue Aug 17, 2021 6:13 am

Re: Read Data over Serial From PC

Post by picolearner » Tue Aug 17, 2021 6:23 am

I am trying to get data from Pico through HC05.
the code line below is not showing 3 values.
uart.write(str(round(pitch,1),round(roll,1),round(az,1)))
Rather,if I write the code below,it shows the values but transferred data is slow,
uart.write(str(round(pitch,1)))
uart.write(",")
uart.write(str(round(roll,1)))
uart.write(",")
uart.write(str(round(az,1)))
uart.write("\r\n")
sleep(0.1)


What is the correct format to acquire data faster? please help,I am using micropython

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

Re: Read Data over Serial From PC

Post by Roberthh » Tue Aug 17, 2021 6:35 am

uart.write() accepts only a single argument. That's why the first version fails. You can create a combined string using format() or the % operator to get the three values in one string item, like

uart.write("{:.1f},{:.1f},{:.1f}".format(ptch, roll, az))

But that would not speed up the UART transmission, which is most likely the limiting factor, not to mention the 100ms sleep(0.1) in the code. At 9600 baud, it is about 1 ms per character,

picolearner
Posts: 2
Joined: Tue Aug 17, 2021 6:13 am

Re: Read Data over Serial From PC

Post by picolearner » Thu Aug 19, 2021 3:50 am

Thanks for help

Post Reply