This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#String json_data = "{\"Sesor_id\":\"3E24R\",\"Value\":" + (String)randNumber + "}"; | |
import serial, time, json | |
import matplotlib.pyplot as plt | |
import numpy as np | |
hw_sensor = serial.Serial(port='COM4', baudrate=115200, timeout=1, write_timeout=1) | |
fig, ax = plt.subplots() | |
t = np.linspace(0,20,21) | |
s = np.zeros(21) | |
if __name__ == '__main__': | |
while True: | |
hw_sensor.write('getValue'.encode('utf-8')) | |
time.sleep(0.2) | |
try: | |
raw_string_b = hw_sensor.readline() | |
raw_string_s = raw_string_b.decode('utf-8') | |
if(raw_string_s.index("}")>=0 and raw_string_s.index("{")==0): | |
raw_string_s = raw_string_s[0:raw_string_s.index("}")+1] | |
raw_string_j = json.loads(raw_string_s) | |
print(raw_string_j) | |
print(raw_string_j["Sensor_id"]) | |
print(raw_string_j["Value"]) | |
else: | |
print("error/ no } found.") | |
except: | |
print("Exception occurred, somthing wrong...") | |
t=np.delete(t,0) | |
t=np.append(t,t[-1]+1) | |
s=np.delete(s,0) | |
try: | |
s=np.append(s,raw_string_j["Value"]) | |
except: | |
s=np.append(s,-1) | |
print("Error 4534") | |
print(s) | |
plt.cla() | |
ax.plot(t,s) | |
plt.pause(0.02) | |
plt.show() | |
hw_sensor.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment