Welcome Guest,Register Now
Log In

ANT Forum

Welcome guest, please Login or Register

You are here: Forum Home → ANT+ Forums → ANT+ Simulation Tools → Thread

   

Programmatically getting device number in SimulANT+

Rank

Total Posts: 16

Joined 2014-04-09

PM

I thought it should be fairly straightforward to get a device number through the IronPython script in SimulANT+. Apparently this is more difficult than I had expected. As per my post in Off-the-shelf interface to log data (to PC or mobile phone), I am using a PC to interface with a couple of heart-rate monitors via an ANTUSB-m dongle and SimulANT+.

Based on one of the demo scripts for another type of device, I added the following code into the supplied hr_DisplayCSV.py file:
# Define a function to handle the SensorFound event
def SensorFound(deviceNumbertransType):
    global 
storedDeviceNumber
    
global storedTransType
    
if storedDeviceNumber != deviceNumber:
        
logScriptEvent("New device found")
        
logger.writerow('test1')
    else:
        
logScriptEvent("Old device found again")
        
logger.writerow('test2')
    
storedDeviceNumber deviceNumber
    storedTransType 
transType 

(Maybe not the prettiest code, but it seemed to follow some sort of logic.)
However, AFAIK this never gets called, so AFAIK the SensorFound event never occurs in my use case. Evidently I'm missing something, so I'd be glad of any pointers.
—DIV      
Avatar
RankRankRankRank

Total Posts: 662

Joined 2012-10-09

PM

The help file "SimulANT+ Scripting Interface.chm" includes a description of all of the methods and properties that are available within the scripting interface for each type of device.

The code snippet you are posting seems correct, and matches the signature of the HeartRateDisplay.SensorFound event. Are you getting any errors when you try to run the entire script? Could you post your entire script?      
Rank

Total Posts: 16

Joined 2014-04-09

PM

Hello, Alejandra.
I'll post the script with irrelevant parts blanked out.
Also, I'll include one line that I know doesn't work, just to illustrate something else I tried:

# This script logs raw data received from a heart rate sensor to a CSV file

currVer '2.01.00'  # Current version number, as a string
verDate '2014-06-19'  # Date of current version, as a string

# It needs to access IronPython modules to use the CSV writer
import clr
clr
.AddReferenceToFile('IronPython.Modules.dll')
from IronPython
.Modules.PythonCsvModule import *

import time;  # This is required to include time module.
import datetime;  # This is required to include datetime module.

# Produce current date and time, to millisecond precision, in string format of varying verbosity
def getDateAndTime(doVerbose):
 
[...]
 
return sTs

ASCII1 
[...]
ASCII2 
[...]

# Produce suffix for filename based on current date and time, in string format
def getFileSuffix():
 
[...]
 
return suffix

kickOff 
getDateAndTime(1)
logScriptEvent(' ')
logScriptEvent''.join(['*** Session started on 'kickOff' ***']) )

# Prepare a file stored in same directory as main simulator executable
fName ''.join(['hr_log'getFileSuffix(), '.csv'])
open(fName 'wb')
logScriptEvent''.join(['*** Logging to 'fName ' in SimulANT+ directory ***']) )
logScriptEvent(' ')

logger writer(fdelimiter=','quotechar='|'quoting=QUOTE_MINIMAL)
logger.writerow(['H E A R T   R A T E   L O G G I N G'])
[...]
logger
.writerow(['----------''-----------''----------''----------'])

simulator.TurnOn()

# This function gets called everytime data is received; will be logging raw data
def DataPageReceived(pagekey):   
   
logScriptEvent("Ahoy, data was received!")
   
logScriptEvent(storedDeviceNumber)
   
logger.writerow([page.HeartBeatEventTimepage.HeartBeatCountpage.ComputedHeartRategetDateAndTime(0)])

# Stop execution of the script, called when user presses the "Stop" button
def stopScript():
   
simulator.TurnOff()
   
f.close()   # close file handle

# Define a function to handle the SensorFound event
def SensorFound(deviceNumbertransType):
    global 
storedDeviceNumber
    
global storedTransType
    
if storedDeviceNumber != deviceNumber:
        
logScriptEvent("Hey, I found a new device!")
 
logger.writerow('test1')
    else:
        
logScriptEvent("Hah, I found the old device again!")
 
logger.writerow('test2')
    
storedDeviceNumber deviceNumber
    storedTransType 
transType

# REFERENCES
[...


None of the ellipted methods refer to any global variables, so I don't suspect they are causing any problems.
I also ellipted a few additional lines of logger.writerow(·) that likewise shouldn't affect anything else.

Besides the snippet posted previously, I'll mention two other lines:
(1) import datetime; # This is required to include datetime module.
This line of code relates to the issue of accurately getting a local time stamp. I will post to another question. (Looking at the code again, now I wonder if it needs to be reformulated as "from IronPython.Modules [...]".)
(2) logScriptEvent(storedDeviceNumber)
This line of code produces an error message, "Error: global name 'storedDeviceNumber' is not defined". Hence, the code that I actually use at the moment does not include this line. Just to be clear: when this line is not included, no errors are reported, and I can log the data to file without any problem.

Regards,
David      
Avatar
RankRankRankRank

Total Posts: 662

Joined 2012-10-09

PM

To use global variables in Python/IronPython, you must define the variable in the outer scope. More details on the keyword global here:

http://www.python-course.eu/global_vs_local_variables.php


#Define global variable for storing device number
storedDeviceNumber 0

# This function gets called everytime data is received; will be logging raw data
def DataPageReceived(pagekey):   
   global 
storedDeviceNumber
   logScriptEvent
(storedDeviceNumber)

# Define a function to handle the SensorFound event
def SensorFound(deviceNumbertransType):
    global 
storedDeviceNumber
    
if storedDeviceNumber != deviceNumber:
        
logScriptEvent("Hey, I found a new device!"