From 70648f1e31f752b0c09f2bf8f9fc2b6318211c2d Mon Sep 17 00:00:00 2001 From: Muhamad Irfan Maulana <13515037@std.stei.itb.ac.id> Date: Wed, 11 Apr 2018 15:36:24 +0700 Subject: [PATCH] Add getSerial.py --- src/python/getSerial.py | 10 ++++++++++ src/python/pythonRead.py | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 src/python/getSerial.py diff --git a/src/python/getSerial.py b/src/python/getSerial.py new file mode 100644 index 0000000..1ed7e09 --- /dev/null +++ b/src/python/getSerial.py @@ -0,0 +1,10 @@ +import serial, struct, time +ser = serial.Serial('COM3') # open serial port +print(ser.name) # check which port was really used +# ser.write(b'\x04') # write a string +# ser.write(struct.pack('>B',1)) +while True : + line = ser.readline() + print(line.decode().split('\r\n')[0]) + +ser.close() # close port \ No newline at end of file diff --git a/src/python/pythonRead.py b/src/python/pythonRead.py index 3126e08..f424d93 100644 --- a/src/python/pythonRead.py +++ b/src/python/pythonRead.py @@ -1,9 +1,9 @@ import serial, urllib.request, json, time, struct -with urllib.request.urlopen("https://tranquil-citadel-63668.herokuapp.com/api/status") as url: - data = json.loads(url.read().decode()) +# with urllib.request.urlopen("https://tranquil-citadel-63668.herokuapp.com/api/status") as url: +# data = json.loads(url.read().decode()) -year = data['year'] +# year = data['year'] # data['year'] = 4 @@ -17,10 +17,12 @@ year = data['year'] ser = serial.Serial() ser.baudrate = 19200 -ser.port = 'COM8' +ser.port = 'COM3' ser.timeout = 1 ser.open() + +count = 0 while True : ser.flush() # print(ser.readable) @@ -38,7 +40,12 @@ while True : # print(struct.unpack('p',ser.read())) # print(b) # print(ser.read(4)) - print(ser.readline(ser.inWaiting())) + line = ser.readline(ser.inWaiting()) + print(line) + print(list(line)) + print(line.decode('cp855')) + ser.write(2) + # print(line.decode('utf-8')) # print(struct.unpack('b',ser.read())) time.sleep(3) ser.close() \ No newline at end of file -- GitLab