receiving serial data Magtag

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
artman70
 
Posts: 20
Joined: Tue Nov 29, 2016 8:44 am

receiving serial data Magtag

Post by artman70 »

is it possible to receive data via usb serial (uart) with the magtag.. i want to send a json via usb connection and parse/display.. im new to circuitpython and could use any help. is there an example that might show how to do this?
thank you!

User avatar
dastels
 
Posts: 15670
Joined: Tue Oct 20, 2015 3:22 pm

Re: receiving serial data Magtag

Post by dastels »

Have a look at sys,stdin https://circuitpython.readthedocs.io/en ... #sys.stdin.

Code: Select all

>>> dir(sys.stdin)
['__class__', '__enter__', '__exit__', '__next__', 'close', 'read', 'readinto', 'readline', 'write', '__del__', 'buffer', 'readlines']
The read, readinto, and readline methods are what you want to look at (from https://docs.python.org/3/library/io.html#module-io):
read(size=- 1)
Read up to size bytes from the object and return them. As a convenience, if size is unspecified or -1, all bytes until EOF are returned. Otherwise, only one system call is ever made. Fewer than size bytes may be returned if the operating system call returns fewer than size bytes.

If 0 bytes are returned, and size was not 0, this indicates end of file. If the object is in non-blocking mode and no bytes are available, None is returned.

readinto(b)
Read bytes into a pre-allocated, writable bytes-like object b, and return the number of bytes read. For example, b might be a bytearray. If the object is in non-blocking mode and no bytes are available, None is returned.

readline(size=- 1)
Read and return one line from the stream. If size is specified, at most size bytes will be read.

The line terminator is always b'\n' for binary files; for text files, the newline argument to open() can be used to select the line terminator(s) recognized.
Dave

User avatar
artman70
 
Posts: 20
Joined: Tue Nov 29, 2016 8:44 am

Re: receiving serial data Magtag

Post by artman70 »

thanks for the answer.. i will look into that as well.. but i cant seem to get the uart to work over usb.. using the code given as example.. it gives an error about the TX and RX in the declaration ----> uart = busio.UART(board.TX, board.RX, baudrate=9600)
error---> Traceback (most recent call last):
File "code.py", line 12, in <module>
AttributeError: 'module' object has no attribute 'TX'

Code: Select all

"""CircuitPython Essentials UART Serial example"""
import board
import busio
import digitalio

# For most CircuitPython boards:
led = digitalio.DigitalInOut(board.LED)
# For QT Py M0:
# led = digitalio.DigitalInOut(board.SCK)
led.direction = digitalio.Direction.OUTPUT

uart = busio.UART(board.TX, board.RX, baudrate=9600)

while True:
    data = uart.read(32)  # read up to 32 bytes
    # print(data)  # this is a bytearray type

    if data is not None:
        led.value = True

        # convert bytearray to string
        data_string = ''.join([chr(b) for b in data])
        print(data_string, end="")

        led.value = False

User avatar
dastels
 
Posts: 15670
Joined: Tue Oct 20, 2015 3:22 pm

Re: receiving serial data Magtag

Post by dastels »

You don't need to use Rx/Tx. sys.stdin is connected to the serial/USB connection. You just need to read from sys.stdin.

Dave

User avatar
tannewt
 
Posts: 3305
Joined: Thu Oct 06, 2016 8:48 pm

Re: receiving serial data Magtag

Post by tannewt »

Take a look at this guide for how to use a second usb serial connection for your data: https://learn.adafruit.com/customizing- ... 3096590-12

User avatar
artman70
 
Posts: 20
Joined: Tue Nov 29, 2016 8:44 am

Re: receiving serial data Magtag

Post by artman70 »

i was having trouble with that link you sent.. i need to edit boot.py but dont find that file.. (i know im a noob)
from what i read i need to add the

Code: Select all

import usb_cdc
usb_cdc.enable(console=True, data=True)    # Enable console and data
in order to get both a console (repl) and a data (serial)
is that correct?
and where do i make the edits..

im using a magtag
thank you for your help!!

User avatar
dastels
 
Posts: 15670
Joined: Tue Oct 20, 2015 3:22 pm

Re: receiving serial data Magtag

Post by dastels »

Add a boot.py file to the CIRCUITPY drive/directory. It isn't present unless you need to do something as CircuitPython starts up.

Dave

User avatar
artman70
 
Posts: 20
Joined: Tue Nov 29, 2016 8:44 am

Re: receiving serial data Magtag

Post by artman70 »

i made a boot.py file and put it in the directory you mentioned with that code in it.. it doesnt mount any disk and shows no com ports ..
i hade to safe boot to get it back..

User avatar
artman70
 
Posts: 20
Joined: Tue Nov 29, 2016 8:44 am

Re: receiving serial data Magtag

Post by artman70 »

Just wanted to give some scope.. i have a linux machine running node.js that will send a json file as string to the magtag for display. thats what im trying to accomplish.
i see 2 ways to do.. one is to use serial and send the string and the other is by mounting the magtag as a disk and writing afile to it with the json ..
do you have any suggestions?
thanks all!!

User avatar
tannewt
 
Posts: 3305
Joined: Thu Oct 06, 2016 8:48 pm

Re: receiving serial data Magtag

Post by tannewt »

Please post the full contents of boot.py that caused it to not work. I'm glad safe mode worked to recover.

I'd recommend the serial approach for sending the data. The filesystem may have sync issues that serial won't have.

User avatar
artman70
 
Posts: 20
Joined: Tue Nov 29, 2016 8:44 am

Re: receiving serial data Magtag

Post by artman70 »

import usb_cdc
usb_cdc.enable(console=True, data=True) # Enable console and data


it was just the above code in a file called boot.py in the circuitpy

what i really would like to do is have the magtag show up like an esp32 feather does when connected to the linux machine and accept serial text in the form of json string.. it works with an esp32 running c++.. is this possible with the repl and all running on magtag?

User avatar
tannewt
 
Posts: 3305
Joined: Thu Oct 06, 2016 8:48 pm

Re: receiving serial data Magtag

Post by tannewt »

With the guide I linked to it would show up as two serial ports, one for CircuitPython and one for your code. I don't know why it breaks the usb when you add that line.

User avatar
artman70
 
Posts: 20
Joined: Tue Nov 29, 2016 8:44 am

Re: receiving serial data Magtag

Post by artman70 »

but just those 2 lines of code in boot.py? is that import included with latest circuitpython for magtag?

User avatar
tannewt
 
Posts: 3305
Joined: Thu Oct 06, 2016 8:48 pm

Re: receiving serial data Magtag

Post by tannewt »

Ya, I think that is all you need in boot.py because it's changing how USB is working before it is connected. The code.py will need to reference the data cdc to send and receive the data. I think that is included in the learn guide.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”