You are here: Forum Home → ANT+ Forums → ANT+ Simulation Tools → Thread
f = open('filename.txt', 'w')
f.write('Heart Rate: ' + page.ComputedHeartRate + '\n')
# This script logs raw data received from a heart rate sensor to a CSV file
# It needs to access IronPython modules to use the CSV writer
import clr
clr.AddReferenceToFile('IronPython.Modules.dll')
from IronPython.Modules.PythonCsvModule import *
f = open('hr_log.txt', 'a') # file stored in same directory as main simulator executable
##logger = writer(f, delimiter=',', quotechar='|', quoting=QUOTE_MINIMAL)
#logger.writerow(['Event Time', 'Event Count', 'Heart Rate'])
f.close()
simulator.TurnOn()
# This function gets called everytime data is received; will be logging raw data
def DataPageReceived(page, key):
f = open('C:/hr_log.txt', 'a') # file stored in same directory as main simulator executable
logger = writer(f, delimiter=',', quotechar='|', quoting=QUOTE_MINIMAL)
logger.writerow([page.HeartBeatEventTime, page.HeartBeatCount, page.ComputedHeartRate])
f.close()
# Stop execution of the script, called when user presses the "Stop" button
def stopScript():
simulator.TurnOff()
f.close()
# This script logs raw data received from a heart rate sensor to a CSV file
# It needs to access IronPython modules to use the CSV writer
import clr
clr.AddReferenceToFile('IronPython.Modules.dll')
from IronPython.Modules.PythonCsvModule import *
# EDIT: this variable will be used for every call to open()
# when creating scripts that will create output to different files, this is the only line that will need to be edited
fileName = 'hr_log.txt'
f = open(fileName, 'a') # file stored in same directory as main simulator executable
##logger = writer(f, delimiter=',', quotechar='|', quoting=QUOTE_MINIMAL)
#logger.writerow(['Event Time', 'Event Count', 'Heart Rate'])
f.close()
simulator.TurnOn()
# This function gets called everytime data is received; will be logging raw data
# EDIT: indenting
def DataPageReceived(page, key):
f = open(fileName, 'a') # file stored in same directory as main simulator executable
logger = writer(f, delimiter=',', quotechar='|', quoting=QUOTE_MINIMAL)
logger.writerow([page.HeartBeatEventTime, page.HeartBeatCount, page.ComputedHeartRate])
f.close()
# Stop execution of the script, called when user presses the "Stop" button
def stopScript():
simulator.TurnOff()
f.close()
Hi there! Thanks for posting your code.
I have some small tweaks to suggest in your script, and I can confirm for you that you will be able to receive from both HRMs, and log them separately.
In your posted script, be careful of indenting in the DataPageReceived function, and make sure that the calls to open() point to the same file. At the moment, the open() call inside DataPageReceived points to C:/hr_log.txt instead of a text file in the same folder as the SimulANT+ executable. To more easily manage this, I would suggest storing the file path in a string variable, and using that string in the open() call.
Here's my suggested edits...
# This script logs raw data received from a heart rate sensor to a CSV file
# It needs to access IronPython modules to use the CSV writer
import clr
clr.AddReferenceToFile('IronPython.Modules.dll')
from IronPython.Modules.PythonCsvModule import *
# EDIT: this variable will be used for every call to open()
# when creating scripts that will create output to different files, this is the only line that will need to be edited
fileName = 'hr_log.txt'
f = open(fileName, 'a') # file stored in same directory as main simulator executable
##logger = writer(f, delimiter=',', quotechar='|', quoting=QUOTE_MINIMAL)
#logger.writerow(['Event Time', 'Event Count', 'Heart Rate'])
f.close()
simulator.TurnOn()
# This function gets called everytime data is received; will be logging raw data
# EDIT: indenting
def DataPageReceived(page, key):
f = open(fileName, 'a') # file stored in same directory as main simulator executable
logger = writer(f, delimiter=',', quotechar='|', quoting=QUOTE_MINIMAL)
logger.writerow([page.HeartBeatEventTime, page.HeartBeatCount, page.ComputedHeartRate])
f.close()
# Stop execution of the script, called when user presses the "Stop" button
def stopScript():
simulator.TurnOff()
f.close()
After the above modification, I was able to collect data the way you described. I used a single USBm, and configured two Heart Rate Displays on two of its channels to listen to two different HRMs. I then made a copy of your script, changed the output filename, and changed the name of the script file. I ran the two different scripts on each of the Heart Rate Displays, and found two separate log files in my SimulANT+ folder.
Don't hesitate to post any further questions you may have!
Jules
def DataPageReceived(page, key):
def DataPageReceived(page):