Skip to content

Commit 879ef4d

Browse files
committed
Sweep the frequencies and return the mean value
1 parent 02f3797 commit 879ef4d

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

RFE_control.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#try:
2+
# while True:
3+
# input_value = input("please enter [G] for next step, [Q] for quit: ")
4+
# if input_value == "G":
5+
# print ("number is", num)
6+
# num += 1
7+
# continue
8+
# # input_value = input("please enter [G] for next step: ")
9+
# elif input_value == "Q":
10+
# break
11+
# else:
12+
# input_value = input("please enter [G] for next step: ")
13+
#except:
14+
# pass
15+
16+
17+
#=======================================================================================
18+
19+
import numpy as np
20+
import time
21+
from datetime import datetime
22+
import RFExplorer
23+
24+
25+
def PrintPeak(objAnalazyer):
26+
"""This function prints the amplitude and frequency peak of the latest received sweep
27+
"""
28+
nIndex = objAnalazyer.SweepData.Count-1
29+
objSweepTemp = objAnalazyer.SweepData.GetData(nIndex)
30+
nStep = objSweepTemp.GetPeakStep() #Get index of the peak
31+
fAmplitudeDBM = objSweepTemp.GetAmplitude_DBM(nStep) #Get amplitude of the peak
32+
fCenterFreq = objSweepTemp.GetFrequencyMHZ(nStep) #Get frequency of the peak
33+
34+
print("Sweep[" + str(nIndex)+"]: Peak: " + "{0:.3f}".format(fCenterFreq) + "MHz " + str(fAmplitudeDBM) + "dBm")
35+
36+
return fAmplitudeDBM
37+
#---------------------------------------------------------
38+
# global variables and initialization
39+
#---------------------------------------------------------
40+
41+
SERIALPORT = None #serial port identifier, use None to autodetect
42+
BAUDRATE = 500000
43+
44+
objRFE = RFExplorer.RFECommunicator() #Initialize object and thread
45+
TOTAL_SECONDS = 5 #Initialize time span to display activity
46+
47+
#---------------------------------------------------------
48+
# Main processing loop
49+
#---------------------------------------------------------
50+
51+
52+
try:
53+
#Find and show valid serial ports
54+
objRFE.GetConnectedPorts()
55+
56+
57+
#Connect to available port
58+
if (objRFE.ConnectPort(SERIALPORT, BAUDRATE)):
59+
#Reset the unit to start fresh
60+
objRFE.SendCommand("r")
61+
#Wait for unit to notify reset completed
62+
while(objRFE.IsResetEvent):
63+
pass
64+
#Wait for unit to stabilize
65+
time.sleep(3)
66+
67+
#Request RF Explorer configuration
68+
objRFE.SendCommand_RequestConfigData()
69+
#Wait to receive configuration and model details
70+
while(objRFE.ActiveModel == RFExplorer.RFE_Common.eModel.MODEL_NONE):
71+
objRFE.ProcessReceivedString(True) #Process the received configuration
72+
73+
while True:
74+
#create a list for saving the recorded values
75+
sample_value = []
76+
objRFE.ResetInternalBuffers()
77+
input_value = input("please enter [G] for next step, [Q] for quit: ")
78+
if input_value == "G":
79+
#If object is an analyzer, we can scan for received sweeps
80+
if (objRFE.IsAnalyzer()):
81+
print("Receiving data...")
82+
#Process until we complete scan time
83+
nLastDisplayIndex=0
84+
startTime=datetime.now()
85+
while ((datetime.now() - startTime).seconds<TOTAL_SECONDS):
86+
#Process all received data from device
87+
objRFE.ProcessReceivedString(True)
88+
#Print data if received new sweep only
89+
if (objRFE.SweepData.Count>nLastDisplayIndex):
90+
peak_value = PrintPeak(objRFE)
91+
sample_value.append(peak_value)
92+
nLastDisplayIndex=objRFE.SweepData.Count
93+
print ("The average values of the sampling values is:", round(np.mean(sample_value), 2), "dBm")
94+
else:
95+
print("Error: Device connected is a Signal Generator. \nPlease, connect a Spectrum Analyzer")
96+
continue
97+
elif input_value == "Q":
98+
break
99+
else:
100+
input_value = input("please enter [G] for next step: ")
101+
else:
102+
print("Not Connected")
103+
except Exception as obEx:
104+
print("Error: " + str(obEx))
105+
106+
#---------------------------------------------------------
107+
# Close object and release resources
108+
#---------------------------------------------------------
109+
110+
#objRFE.Close() #Finish the thread and close port
111+
objRFE = None

0 commit comments

Comments
 (0)