New topic: 

Timing and port access in RB - Neuroscientist needs help!

<http://forums.realsoftware.com/viewtopic.php?t=46887>

         Page 1 of 1
   [ 5 posts ]                 Previous topic | Next topic          Author  
Message        GeoffBird          Post subject: Timing and port access in RB - 
Neuroscientist needs help!Posted: Sat Feb 09, 2013 7:28 pm                      
   
Joined: Sat Feb 09, 2013 6:34 pm
Posts: 2                Dear All,
I would be incredibly grateful for any help you can give. I am a neuroscientist 
who used to work in VB6 and now uses Matlab but Matlab seems not to do what I 
need so I wondered about Real Basic. In essence, I will be connecting a 
'parallel port' (via an express card to parallel converter) through an 
amplifier into someone's brain. These are chronic pain patients and we need to 
record their brain activity to an image we present. So I want my program to do 
this:

Present a picture
Change the value of the parallel port.

The idea is that the changing value of the parallel port will act as an event 
marker of when we showed the patient the image. My problem is that I need this 
to to be exact. Variance in the recorded time between the appearance of the 
image and the changing port value will be disastrous. Do you know if RB can 
handle it?

In particular, how does the program execute? Is line 2 called after line 1 
(i.e. potentially before the image has loaded and been displayed), or is line 2 
only executed after the image has been displayed?

Thanks a million for any help you can give.   
                             Top                charonn0          Post subject: 
Re: Timing and port access in RB - Neuroscientist needs helpPosted: Sat Feb 09, 
2013 8:31 pm                                 
Joined: Mon Apr 02, 2007 2:08 am
Posts: 1113
Location: San Francisco, CA, USA                GeoffBird wrote:Dear All,
In essence, I will be connecting a 'parallel port' (via an express card to 
parallel converter) through an amplifier into someone's brain [...]
The idea is that the changing value of the parallel port will act as an event 
marker of when we showed the patient the image. 

I've never tried using RB with a parallel port; I know RB supports serial 
ports, but I don't know if that has any bearing on working with parallel ports.

Quote:My problem is that I need this to to be exact. Variance in the recorded 
time between the appearance of the image and the changing port value will be 
disastrous. Do you know if RB can handle it?

In particular, how does the program execute? Is line 2 called after line 1 
(i.e. potentially before the image has loaded and been displayed), or is line 2 
only executed after the image has been displayed?


RB applications are single-threaded, non-reentrant applications. What this 
means is that line N+1 of a program cannot be executed until line N has 
executed, e.g.:
Dim Number1 As Integer  'Number1 is 0
Dim Number2 As Integer  'Number2 is 0
Number1 = 5
Number2 = 3
Dim sum As Integer = Number1 + Number2  'sum will always be 8


However, this doesn't necessarily mean that line N immediately precedes line 
N+1, merely that one always comes after the other. Graphical applications need 
to be able to respond to messages from the system, so RB will automatically 
"yield" to the RB framework unless you explicitly tell it not to. If your 
experiment requires response times faster than about 10 milliseconds* then RB 
may not be appropriate.


*This is just an estimate, I've never measured how response times are affected 
by the RB framework.      
_________________
Boredom Software  
                             Top                GeoffBird          Post 
subject: Re: Timing and port access in RB - Neuroscientist needs helpPosted: 
Sat Feb 09, 2013 8:36 pm                         
Joined: Sat Feb 09, 2013 6:34 pm
Posts: 2                Thanks very much for this. I'm reassured that the 
parallel value won't change before the picture is presented but I'm now worried 
that the port value may change anything from 1 to 10 milliseconds later. Is 
there any way of assigning RB greater priority or anything? I'm intrigued by 
"so RB will automatically "yield" to the RB framework unless you explicitly 
tell it not to" - coudl you elaborate please?
Many thanks indeed,
Geoff   
                             Top                charonn0          Post subject: 
Re: Timing and port access in RB - Neuroscientist needs helpPosted: Sat Feb 09, 
2013 9:48 pm                                 
Joined: Mon Apr 02, 2007 2:08 am
Posts: 1113
Location: San Francisco, CA, USA                You can't change the relative 
priority of your code vs the RB framework*. What you can do is tell RB not to 
yield to the framework by using using the Backgroundtasks #pragma directive. 
For example:

Sub ImportantTask()
  
  'Yielding is always on automatically
  #pragma BackgroundTasks False 'Turn off yielding
  'Yielding is now off
  For i As Integer = 0 To 99999
  'Do something
  Next
  
End Sub  ' Yielding back on automatically


The BackGroundTasks pragma directive takes effect immediately for the method in 
which it's used, and loses effect when the method exits or you explicitly 
re-enable it. Note that while disabling background tasks can speed up certain  
(but not all) code, doing so will prevent the user-interface from working (e.g. 
detecting button clicks and drawing pictures) until the framework (i.e. 
backgroundtasks) is allowed to run again.


*Not 100% true: RB includes the Thread class which can be used for cooperative 
threading, the relative priority of each thread can be set independently. 
However, I don't think using threads will actually save you any milliseconds 
given the app description.      
_________________
Boredom Software  
                             Top                taylor-design          Post 
subject: Re: Timing and port access in RB - Neuroscientist needs helpPosted: 
Sat Feb 09, 2013 10:20 pm                         
Joined: Wed Mar 22, 2006 11:15 am
Posts: 544
Location: Southern California                If timing is that critical, you 
need to keep in mind that any modern user OS has a million things going on and 
schedules them all as it sees fit. Thread yielding only occurs at well defined 
times in RB, and can be turned off. But the OS can preempt the entire 
application at any time to attend to anything it wants. By the same token 
whatever class or API you use to read a port has to go through the OS and 
driver, and those may introduce additional timing issues.

There is literature about this online. And there are various APIs which provide 
for more reliable and consistent execution of code. But you can't guarantee it 
beyond a certain resolution on Mac OS X or Windows.

That said...depending on the resolution you need, it may be sufficient to 
simply disable background tasks and give your application a higher priority on 
the host OS.      
_________________
Daniel L. Taylor
Custom Controls for Real Studio WE!
Visit: http://www.webcustomcontrols.com/  
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 5 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to