Skip to content

Instantly share code, notes, and snippets.

@dramamine
Created June 12, 2019 04:58
Show Gist options
  • Save dramamine/c7e02ac24ece61ab135b7a946fec2254 to your computer and use it in GitHub Desktop.
Save dramamine/c7e02ac24ece61ab135b7a946fec2254 to your computer and use it in GitHub Desktop.
import aioserial
import asyncio
# find port name by printing them all out
import serial.tools.list_ports
print([comport.device for comport in serial.tools.list_ports.comports()])
# callback for values. try setting this to a useful function
listener = None
async def get_values(aioserial_instance: aioserial.AioSerial):
while True:
text = (await aioserial_instance.readline_async()).decode(errors='ignore')
try:
val = int(text)
except:
val = 0
print('my val:', val)
if val > 0:
# do something
if listener:
listener(val)
def run():
asyncio.run(get_values(aioserial.AioSerial(port='COM3')))
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment