Skip to content

Instantly share code, notes, and snippets.

@outtoin
Last active December 27, 2018 13:24
Show Gist options
  • Save outtoin/9486110254b25a97c00e87b79959d031 to your computer and use it in GitHub Desktop.
Save outtoin/9486110254b25a97c00e87b79959d031 to your computer and use it in GitHub Desktop.
import time
import serial
import traceback
import signal
import sys
import codecs
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='COM6',
baudrate=9600,
# parity=serial.PARITY_ODD,
# stopbits=serial.STOPBITS_TWO,
# bytesize=serial.SEVENBITS
)
ser.isOpen()
print ('Enter your commands below.\r\nInsert "exit" to leave the application.')
inputs=1
flag = 0
while 1 :
try:
# get keyboard input
# Python 3 users
inputs = input(">> ")
if inputs == 'exit':
ser.close()
break
# exit(0)
else:
# send the character to the device
# (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)
ser.write(inputs.encode('ascii'))
out = b''
# let's wait one second before reading output (let's give device time to answer)
time.sleep(1)
while ser.inWaiting() > 0:
a = ser.read(1)
if flag:
print(a.decode('ascii'))
else:
print(a)
flag = 1
out += a
if out != '':
print(out)
# print (">>" + out)
except:
traceback.print_exc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment