AJB2K3
Published © CC BY-NC-SA

Control the Lego Interface B with Python

Use Python and Micropython to control the lego interface B and 9V lego technic components.

IntermediateProtip4 hours178
Control the Lego Interface B with Python

Story

Read more

Code

Interface B Test read Code

Python
This is just basic Python code for activating and communicating with the old Lego Interface B
import serial
import time
ser = serial.Serial(port='/dev/cu.PL2303G-USBtoUART24110')
ser.write(b'p\0###Do you byte, when I knock?$$$')
ser.read(16)

while True:
    ser.write(b'2')
    time.sleep(1)
    ser.read(16)
    print(ser.read(16))

Updated test code with serial buffer flush.

Python
I updated the code to flush the read buffer on each pass and now I see readings.
import serial
import time
ser = serial.Serial(port='/dev/cu.PL2303G-USBtoUART24110')
ser.write(b'p\0###Do you byte, when I knock?$$$')
#ser.read(16)
print(ser.read(31))
ser.flushInput()
ser.flushOutput()

while True:
    ser.write(b'2')
    #time.sleep(1)
    #ser.read(16)
    print(ser.read(19)) # Must always be 19 bytes
    #print(ser.read(2))
    ser.flushInput()

Credits

AJB2K3

AJB2K3

54 projects • 33 followers
I have always had an interest in electronics but having failed my school exams, it has taken me 20+ years to produce products to share.

Comments