Hi, I am trying to access com port to read data from an STM32 based sensor board. My main problem at this point is that i cannot access the serial port through the dash app. I am using windows 10 and serial port works fine without the app. here is my code
import serial
import re
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
s = serial.Serial(port='COM5',baudrate=460800, timeout =1)
s.parity = serial.PARITY_NONE #set parity check: no parity
s.stopbits = serial.STOPBITS_ONE #number of stop bits
s.bytesize = serial.EIGHTBITS
# s.timeout = 1 #timeout block read
s.xonxoff = True #disable software flow control
s.rtscts = False #enable hardware (RTS/CTS) flow control
s.dsrdtr = False #disable hardware (DSR/DTR) flow control
s.writeTimeout = 5 #timeout for write
s.reset_input_buffer()
app = dash.Dash(__name__)
app.layout = html.Div(
html.Div([
html.H4('Serial-Port-Test'),
html.Div(id='live-update-text'),
dcc.Interval(
id='interval-component',
interval=1*1000, # in milliseconds
n_intervals=0
)
])
)
@app.callback(Output('live-update-text', 'children'),
[Input('interval-component', 'n_intervals')])
def update_metrics(n):
s.open()
(x,y,z) = re.sub(b'\x00','',s.readline().strip('\r\n')).split(',')
style = {'padding': '5px', 'fontSize': '16px'}
s.close()
return [
html.Span('X: {0:.2f}'.format(float(x)), style=style),
html.Span('Y: {0:.2f}'.format(float(y)), style=style),
html.Span('Z: {0:0.2f}'.format(float(z)), style=style)
]
if __name__ == '__main__':
app.run_server(debug=True)
The error i get is:
Traceback (most recent call last):
File "C:\Users\Mano\Documents\CPP\workspace\DashTest\src\serialStream.py", line 40, in <module>
s = serial.Serial(port='COM5',baudrate=460800, timeout =1)
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 31, in __init__
super(Serial, self).__init__(*args, **kwargs)
File "C:\Python27\lib\site-packages\serial\serialutil.py", line 240, in __init__
self.open()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 62, in open
raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM5': WindowsError(5, 'Access is denied.')