Commit 864c7749 by Sebastian Renner

Debug to stderr if necessary

parent 520bc41f
...@@ -7,7 +7,8 @@ import struct ...@@ -7,7 +7,8 @@ import struct
import serial import serial
import subprocess import subprocess
f = open("subprocess.log", "w+") DBG='1'
ser = serial.Serial('/dev/ttyUSB0', 115200) ser = serial.Serial('/dev/ttyUSB0', 115200)
...@@ -23,8 +24,6 @@ def read(channel, l): ...@@ -23,8 +24,6 @@ def read(channel, l):
data = sys.stdin.buffer.read(l) data = sys.stdin.buffer.read(l)
else: else:
raise Exception("read() complains: no sai channelino") raise Exception("read() complains: no sai channelino")
# if channel == "std" and len(data) == 0:
# sys.exit(0)
if len(data) != l: if len(data) != l:
raise Exception("could not read %d bytes of data (got %d)" % (l, len(data))) raise Exception("could not read %d bytes of data (got %d)" % (l, len(data)))
return data return data
...@@ -32,7 +31,8 @@ def read(channel, l): ...@@ -32,7 +31,8 @@ def read(channel, l):
def write(channel, data): def write(channel, data):
if channel == 'ser': if channel == 'ser':
l = ser.write(data) l = ser.write(data)
f.write("Data sent to serial: " + str(data) + "\n") if DBG:
print("Data sent to serial: " + str(data) + "\n", file=sys.stderr)
elif channel == 'std': elif channel == 'std':
l = sys.stdout.buffer.write(data) l = sys.stdout.buffer.write(data)
else: else:
...@@ -43,7 +43,8 @@ def write(channel, data): ...@@ -43,7 +43,8 @@ def write(channel, data):
def obtain(channel): def obtain(channel):
l = read(channel, 4) l = read(channel, 4)
(l, ) = struct.unpack("<I", l) (l, ) = struct.unpack("<I", l)
f.write("Trying to read %d bytes from %s\n" % (l, channel)) if DBG:
print("Trying to read %d bytes from %s\n" % (l, channel), file=sys.stderr)
return read(channel, l) return read(channel, l)
def submit(channel, action, data): def submit(channel, action, data):
...@@ -56,15 +57,17 @@ def submit(channel, action, data): ...@@ -56,15 +57,17 @@ def submit(channel, action, data):
time.sleep(0.1) time.sleep(0.1)
def main(argv): def main(argv):
if DBG:
print(argv[0], file=sys.stderr)
script_dir = os.path.split(argv[0])[0] script_dir = os.path.split(argv[0])[0]
if len(script_dir ) > 0: if len(script_dir ) > 0:
os.chdir(script_dir) os.chdir(script_dir)
f.write(os.getcwd())
flash() flash()
print("Hello, World!") print("Hello, World!")
while(1): while(1):
action = read('std', 1) action = read('std', 1)
f.write("Read following action from stdin: %s\n" % action) if DBG:
print("Read following action from stdin: %s\n" % action, file=sys.stderr)
if action == b'c' or action == b'm' or action == b'a' or action == b'k' or action == b's' or action == b'p': if action == b'c' or action == b'm' or action == b'a' or action == b'k' or action == b's' or action == b'p':
data = obtain('std') data = obtain('std')
submit('ser', action, data) submit('ser', action, data)
...@@ -75,9 +78,11 @@ def main(argv): ...@@ -75,9 +78,11 @@ def main(argv):
elif action == b'C' or action == b'M' or action == b'A' or action == b'K' or action == b'S' or action == b'P': elif action == b'C' or action == b'M' or action == b'A' or action == b'K' or action == b'S' or action == b'P':
write('ser', action) write('ser', action)
data = obtain('ser') data = obtain('ser')
f.write("Obtained following data from serial: %s\n" % data) if DBG:
print("Obtained following data from serial: %s\n" % data, file=sys.stderr)
submit('std', action, data) submit('std', action, data)
f.write("Submitted following data to stdout: %s\n" % data) if DBG:
print("Submitted following data to stdout: %s\n" % data, file=sys.stderr)
else: else:
raise Exception("no capiche aczione %s" % (action)) raise Exception("no capiche aczione %s" % (action))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment