Skip to content

Instantly share code, notes, and snippets.

@heborras
Last active October 12, 2017 11:02
Show Gist options
  • Save heborras/3ee85aee16575391f46451dbd566b0ef to your computer and use it in GitHub Desktop.
Save heborras/3ee85aee16575391f46451dbd566b0ef to your computer and use it in GitHub Desktop.
his python script can record arbitrary data from a serial connection.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Serial recorder"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### This python script can record arbitrary data from a serial connection. For a set time even. Please note that the outputted data is likely formatted in mac line endings (because that is what Arduinos send). So if you want to use the data in another program, especially the analysis program, conversion to Linux line endings must be done. On Windows Notepad++ can do this.\n",
"### The script was often used in combination with an Arduino DUE firmware for recording events: https://gist.github.com/HenniOVP/09cb302eedfb100eca4fbe17489bded0 \n",
"### The python software for analyzing such outputs can be found here: https://gist.github.com/HenniOVP/b48d752b523b778235a2a62b49d0a2f5"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import serial\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# settings\n",
"recording_secs = 60*20\n",
"output_file = time.strftime(\"%Y%m%d-%H%M\")+\"_interrupt_output_\" + str(recording_secs) + \"_sec.txt\"\n",
"print(\"Output file name: \" + output_file)\n",
"serial_port = 'COM7'\n",
"baud_rate = 115200"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Output file name: 20171010-1252_interrupt_output_PMT_SiPM_GM_2400_sec.txt\n",
"Current number of read lines: 100\n",
"Seconds left: 2383.9\n",
"Current number of read lines: 200\n",
"Seconds left: 2367.5\n",
"Current number of read lines: 300\n",
"Seconds left: 2342.6\n",
"Current number of read lines: 400\n",
"Seconds left: 2322.3\n",
"Current number of read lines: 500\n",
"Seconds left: 2309.0\n",
"Current number of read lines: 600\n",
"Seconds left: 2293.9\n",
"Current number of read lines: 700\n",
"Seconds left: 2275.3\n",
"Current number of read lines: 800\n",
"Seconds left: 2259.1\n",
"Current number of read lines: 900\n",
"Seconds left: 2241.1\n",
"Current number of read lines: 1000\n",
"Seconds left: 2220.0\n",
"Current number of read lines: 1100\n",
"Seconds left: 2204.9\n",
"Current number of read lines: 1200\n",
"Seconds left: 2184.4\n",
"Current number of read lines: 1300\n",
"Seconds left: 2161.5\n",
"Current number of read lines: 1400\n",
"Seconds left: 2143.2\n",
"Current number of read lines: 1500\n",
"Seconds left: 2123.8\n",
"Current number of read lines: 1600\n",
"Seconds left: 2104.9\n",
"Current number of read lines: 1700\n",
"Seconds left: 2086.1\n",
"Current number of read lines: 1800\n",
"Seconds left: 2066.2\n",
"Current number of read lines: 1900\n",
"Seconds left: 2048.2\n",
"Current number of read lines: 2000\n",
"Seconds left: 2029.4\n",
"Current number of read lines: 2100\n",
"Seconds left: 2010.2\n",
"Current number of read lines: 2200\n",
"Seconds left: 1992.8\n",
"Current number of read lines: 2300\n",
"Seconds left: 1975.1\n",
"Current number of read lines: 2400\n",
"Seconds left: 1954.6\n",
"Current number of read lines: 2500\n",
"Seconds left: 1937.9\n",
"Current number of read lines: 2600\n",
"Seconds left: 1915.1\n",
"Current number of read lines: 2700\n",
"Seconds left: 1894.7\n",
"Current number of read lines: 2800\n",
"Seconds left: 1876.1\n",
"Current number of read lines: 2900\n",
"Seconds left: 1856.5\n",
"Current number of read lines: 3000\n",
"Seconds left: 1837.6\n",
"Current number of read lines: 3100\n",
"Seconds left: 1817.8\n",
"Current number of read lines: 3200\n",
"Seconds left: 1798.6\n",
"Current number of read lines: 3300\n",
"Seconds left: 1777.6\n",
"Current number of read lines: 3400\n",
"Seconds left: 1758.7\n",
"Current number of read lines: 3500\n",
"Seconds left: 1738.5\n",
"Current number of read lines: 3600\n",
"Seconds left: 1719.2\n",
"Current number of read lines: 3700\n",
"Seconds left: 1703.8\n",
"Current number of read lines: 3800\n",
"Seconds left: 1685.9\n",
"Current number of read lines: 3900\n",
"Seconds left: 1670.0\n",
"Current number of read lines: 4000\n",
"Seconds left: 1655.0\n",
"Current number of read lines: 4100\n",
"Seconds left: 1635.6\n",
"Current number of read lines: 4200\n",
"Seconds left: 1615.6\n",
"Current number of read lines: 4300\n",
"Seconds left: 1597.0\n",
"Current number of read lines: 4400\n",
"Seconds left: 1583.1\n",
"Current number of read lines: 4500\n",
"Seconds left: 1567.1\n",
"Current number of read lines: 4600\n",
"Seconds left: 1552.5\n",
"Current number of read lines: 4700\n",
"Seconds left: 1529.9\n",
"Current number of read lines: 4800\n",
"Seconds left: 1509.9\n",
"Current number of read lines: 4900\n",
"Seconds left: 1486.5\n",
"Current number of read lines: 5000\n",
"Seconds left: 1466.3\n",
"Current number of read lines: 5100\n",
"Seconds left: 1447.7\n",
"Current number of read lines: 5200\n",
"Seconds left: 1431.4\n",
"Current number of read lines: 5300\n",
"Seconds left: 1414.1\n",
"Current number of read lines: 5400\n",
"Seconds left: 1397.0\n",
"Current number of read lines: 5500\n",
"Seconds left: 1380.9\n",
"Current number of read lines: 5600\n",
"Seconds left: 1364.9\n",
"Current number of read lines: 5700\n",
"Seconds left: 1348.7\n",
"Current number of read lines: 5800\n",
"Seconds left: 1329.7\n",
"Current number of read lines: 5900\n",
"Seconds left: 1310.1\n",
"Current number of read lines: 6000\n",
"Seconds left: 1293.0\n",
"Current number of read lines: 6100\n",
"Seconds left: 1273.1\n",
"Current number of read lines: 6200\n",
"Seconds left: 1250.1\n",
"Current number of read lines: 6300\n",
"Seconds left: 1230.7\n",
"Current number of read lines: 6400\n",
"Seconds left: 1216.4\n",
"Current number of read lines: 6500\n",
"Seconds left: 1202.0\n",
"Current number of read lines: 6600\n",
"Seconds left: 1183.9\n",
"Current number of read lines: 6700\n",
"Seconds left: 1163.1\n",
"Current number of read lines: 6800\n",
"Seconds left: 1144.2\n",
"Current number of read lines: 6900\n",
"Seconds left: 1126.2\n",
"Current number of read lines: 7000\n",
"Seconds left: 1108.5\n",
"Current number of read lines: 7100\n",
"Seconds left: 1093.0\n",
"Current number of read lines: 7200\n",
"Seconds left: 1079.1\n",
"Current number of read lines: 7300\n",
"Seconds left: 1062.2\n",
"Current number of read lines: 7400\n",
"Seconds left: 1043.6\n",
"Current number of read lines: 7500\n",
"Seconds left: 1027.3\n",
"Current number of read lines: 7600\n",
"Seconds left: 1009.9\n",
"Current number of read lines: 7700\n",
"Seconds left: 993.4\n",
"Current number of read lines: 7800\n",
"Seconds left: 974.5\n",
"Current number of read lines: 7900\n",
"Seconds left: 959.0\n",
"Current number of read lines: 8000\n",
"Seconds left: 941.8\n",
"Current number of read lines: 8100\n",
"Seconds left: 926.7\n",
"Current number of read lines: 8200\n",
"Seconds left: 908.2\n",
"Current number of read lines: 8300\n",
"Seconds left: 889.2\n",
"Current number of read lines: 8400\n",
"Seconds left: 867.8\n",
"Current number of read lines: 8500\n",
"Seconds left: 849.0\n",
"Current number of read lines: 8600\n",
"Seconds left: 832.3\n",
"Current number of read lines: 8700\n",
"Seconds left: 817.5\n",
"Current number of read lines: 8800\n",
"Seconds left: 798.5\n",
"Current number of read lines: 8900\n",
"Seconds left: 780.3\n",
"Current number of read lines: 9000\n",
"Seconds left: 758.9\n",
"Current number of read lines: 9100\n",
"Seconds left: 743.2\n",
"Current number of read lines: 9200\n",
"Seconds left: 726.5\n",
"Current number of read lines: 9300\n",
"Seconds left: 703.8\n",
"Current number of read lines: 9400\n",
"Seconds left: 686.4\n",
"Current number of read lines: 9500\n",
"Seconds left: 664.5\n",
"Current number of read lines: 9600\n",
"Seconds left: 648.1\n",
"Current number of read lines: 9700\n",
"Seconds left: 633.4\n",
"Current number of read lines: 9800\n",
"Seconds left: 614.2\n",
"Current number of read lines: 9900\n",
"Seconds left: 594.3\n",
"Current number of read lines: 10000\n",
"Seconds left: 574.5\n",
"Current number of read lines: 10100\n",
"Seconds left: 558.6\n",
"Current number of read lines: 10200\n",
"Seconds left: 541.6\n",
"Current number of read lines: 10300\n",
"Seconds left: 522.7\n",
"Current number of read lines: 10400\n",
"Seconds left: 504.2\n",
"Current number of read lines: 10500\n",
"Seconds left: 486.1\n",
"Current number of read lines: 10600\n",
"Seconds left: 467.7\n",
"Current number of read lines: 10700\n",
"Seconds left: 447.6\n",
"Current number of read lines: 10800\n",
"Seconds left: 434.2\n",
"Current number of read lines: 10900\n",
"Seconds left: 420.3\n",
"Current number of read lines: 11000\n",
"Seconds left: 397.0\n",
"Current number of read lines: 11100\n",
"Seconds left: 379.9\n",
"Current number of read lines: 11200\n",
"Seconds left: 365.0\n",
"Current number of read lines: 11300\n",
"Seconds left: 349.6\n",
"Current number of read lines: 11400\n",
"Seconds left: 333.6\n",
"Current number of read lines: 11500\n",
"Seconds left: 316.8\n",
"Current number of read lines: 11600\n",
"Seconds left: 297.5\n",
"Current number of read lines: 11700\n",
"Seconds left: 276.6\n",
"Current number of read lines: 11800\n",
"Seconds left: 257.3\n",
"Current number of read lines: 11900\n",
"Seconds left: 239.2\n",
"Current number of read lines: 12000\n",
"Seconds left: 221.6\n",
"Current number of read lines: 12100\n",
"Seconds left: 203.0\n",
"Current number of read lines: 12200\n",
"Seconds left: 185.8\n",
"Current number of read lines: 12300\n",
"Seconds left: 163.9\n",
"Current number of read lines: 12400\n",
"Seconds left: 149.2\n",
"Current number of read lines: 12500\n",
"Seconds left: 130.7\n",
"Current number of read lines: 12600\n",
"Seconds left: 112.7\n",
"Current number of read lines: 12700\n",
"Seconds left: 93.8\n",
"Current number of read lines: 12800\n",
"Seconds left: 72.8\n",
"Current number of read lines: 12900\n",
"Seconds left: 52.9\n",
"Current number of read lines: 13000\n",
"Seconds left: 38.2\n",
"Current number of read lines: 13100\n",
"Seconds left: 21.9\n",
"Current number of read lines: 13200\n",
"Seconds left: 0.9\n",
"Finished recording! Read lines: 13207\n"
]
}
],
"source": [
"line_counter = 0\n",
"# start recording our serial port\n",
"with serial.Serial(serial_port, baud_rate, timeout=10) as ser:\n",
" start_time = time.time()\n",
" # write into output file\n",
" with open(output_file, 'w') as f:\n",
" # write as long as we defined\n",
" while (start_time + recording_secs) > time.time():\n",
" line = ser.readline() # read a '\\n' terminated line\n",
" # remove the \\n at the end\n",
" line = line.split('\\n')[0]\n",
" f.write(line)\n",
" line_counter += 1\n",
" if line_counter % 100 == 0:\n",
" print(\"Current number of read lines: \"+str(line_counter))\n",
" print(\"Seconds left: \" + str(round(start_time + recording_secs - time.time(), 1)))\n",
" f.flush()\n",
"print(\"Finished recording! Read lines: \"+str(line_counter))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [conda root]",
"language": "python",
"name": "conda-root-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment