mattercontrol/ComTester.py

37 lines
843 B
Python
Raw Normal View History

2014-12-31 10:01:13 -08:00
import serial
2014-12-31 12:32:00 -08:00
import time
import random
2014-12-31 10:01:13 -08:00
ser = serial.Serial('COM14', 250000, timeout=1)
2014-12-31 12:32:00 -08:00
print "Initializing emulator..."
"""Add response callbacks here"""
def randomTemp(command):
2014-12-31 12:37:04 -08:00
# temp commands look like this: ok T:19.4 /0.0 B:0.0 /0.0 @:0 B@:0
2014-12-31 12:32:00 -08:00
return "ok T:%s\n" % random.randrange(202,215)
def echo(command):
return command
"""Dictionary of command and response callback"""
responses = { "M105" : randomTemp, "A": echo}
def getCorrectResponse(command):
try:
#Remove line returns
command = ''.join(command.splitlines())
if command in responses:
return responses[command](command)
except Exception, e:
print e
return 'ok\n'
2014-12-31 10:01:13 -08:00
while True:
line = ser.readline() # read a '\n' terminated line
2014-12-31 12:32:00 -08:00
if len(line) > 0:
print(line)
response = getCorrectResponse(line)
print response
ser.write(response)
2014-12-31 10:01:13 -08:00
ser.close()