Skip to content
Snippets Groups Projects
Commit ec854c1c authored by Jasper Franse's avatar Jasper Franse
Browse files

example coding

parent e853ed81
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:clean-marsh tags:
``` python
# import warnings
# warnings.filterwarnings('ignore')
# import sys
# import os
# import shutil
# from pyrpl import Pyrpl
# import numpy as np
# import time
# import matplotlib.pyplot as plts
# import IPython
# import ipywidgets as widgets
# from bokeh.plotting import figure, show
# from bokeh.io import output_notebook, push_notebook
# from bokeh.layouts import column, row
# from bokeh.models import ColumnDataSource, Toggle, Range1d
# from scipy.signal import welch
# from datetime import datetime
#Arduino Serial connection
import serial
```
%% Cell type:code id:variable-composite tags:
``` python
ser = serial.Serial('COM9',9600,timeout=0.5)
ser.flush()
time.sleep(0.5)
ser.reset_input_buffer()
ser.write(b'at')
ser.write(b'ch1')
```
%% Output
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
/opt/conda/lib/python3.8/site-packages/serial/serialposix.py in open(self)
321 try:
--> 322 self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
323 except OSError as msg:
FileNotFoundError: [Errno 2] No such file or directory: 'COM9'
During handling of the above exception, another exception occurred:
SerialException Traceback (most recent call last)
<ipython-input-5-ccbc81474993> in <module>
----> 1 ser = serial.Serial('COM9',9600,timeout=0.5)
2 ser.flush()
3
4 time.sleep(0.5)
5 ser.reset_input_buffer()
/opt/conda/lib/python3.8/site-packages/serial/serialutil.py in __init__(self, port, baudrate, bytesize, parity, stopbits, timeout, xonxoff, rtscts, write_timeout, dsrdtr, inter_byte_timeout, exclusive, **kwargs)
242
243 if port is not None:
--> 244 self.open()
245
246 # - - - - - - - - - - - - - - - - - - - - - - - -
/opt/conda/lib/python3.8/site-packages/serial/serialposix.py in open(self)
323 except OSError as msg:
324 self.fd = None
--> 325 raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
326 #~ fcntl.fcntl(self.fd, fcntl.F_SETFL, 0) # set blocking
327
SerialException: [Errno 2] could not open port COM9: [Errno 2] No such file or directory: 'COM9'
%% Cell type:code id:rural-musical tags:
``` python
```
%% Cell type:code id:vulnerable-leone tags:
``` python
```
%% Cell type:code id:freelance-ticket tags:
``` python
```
%% Cell type:code id:continuing-article tags:
``` python
# Global variables
R = []
T = []
tm = []
Iavg = []
Iptp = []
Vavg = []
Vptp = []
t0 = time.time()
timer_t0 = time.time()
```
%% Cell type:code id:verbal-blame tags:
``` python
def get_temperature(serial_port, baudrate, timeout):
# serial_port='COM7', baudrate=9600, timeout=1
#variables (could be adjusted to inputs?)
alpha = 4.1139e-3; R0 = 1000; R1=1081; Vin=3.272 #should be according to literature: alpha = 3.85e-3 & R0 = 1000
#open Serial port to Arduino + flush what's still on it
ser = serial.Serial(serial_port,baudrate,timeout=0.5)
ser.flush()
#sent command to Arduino so we can read it out:
time.sleep(0.5) #needs to sleep, otherwise we get a: 'can not convert float... at the Vout-line'
ser.reset_input_buffer()
ser.write(b'g')
#Serial read + Temperature calculations:
serial_reply = ser.readline()
try:
Vout = float(str(serial_reply.decode("utf-8")))*(Vin/1023.0) #calculates the voltage from the voltage divider (over the PT1000)
except:
return serial_reply
Rpt = (Vout*R1)/(Vin-Vout) #calculates the resistance of the PT1000
Temp_C = ((Rpt/R0)-1)/alpha #with the above calculated resistance and 'known' variable we calculate the temperature in Celsius
Temp_K = Temp_C + 273.15 #Temperature in Kelvin
return Temp_K
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment