Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
RF Switch BOX
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SteeleLab
RF Switch BOX
Commits
ec854c1c
Commit
ec854c1c
authored
3 years ago
by
Jasper Franse
Browse files
Options
Downloads
Patches
Plain Diff
example coding
parent
e853ed81
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
example_code_switch.ipynb
+171
-0
171 additions, 0 deletions
example_code_switch.ipynb
with
171 additions
and
0 deletions
example_code_switch.ipynb
0 → 100755
+
171
−
0
View file @
ec854c1c
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "clean-marsh",
"metadata": {},
"outputs": [],
"source": [
"# import warnings\n",
"# warnings.filterwarnings('ignore')\n",
"\n",
"# import sys\n",
"# import os\n",
"# import shutil\n",
"# from pyrpl import Pyrpl\n",
"# import numpy as np\n",
"# import time\n",
"# import matplotlib.pyplot as plts\n",
"\n",
"# import IPython\n",
"# import ipywidgets as widgets\n",
"\n",
"# from bokeh.plotting import figure, show\n",
"# from bokeh.io import output_notebook, push_notebook\n",
"# from bokeh.layouts import column, row\n",
"# from bokeh.models import ColumnDataSource, Toggle, Range1d\n",
"\n",
"# from scipy.signal import welch\n",
"\n",
"# from datetime import datetime\n",
"\n",
"#Arduino Serial connection\n",
"import serial"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "variable-composite",
"metadata": {},
"outputs": [
{
"ename": "SerialException",
"evalue": "[Errno 2] could not open port COM9: [Errno 2] No such file or directory: 'COM9'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/opt/conda/lib/python3.8/site-packages/serial/serialposix.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 321\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 322\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mportstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mO_RDWR\u001b[0m \u001b[0;34m|\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mO_NOCTTY\u001b[0m \u001b[0;34m|\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mO_NONBLOCK\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 323\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'COM9'",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mSerialException\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-5-ccbc81474993>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mser\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mserial\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSerial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'COM9'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m9600\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mser\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreset_input_buffer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/conda/lib/python3.8/site-packages/serial/serialutil.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, port, baudrate, bytesize, parity, stopbits, timeout, xonxoff, rtscts, write_timeout, dsrdtr, inter_byte_timeout, exclusive, **kwargs)\u001b[0m\n\u001b[1;32m 242\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 243\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mport\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 244\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 245\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 246\u001b[0m \u001b[0;31m# - - - - - - - - - - - - - - - - - - - - - - - -\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/opt/conda/lib/python3.8/site-packages/serial/serialposix.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 323\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 324\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 325\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mSerialException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmsg\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0merrno\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"could not open port {}: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_port\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 326\u001b[0m \u001b[0;31m#~ fcntl.fcntl(self.fd, fcntl.F_SETFL, 0) # set blocking\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 327\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mSerialException\u001b[0m: [Errno 2] could not open port COM9: [Errno 2] No such file or directory: 'COM9'"
]
}
],
"source": [
"ser = serial.Serial('COM9',9600,timeout=0.5)\n",
"ser.flush()\n",
"\n",
"time.sleep(0.5) \n",
"ser.reset_input_buffer()\n",
"ser.write(b'at')\n",
"ser.write(b'ch1')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "rural-musical",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "vulnerable-leone",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "freelance-ticket",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "continuing-article",
"metadata": {},
"outputs": [],
"source": [
"# Global variables \n",
"R = []\n",
"T = []\n",
"tm = []\n",
"Iavg = []\n",
"Iptp = []\n",
"Vavg = []\n",
"Vptp = []\n",
"t0 = time.time()\n",
"timer_t0 = time.time()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "verbal-blame",
"metadata": {},
"outputs": [],
"source": [
"def get_temperature(serial_port, baudrate, timeout):\n",
" # serial_port='COM7', baudrate=9600, timeout=1\n",
" \n",
" #variables (could be adjusted to inputs?)\n",
" alpha = 4.1139e-3; R0 = 1000; R1=1081; Vin=3.272 #should be according to literature: alpha = 3.85e-3 & R0 = 1000\n",
" \n",
" #open Serial port to Arduino + flush what's still on it\n",
" ser = serial.Serial(serial_port,baudrate,timeout=0.5)\n",
" ser.flush()\n",
" \n",
" #sent command to Arduino so we can read it out:\n",
" time.sleep(0.5) #needs to sleep, otherwise we get a: 'can not convert float... at the Vout-line'\n",
" ser.reset_input_buffer()\n",
" ser.write(b'g')\n",
" \n",
" #Serial read + Temperature calculations:\n",
" serial_reply = ser.readline()\n",
" try:\n",
" Vout = float(str(serial_reply.decode(\"utf-8\")))*(Vin/1023.0) #calculates the voltage from the voltage divider (over the PT1000)\n",
" except:\n",
" return serial_reply\n",
" Rpt = (Vout*R1)/(Vin-Vout) #calculates the resistance of the PT1000\n",
" Temp_C = ((Rpt/R0)-1)/alpha #with the above calculated resistance and 'known' variable we calculate the temperature in Celsius\n",
" Temp_K = Temp_C + 273.15 #Temperature in Kelvin\n",
" return Temp_K"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% 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
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment