import sys
import select
import time
# Poll stdin without blocking
poll = select.poll()
poll.register(sys.stdin, select.POLLIN)
def handle_command(cmd_bytes):
"""
Replace this function with logic ported
from your Arduino C code.
cmd_bytes is a bytes object.
"""
# Example fixed reply (replace!)
return b'\xFF\xFF\x01\x02\x00\xFC'
while True:
if poll.poll(0):
# Read available data
data = sys.stdin.buffer.read(64)
if data:
response = handle_command(data)
if response:
sys.stdout.buffer.write(response)
sys.stdout.buffer.flush()
time.sleep_ms(1)
for b in response:
sys.stdout.buffer.write(bytes([b]))
sys.stdout.buffer.flush()
time.sleep_us(1040) # ~9600 baud
import sys
import select
import time
poll = select.poll()
poll.register(sys.stdin, select.POLLIN)
def handle_command(data):
# TODO: port Arduino C logic here
return b'\xFF\xFF\x01\x02\x00\xFC'
while True:
if poll.poll(0):
data = sys.stdin.buffer.read(64)
if data:
response = handle_command(data)
if response:
sys.stdout.buffer.write(response)
sys.stdout.buffer.flush()
time.sleep_ms(1)