On the PC side:
Code: Select all
# Master device (host) main program
import serial
import time
slave = serial.Serial('/dev/ttyUSB0', 115200, timeout=3)
try:
time1 = time.time()
while True:
time2 = time.time()
if time2 - time1 > 1:
time1 = time2
packet = '[{:.3f}] ABCDEFGHIJKLMNOPQRSTUVWXYZ\n'.format(time1)
print('Sending packet: {}'.format(packet), end='')
slave.write(packet.encode('utf-8'))
# Uncomment following line to reduce host CPU usage
time.sleep(.1)
except KeyboardInterrupt:
print('Ctrl+C was pressed')
finally:
if slave.is_open:
slave.close()
print(' Serial connection was closed')
Code: Select all
Sending packet: [1554597073.694] ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sending packet: [1554597074.697] ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sending packet: [1554597075.699] ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sending packet: [1554597076.702] ABCDEFGHIJKLMNOPQRSTUVWXYZ
^CCtrl+C was pressed
Serial connection was closed
Code: Select all
uos.dupterm(None, 1)
Code: Select all
# Slave device (ESP8266 board) main program
import machine
import time
led = machine.Pin(2, machine.Pin.OUT) # set to 2 for builtin LED in Wemos D1 mini lite
debugger = machine.UART(1, 115200)
def debugPrint(msg):
debugger.write(str(msg) + '\r\n')
def main():
try:
uart = machine.UART(0)
uart.init(115200, timeout=100)
ticker = time.ticks_ms()
debugPrint('Entering main loop')
while True:
if uart.any():
msg = uart.readline()
debugPrint(msg)
except Exception as exc:
debugPrint('Exception was raised')
debugPrint(type(exc))
debugPrint(str(exc))
finally:
# Blink repeteadly
for i in range(8):
led.value(not led.value()) # Toggle LED
time.sleep(0.1)
# Run main loop
main()
Code: Select all
Entering main loop
b'[1554597073.694 ABCDEFGHIJKLMNPQRSTUVWXYZ\n'
b'[1554597074.697 ABCDEFGHIJKLMNPQRSTUVWXYZ\n'
b'[1554597075.699 ABCDEFGHIJKLMNPQRSTUVWXYZ\n'
b'[1554597076.702 ABCDEFGHIJKLMNPQRSTUVWXYZ\n'
My solution was to send one byte at a time from the host, adding a short delay between bytes. On the board, package is reconstructed:
PC code:
Code: Select all
time1 = time.time()
while True:
time2 = time.time()
if time2 - time1 > 1:
time1 = time2
packet = '[{:.3f}] ABCDEFGHIJKLMNOPQRSTUVWXYZ\n'.format(time1)
print('Sending packet: {}'.format(packet), end='')
for byte in list(packet):
slave.write(byte.encode())
time.sleep(.001)
Code: Select all
Sending packet: [1554598416.067] ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sending packet: [1554598417.123] ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sending packet: [1554598418.179] ABCDEFGHIJKLMNOPQRSTUVWXYZ
^CCtrl+C was pressed
Serial connection was closed
Code: Select all
buffer = []
while True:
if uart.any():
c = uart.read(1)
if c is not None:
if c.decode('utf-8') == '\n':
msg = ''.join([x.decode('utf-8') for x in buffer])
debugPrint(msg)
buffer = []
else:
buffer.append(c)
Code: Select all
Entering main loop
[1554598416.067] ABCDEFGHIJKLMNOPQRSTUVWXYZ
[1554598417.123] ABCDEFGHIJKLMNOPQRSTUVWXYZ
[1554598418.179] ABCDEFGHIJKLMNOPQRSTUVWXYZ
Thanks!
*I still don't fully understand REPL redirection/duplication. What is the 2nd parameter of uos.dupterm() for again? I think the documentation could benefit from more examples.