47 lines
1019 B
Python
47 lines
1019 B
Python
|
import serial
|
||
|
import re
|
||
|
import matplotlib.pyplot as plt
|
||
|
#connect to device, check if correct port
|
||
|
ser =serial.Serial('COM4',19200)
|
||
|
Bx=[]
|
||
|
By=[]
|
||
|
Bz=[]
|
||
|
x=[]
|
||
|
i=0
|
||
|
f=open('Data.txt','w')
|
||
|
fig, axs=plt.subplots(1,3)
|
||
|
|
||
|
#plug in number of measurement points (here 35)
|
||
|
#save all data in a txt sheet
|
||
|
for k in range(0,35):
|
||
|
|
||
|
a=ser.readline()
|
||
|
data=str(a,'utf-8')
|
||
|
points=re.findall(r'-?\d+\.?\d*', data)
|
||
|
Bx_point=float(points[0])
|
||
|
By_point=float(points[1])
|
||
|
Bz_point=float(points[2])
|
||
|
Bx.append(Bx_point)
|
||
|
By.append(By_point)
|
||
|
Bz.append(Bz_point)
|
||
|
x.append(i)
|
||
|
i+=1
|
||
|
print(Bx_point,By_point,Bz_point)
|
||
|
f.write(str(Bx_point)+'\t'+str(By_point)+'\t'+str(Bz_point)+'\n')
|
||
|
|
||
|
f.close()
|
||
|
|
||
|
|
||
|
#plot to get a first look at magnetic field (not calibrated)
|
||
|
axs[0].errorbar(x,Bx,1.615)
|
||
|
axs[0].set_title('Bx')
|
||
|
axs[0].set(ylabel='G')
|
||
|
axs[1].errorbar(x,By,1.615)
|
||
|
axs[1].set_title('By')
|
||
|
axs[1].set(ylabel='G')
|
||
|
axs[2].errorbar(x,Bz,1.615)
|
||
|
axs[2].set_title('Bz')
|
||
|
axs[2].set(ylabel='G')
|
||
|
print(By)
|
||
|
plt.show()
|