Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

ImportError: no module named 'serial'
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

ImportError: no module named 'serial'

by alpierce on Wed Sep 23, 2020 7:03 pm

trying to use example code for UART ranging module, Adafruit part # 4665. Fails to compile with error

code.py output:
Traceback (most recent call last):
File "code.py", line 2, in <module>
ImportError: no module named 'serial'

I can find no mention of a function module called "serial" in any of the documentation
There is no such library in the bundle
Have tried 5.3.1 and 6.0 beta

Does this function even exist?


Code: Select all | TOGGLE FULL SIZE

import time
import serial

SERIAL_PORT = "/dev/ttyS15"   # or "COM4" or whatever

serialport = serial.Serial(SERIAL_PORT, 9600)


def read_me007ys(ser, timeout = 1.0):
    ts = time.monotonic()
    buf = bytearray(3)
    idx = 0

    while True:
        # Option 1, we time out while waiting to get valid data
        if time.monotonic() - ts > timeout:
            raise RuntimeError("Timed out waiting for data")

        c = ser.read(1)[0]
        #print(c)
        if idx == 0 and c == 0xFF:
            buf[0] = c
            idx = idx + 1
           
        elif 0 < idx < 3:
            buf[idx] = c
            idx = idx + 1
        else:
            chksum = sum(buf) & 0xFF
            if chksum == c:
                return (buf[1] << 8) + buf[2]
            idx = 0
    return None

while True:
    dist = read_me007ys(serialport)
    print("Distance = %d mm" % dist)



code.py output:
Traceback (most recent call last):
File "code.py", line 2, in <module>
ImportError: no module named 'serial'

alpierce
 
Posts: 41
Joined: Mon May 13, 2013 2:44 am

Re: ImportError: no module named 'serial'

by siddacious on Wed Sep 23, 2020 7:54 pm

What board or computer are you trying to run this code with?

It looks like the example code is meant to be run on a computer using Python and the pyserial module.

I don't have one of these sensors so I can't test, but you should be able to replace the serial connection setup with code to use the UART class in the busio module

Code: Select all | TOGGLE FULL SIZE
import board
import busio
serialport = busio.UART(board.TX, board.RX, baudrate=9600)

You'll of course need to hook then sensor up to the TX and RX pins of your board.

Remember! RX on the sensor goes to TX on the board. It's a common mistake I make regularly to connect to the pin with the same name.

Let me know if that works for you

siddacious
 
Posts: 338
Joined: Fri Apr 21, 2017 3:09 pm

Re: ImportError: no module named 'serial'

by alpierce on Wed Sep 23, 2020 8:15 pm

Thanks siddacious,

I was trying to run it on a Trinket M0, and an ItsyBitsy M4, so that explains it.
I just mis-read the Python as CircuitPython!
I saw the busio example in an older post for a GPS sensor, but thought it was deprecated or something.
I'll try it and let you know.
Anyway, thanks again for the quick reply!

alpierce
 
Posts: 41
Joined: Mon May 13, 2013 2:44 am

Re: ImportError: no module named 'serial'

by alpierce on Wed Sep 23, 2020 8:34 pm

Oh well, more work to do yet, i'll keep you posted.

code.py output:
Traceback (most recent call last):
File "code.py", line 35, in <module>
File "code.py", line 18, in read_me007ys
TypeError: 'NoneType' object is not subscriptable

Code: Select all | TOGGLE FULL SIZE
import time
import board
import busio

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



def read_me007ys(ser, timeout = 1.0):
    ts = time.monotonic()
    buf = bytearray(3)
    idx = 0

    while True:
        # Option 1, we time out while waiting to get valid data
        if time.monotonic() - ts > timeout:
            raise RuntimeError("Timed out waiting for data")
        c = ser.read(1)[0]
        #print(c)
        if idx == 0 and c == 0xFF:
            buf[0] = c
            idx = idx + 1
           
        elif 0 < idx < 3:
            buf[idx] = c
            idx = idx + 1
        else:
            chksum = sum(buf) & 0xFF
            if chksum == c:
                return (buf[1] << 8) + buf[2]
            idx = 0
    return None

while True:
    dist = read_me007ys(serialport)
    print("Distance = %d mm" % dist)
Last edited by tannewt on Thu Sep 24, 2020 2:16 pm, edited 1 time in total.
Reason: put code tags around code

alpierce
 
Posts: 41
Joined: Mon May 13, 2013 2:44 am

Re: ImportError: no module named 'serial'

by alpierce on Thu Sep 24, 2020 2:25 pm

Enjoy
Code: Select all | TOGGLE FULL SIZE

# CircuitPython code for Adafruit UART Ranging module P4665
# Al Pierce Sept 9, 2020
# Based on Adafruit CircuitPython_UART.py example code

import board
import busio
import digitalio

led = digitalio.DigitalInOut(board.D13)
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
   
    if data is not None:
        led.value = True

        # convert bytearray to distance in mm
        distance = data[1] * 256 + data[2]
        # print distance in cm
        print(distance/10)

        led.value = False

alpierce
 
Posts: 41
Joined: Mon May 13, 2013 2:44 am

Please be positive and constructive with your questions and comments.