-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopenscopedash.py
More file actions
92 lines (78 loc) · 2.82 KB
/
openscopedash.py
File metadata and controls
92 lines (78 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
import dash
from dash.dependencies import Input, Output, Event
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
import requests
import re
import json
import numpy
import pprint
url = 'http://localhost:42135'
#url = 'http://10.190.76.108'
pp = pprint.PrettyPrinter(indent=4)
'''
payload = {"device": [{"command": "enumerate"}]}
r = requests.post(url, json=payload)
pp.pprint(r.json())
'''
app = dash.Dash()
app.layout = html.Div([
html.Section([],className='header'),
html.H1(children='Dash'),
html.Div(children='A web application framework for Python.'),
html.Hr(),
dcc.Graph(id='example-graph'),
dcc.Interval(
id='interval-component',
interval=5*1000 # in milliseconds
)
], className='container')
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
@app.callback(Output('example-graph', 'figure'),
events=[Event('interval-component', 'interval')])
def update_graph_live():
data = {'time': [],'voltage': []}
'''
payload = {"awg":{"1":[{"command":"stop"},
{"command":"setRegularWaveform",
"signalType":"square",
"signalFreq":1000000,
"vpp":3000,
"vOffset":0},
{"command":"run"}]}}
r = requests.post(url, json=payload)
'''
payload = {"trigger":{"1":[{"command":"single"}]}}
r = requests.post(url, json=payload)
payload = {'osc': {'1': [{'command': 'read', 'acqCount': 1}]}}
r = requests.post(url, json=payload)
result = re.split(b'\r\n',r.content)
# Decode UTF-8 bytes to Unicode, and convert single quotes
# to double quotes to make it valid JSON
if len(result) > 1 and result[1]:
str_json = result[1].decode('ASCII').replace("'", '"')
# Load the JSON to a Python list & dump it back out as formatted JSON
my_json = json.loads(str_json)
#pp.pprint(my_json)
actualSampleFreq = my_json['osc']['1'][0]['actualSampleFreq']/1000
for i in range(0, len(result[4])-2, 2):
data['voltage'].append(int.from_bytes(result[4][i:i+2], byteorder='little', signed=True))
data['time'].append(i/2/actualSampleFreq)
# Create the graph
return {
'data': [go.Scatter(
x=data['time'],
y=data['voltage'],
mode='lines',
)],
'layout': go.Layout(
margin={'l': 40, 'b': 40, 't': 10, 'r': 0},
)
}
else:
print('Error reading')
if __name__ == '__main__':
app.run_server(debug=True)