UART - sending from pyboard to PC

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
ulrich
Posts: 14
Joined: Thu Nov 27, 2014 7:17 pm

UART - sending from pyboard to PC

Post by ulrich » Thu Nov 27, 2014 7:26 pm

Good evening!

I'm a proud newbie of a pyboard and would like to use it to sample a signal from the ADC, do basic processing on it and then send some resulting data from the pyboard to a connected PC (via the USB cable) to be processed/displayed there using python. While I have quite some experience with Python, I've never used uart (or similar stuff) bevore...

Given the UART example on the webpage, I've managed to send strings from my PC from python to the pyboard. Unfortunately I fail to do it the other way round.

On the pyboard, I do:
import pyb
import select
u = pyb.UART(1, 9600)
u.write('teststring')

On my pc in ipython:
import serial
ser = serial.Serial('/dev/ttyACM0',9600, timeout=1)
ser.readall()

-> The last command (readall) returns an empty string (same for readline(), readlines()) adding '\n' to the string etc...

Could someone pl. help me and fix this simple example/ point me to a working example?

Thanks!

User avatar
gcarver
Posts: 34
Joined: Sun Oct 26, 2014 4:07 am
Location: New Market, Maryland
Contact:

Re: UART - sending from pyboard to PC

Post by gcarver » Fri Nov 28, 2014 3:49 am

I use the USB_VCP module to communicate with the PC over USB.
http://docs.micropython.org/en/latest/l ... B_VCP.html
You use it much like UART only it goes back to the PC over the COM port the uPython board is assigned.
End of line...

ulrich
Posts: 14
Joined: Thu Nov 27, 2014 7:17 pm

Re: UART - sending from pyboard to PC

Post by ulrich » Fri Nov 28, 2014 10:09 am

Hi,

Thanks a lot! I'll try it once I'm back home to play with my pyboard.

Do you maybe happen to have a short example illustrating the commands on the pyboard and PC?

fma
Posts: 161
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: UART - sending from pyboard to PC

Post by fma » Fri Nov 28, 2014 1:35 pm

@ulrich, the port /dev/ttyACM0 is bind to the virtual serial line over usb; UART(1) is a real serial line, connected to X9/X10:

http://micropython.org/resources/pybv10-pinout.jpg

If you want to test this real serial line, you need to connect it to your PC, through a FTDI cable, for example (USB-TTL, not USB-RS232).
Frédéric

ulrich
Posts: 14
Joined: Thu Nov 27, 2014 7:17 pm

Re: UART - sending from pyboard to PC

Post by ulrich » Wed Dec 03, 2014 9:00 pm

Hi,

Thanks for your replies. I'm not back home and eager to tackle this issue again!

I'm slowly learning... SO UART is actually not what I'm looking for. I have no crutial real time requirements or lot of data... All I want is to send a few strings to a python running on my pc.

I'm now trying on my pyboard:
u = pyb.USB_VCP()
u.send('sending up\n', *, timeout=5000)

and on my pc:
import serial
ser = serial.Serial('/dev/ttyACM0',9600, timeout=1)
while True:
rcv = ser.readlines()
print(rcv)

but I still don't receive anything...

Do you have any other ideas?

Thanks,

Ulrich

fma
Posts: 161
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: UART - sending from pyboard to PC

Post by fma » Thu Dec 04, 2014 6:18 am

Have a look at the pyboard.py script, which is uses to transmit and run scripts to pyboard...
Frédéric

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

Re: UART - sending from pyboard to PC

Post by dhylands » Thu Dec 04, 2014 6:38 am

ulrich wrote: while True:
rcv = ser.readlines()
print(rcv)
readline() reads a single line from the file.
readlines() reads ALL of the lines from the file before returning. The serial port doesn't really have an end-of-file, so readlines may never return.

Here's a serial terminal program that I wrote for llinux. It uses udev to detect when the serial port shows up and goes away, and then uses pyserial to read/write the data a single character at a time.

It also uses termios to put the serial port in raw mode.

https://github.com/dhylands/usb-ser-mon ... ser-mon.py

ulrich
Posts: 14
Joined: Thu Nov 27, 2014 7:17 pm

Re: UART - sending from pyboard to PC

Post by ulrich » Fri Dec 05, 2014 10:08 pm

Hi,

Thanks for all the hints -unfortunately my ignorance about USB, protcolls etc is still too big... I've used this occasion to try to learn about it now...

I've had a look at the pyboard.py - while it contains stuff close to what I need, it's not quite what it want...
Also the usb-erial code that was mentioned did not solve the problem (I've tried all combinations of read, readline, readlines, readall,...)
I've now noticed something, that seems odd: If I enter on my pyboard
usb = pyb.USB_VCP()
usb.send(b'testup')
it returns "testup6" although the command should only return the numebr of bytes (and not my string)
If I try to send an integer only, the return value is "1" - which seems to be correct only the bytes (but still nothing readable on my PC...

Cheers,

Ulrich

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

Re: UART - sending from pyboard to PC

Post by dhylands » Fri Dec 05, 2014 10:56 pm

When you do USB.send it's sending the data on the same stream as the reply. So you see 'testup'. Then because you didn't assign the result to a variable it prints the result 6 on the repl which is also on USB_VCP

rankor
Posts: 38
Joined: Sun Nov 30, 2014 12:38 am

Re: UART - sending from pyboard to PC

Post by rankor » Sat Dec 06, 2014 12:17 am

>>> from pyb import UART
>>> uart=UART(3,9600)
>>> pyb.repl_uart(uart)
>>> uart.send('AT+PIN3456')
>>>
>>> y4=pyb.Pin('Y4',pyb.Pin.OUT_PP)
>>> y4.low()
>>> uart.send('AT+PIN3456')
>>>
>>> y4.high()
>>> ERO:0
File "<stdin>", line 1, column 4
SyntaxError: invalid syntax
>>> ERO:0
File "<stdin>", line 1, column 4
SyntaxError: invalid syntax
>>> ERO:0)ERO:0


Is that ERO:0 stuff from Micropython or from the UART? I am trying to send to the JYMCA Bluetooth module. I set the pin high to set the device in AT mode.

Post Reply