Accessing /dev/ttyUSB0 from the micropython unix port

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
jaimet
Posts: 3
Joined: Tue Apr 06, 2021 9:17 pm

Accessing /dev/ttyUSB0 from the micropython unix port

Post by jaimet » Tue Apr 06, 2021 11:19 pm

Hi all.

I have a DS18B20 digital thermometer connected via a Silicon Labs CP210x USB-to-UART Bridge to my laptop (which is running Debian testing), and I can access the bridge (from debian linux) via the usual device file /dev/ttyUSB0.

I would like to try out micropython for the first time, and I've successfully installed the unix port of micropython (version 1.14) on my debian amd64 laptop using snapcraft.io.

I've found the micropython tutorial's "Controlling 1-wire devices" page which contains the following code:

Code: Select all

# the device is on GPIO12
dat = machine.Pin(12)

# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))
but of course, as I am using the unix port of micropython, I have no pins - I need to a "OneWire" object out of /dev/ttyUSB0.

Is this possible? TIA, J

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

Re: Accessing /dev/ttyUSB0 from the micropython unix port

Post by Roberthh » Wed Apr 07, 2021 6:17 am

Is this possible?
No, it's not. The UART protocol and the DS18B20 protocol are completely different. UART uses a character tieming with start bit/5-9 data bits and stop bit, and deidcated input and output mode lines, while Onewire uses a single data line for both input and output, a bit timing switching between the modes and a 40 bit data frame. You cannot even use the modem control lines for that purpose.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Accessing /dev/ttyUSB0 from the micropython unix port

Post by stijn » Wed Apr 07, 2021 7:58 am

jaimet wrote:
Tue Apr 06, 2021 11:19 pm
I can access the bridge (from debian linux) via the usual device file /dev/ttyUSB0.
Have you tried standard open("/dev/ttyUSB0") in MicroPython? If it's a driver where you can set baud rate etc on the diriver, that might just work, if you're lucky. Otherwise you'd be looking at porting the relevant parts of pyserial to MicroPython. I'v recently written the core parts (i.e. just reading/writing) for Windows and it's some work, but doable.

jaimet
Posts: 3
Joined: Tue Apr 06, 2021 9:17 pm

Re: Accessing /dev/ttyUSB0 from the micropython unix port

Post by jaimet » Wed Apr 07, 2021 8:44 am

Roberthh wrote:
Wed Apr 07, 2021 6:17 am
Is this possible?
No, it's not. The UART protocol and the DS18B20 protocol are completely different. UART uses a character tieming with start bit/5-9 data bits and stop bit, and deidcated input and output mode lines, while Onewire uses a single data line for both input and output, a bit timing switching between the modes and a 40 bit data frame. You cannot even use the modem control lines for that purpose.
Hi @Roberthh.

Thank you very much for replying, but perhaps my question was badly phrased.

I already know that it is possible to talk "1-wire" to a DS18B20 using a UART (here is Dallas Semiconductors'/Maxim Integrated's "Using a UART to Implement a 1-Wire Bus Master" tutorial and here is an implementation with some excellent documentation).

My question(s) should, perhaps, have been:

What methods are available, under the unix port of micropython, to talk to a USB-to-UART bridge/adapter/converter (whose device node is, say, /dev/ttyUSB0)? (My long term goal is to be able to instantiate a onewire.OneWire object that "wraps" /dev/ttyUSB0 by writing "onewire.OneWire('/dev/ttyUSB0')").

Thanks again, J

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

Re: Accessing /dev/ttyUSB0 from the micropython unix port

Post by Roberthh » Wed Apr 07, 2021 11:53 am

That's interesting. You still need the circuitry to make the TX pin open drain.
For access to the board you would need something like pyserial. I found that link: https://pypi.org/project/micropython-serial/, but could not get it working instantly.

jaimet
Posts: 3
Joined: Tue Apr 06, 2021 9:17 pm

Re: Accessing /dev/ttyUSB0 from the micropython unix port

Post by jaimet » Wed Apr 07, 2021 12:21 pm

Roberthh wrote:
Wed Apr 07, 2021 11:53 am
I found that link: https://pypi.org/project/micropython-serial/
Nice find! I can see that this is going to take me some time... :D

Post Reply