AJB2K3COPERNICON
Published © CC BY-NC-ND

Control the Lego Interface B with Python Part 2

Part 2 of my controlling the old lego 9V computer interface with Python.

IntermediateProtip4 hours20
Control the Lego Interface B with Python Part 2

Things used in this project

Hardware components

Lego Dacta Interface B
×1
Prolific Serial to USB Adapter
×1
Null modem serial cable adapter
×1

Software apps and online services

Python

Story

Read more

Code

Oridginal test code

Python
import serial
import time
ser = serial.Serial(
port='/dev/cu.PL2303G-USBtoUART2430',
baudrate=9600,
#parity=serial.PARITY_NONE,
#stopbits=serial.STOPBITS_ONE,
#bytesize=serial.SEVENBITS,
)
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))
#print(ser.readline())
#ser.flushInput()
#ser.close()

Part 2 Ending code

Python
Command center port detection code from the end of Part 2
import serial
import time
from struct import *
import functools
ser = serial.Serial(
port='/dev/cu.PL2303G-USBtoUART2430',
baudrate=9600,
)
ser.write(b'p\0###Do you byte, when I knock?$$$') #initiates communication with the Control center.
print(ser.read(31))  # Check  to see if the Interface has responded. Stop LED should go off.
print("Lets looks at the input ports")
# the First two bytes should always be x00\x00. If the first two bytes are b'\x10\x00 then the stop button has been hit.
#Port 1 > 4 ofset value = 14081 when output voltage = 9vish
#Port 5 > 8 ofset value = 13057 when output voltage = 9vish
time.sleep(2)
while True:
ser.write(b'2')
dat = ser.read(19)
#    cksum = functools.reduce(lambda x, y: x + y, dat)
#   if cksum != 0xff:
#      print('Checksum error got', cksum, 'expected 0xff')
_, p4, p8, p3, p7, p2, p6, p1, p5 = unpack('<hhhhhhhhh', dat[:-1])
#    print(p1, p2, p3, p4, p5, p6, p7, p8)
#    print(f'Port1: {p1}, Port2: {p2}, Port3: {p3}, Port4: {p4}, Port5: {p5}, Port6: {p6}, Port7: {p7}, Port8: {p8}')
#     print(f'Port5: {p5}')
pt1 = p1 + 14081
pt2 = p2 + 14081
pt3 = p3 + 14081
pt4 = p4 + 14081
print(f'Port1: {pt1}')
print(f'Port2: {pt2}')
print(f'Port3: {pt3}')
print(f'Port4: {pt4}')

Credits

AJB2K3
58 projects • 39 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.
COPERNICON
1 project • 1 follower

Comments