Skip to content

Instantly share code, notes, and snippets.

@phsheth
Last active January 28, 2018 17:17
Show Gist options
  • Save phsheth/0fa6caac5b008e938a664ea24b5d7b72 to your computer and use it in GitHub Desktop.
Save phsheth/0fa6caac5b008e938a664ea24b5d7b72 to your computer and use it in GitHub Desktop.
Read Arduino serial port data using Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import serial\n",
"import time\n",
"import string\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"from matplotlib import style\n",
"import matplotlib.animation as animation\n",
"ser = serial.Serial('COM3', 9600) #read data from port COM3, similar command in linux/ubuntu - COM3 gets replaced by /dev/ttyUSB0 equivalent"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
],
"source": [
"dataoutput = []\n",
"while True:\n",
" ser.flushInput()\n",
" rs = []\n",
" read_serial = ser.readline()\n",
" read_serial_split = (str(read_serial)).split(\",\")\n",
" a1 = read_serial_split[1] #index 0 contains the annoying b' - hence ignored\n",
" a2 = read_serial_split[2]\n",
" a3 = read_serial_split[3]\n",
" a4 = read_serial_split[4]\n",
" a5 = read_serial_split[5]\n",
" a6 = read_serial_split[6]\n",
" a7 = read_serial_split[7]\n",
" rs = [a1,a2,a3,a4,a5,a6,a7]\n",
" dataoutput.append(rs)\n",
" dtop = np.transpose(np.array(dataoutput,dtype=float))\n",
" #print(dtop[:,0])\n",
" np.savetxt('dtop.csv',np.transpose(dtop),delimiter=',')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment