AJB2K3
Published © CC BY-NC-ND

Lego Dacta Interface B Part 3 - Starting Again

Learning Python again to make more stable code.

IntermediateProtip1 hour78
Lego Dacta Interface B Part 3 - Starting Again

Things used in this project

Hardware components

Lego Dacta Interface B
×1
Lego City Sounder
×1

Software apps and online services

Thonny
Python Idle

Story

Read more

Code

Example siren code

Python
When run it will read the input data and when the stop button is pressed will trigger the siren connected to output port A
import serial
import time
ser = serial.Serial(
    port='/dev/cu.PL2303G-USBtoUART2420',
    # Set additional parameters if strictly required.
)

ser.write(b'p\0###Do you byte, when I knock?$$$')
print(b'p\0###Do you byte, when I knock?$$$')
print(ser.read(31))
time.sleep(1)

input_bytes = bytearray(ser.read(19))
keepalive = (b'\x02')

## Input ports definition
st = (input_bytes[0:2])
p4 = (input_bytes[2:4])
p8 = (input_bytes[4:6])
p3 = (input_bytes[6:8])
p7 = (input_bytes[8:10])
p2 = (input_bytes[10:12])
p6 = (input_bytes[12:14])
p1 = (input_bytes[14:16])
p5 = (input_bytes[16:18])
cks = (input_bytes[18])

##Prints Unlabeled byte values from input array
print(input_bytes)
print(st)
print(p1)
print(p2)
print(p3)
print(p4)
print(p5)
print(p6)
print(p7)
print(p8)
print(cks)

##Labeled byte values from input array
print(f'STOP: {st}')
print(f'Port1: {p1}')
print(f'Port2: {p2}')
print(f'Port3: {p3}')
print(f'Port4: {p4}')
print(f'Port5: {p5}')
print(f'Port6: {p6}')
print(f'Port7: {p7}')
print(f'Port8: {p8}')
print(f'Checksum: {cks}')

##int from bytes??
port4 = int.from_bytes(p4, "big")
print(port4)
print(f'Port4: {port4}')

#stop = int.from_bytes(st, "big")
#print(stop)
#print(f'Stop Button: {stop}')

while True:
    ser.write(keepalive)
    input_bytes = bytearray(ser.read(19))
    st = (input_bytes[0:2])
    p4 = (input_bytes[2:4])
    stop = int.from_bytes(st, "big")
    print(stop)
    print(f'Stop Button: {stop}')
    port4 = int.from_bytes(p4, "big")
    print(port4)
    print(f'Port4: {port4}')
    if stop > 0:
        print('Stop button pressed.')
        ser.write(b'\x91\x01') #Turns on Output Port A
    if port4 <60000:
         print('Touch Sensor pressed.')

Credits

AJB2K3
60 projects • 41 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