Reading out a Forp via IOPort on a Win Xp ?

Hey, 

I am trying to read out the signal of a Forp response box (HH-2x2) via the serial port of the forp interface FIU-005, program 0, via serial port, on windows xp/ Matlab R2007b.

The problem is the following: If I use PsychSerial, I can read out the signal, but any flip command less than 200ms following the read out will be extremely inaccurate (in the extreme ca. 200ms). Example snippet:

serial_port = PsychSerial('Open','COM3','foobar',19200)
while true
  trigger_on = PsychSerial('Read',serial_port);
  if strcmp(trigger_on,'4')
  disp('serial signal received');
  break
  end
end

If I use IOPort, then I cannot read out any signal at all, or sometimes I get a vector of numbers which have been pressed in the far past. Example snippet: 

[handle, errmsg] = IOPort('OpenSerialPort', 'Com3' , 'BaudRate=19200');
IOPort('Purge', handle);
while true
  [signal when, errmsg] = IOPort('Read', 0);
  if signal ~= []
  disp('serial signal received');
  break
  end
  WaitSecs(0.004);
end
IOPort('Close',0)

In general i would like to use IOPort much more than PsychSerial, as with another Response Box on another Scanner IOPort worked well and I did not have any timing issues with Flips after it. 

Any comments or ideas of how to solve that problem on Win XP are highly appreciated. 

Best, 

Radek


Not sure what you mean with your timing issues,
but if you just want to fetch a byte from the serial
port:

% Beginning of script:
[handle, errmsg] = IOPort('OpenSerialPort', 'Com3' , 'BaudRate=19200');

% Before start of response period we readout and
% discard any stale keypresses. IOPort('Purge', handle);
% would do the same, but doesn't hurt to test this
% as well in case your machine has some OS bug:
IOPort('Read', handle);

% Sleep until at least one byte of data becomes
% available, then return it:
while 1
[signal, when, errmsg] = IOPort('Read', handle, 1, 1);
if isempty(signal)
disp('Timeout exceeded while waiting for button press!');
else
% 'signal' contains exactly one byte of forp data:
break;
end
end;

% and so on...

This will not consume cpu time while waiting for
data to arrive, so can't cause any timing problems
that might be related to overloading the cpu.

See the optional 'ReceiveTimeout' parameter for
'OpenSerialPort' for setting the timeout. Default
is 1 second, a higher value probably makes more
sense for waiting for subject response.

-mario

--- In psychtoolbox@yahoogroups.com, "radoslaw martin cichy" <rmcichy@...> wrote:
>
> Hey,
>
> I am trying to read out the signal of a Forp response box (HH-2x2) via the
> serial port of the forp interface FIU-005, program 0, via serial port, on
> windows xp/ Matlab R2007b.
>
> The problem is the following: If I use PsychSerial, I can read out the
> signal, but any flip command less than 200ms following the read out will be
> extremely inaccurate (in the extreme ca. 200ms). Example snippet:
>
> serial_port = PsychSerial('Open','COM3','foobar',19200)
> while true
> trigger_on = PsychSerial('Read',serial_port);
> if strcmp(trigger_on,'4')
> disp('serial signal received');
> break
> end
> end
>
> If I use IOPort, then I cannot read out any signal at all, or sometimes I
> get a vector of numbers which have been pressed in the far past. Example
> snippet:
>
> [handle, errmsg] = IOPort('OpenSerialPort', 'Com3' , 'BaudRate=19200');
> IOPort('Purge', handle);
> while true
> [signal when, errmsg] = IOPort('Read', 0);
> if signal ~= []
> disp('serial signal received');
> break
> end
> WaitSecs(0.004);
> end
> IOPort('Close',0)
>
> In general i would like to use IOPort much more than PsychSerial, as with
> another Response Box on another Scanner IOPort worked well and I did not
> have any timing issues with Flips after it.
>
> Any comments or ideas of how to solve that problem on Win XP are highly
> appreciated.
>
> Best,
>
> Radek
>
Salve Mario,
I updated Psychtoolbox to the newest version. The Situation is the
same: I cannot read with IOPort, I can read with PsychSerial.

Further, n = IOPort('BytesAvailable', handle); always gives me a 0.

By the way, whether I use a FTDI USB Serial Emulator or the real FORP
does not make a difference in this regard.

Best,
Radek
Hmm. If 'BytesAvailable' always gives you zero,
then nothing is received at all from the serial
port, which would hint to a misconfiguration.

Add the parameter HardwareBufferSizes=16,16

to the open call like this:

[handle, errmsg] = IOPort('OpenSerialPort', 'Com3' , 'HardwareBufferSizes=16,16 BaudRate=19200 ');

This will set the hardware receive buffer to
a ridicously low setting of 16 bytes, which
is what the old PsychSerial driver does.

We don't set this size at all by default, because
according to Microsoft docu it is optional,
and rather harmful if done wrong. But who
knows, maybe your serial port needs this?

Another option to test would be...
'FlowControl=Hardware'

by default we set it to None, PsychSerial
doesn't touch it at all. I'd expect None to
be the proper setting, but who knows?

You could also look in the windows device
manager what kind of settings are set after
a successfull run with PsychSerial.

What do you mean with this statement?

> By the way, whether I use a FTDI USB Serial Emulator or the real FORP
> does not make a difference in this regard.

-mario

--- In psychtoolbox@yahoogroups.com, "radekcichy" <radekcichy@...> wrote:
>
> Salve Mario,
> I updated Psychtoolbox to the newest version. The Situation is the
> same: I cannot read with IOPort, I can read with PsychSerial.
>
> Further, n = IOPort('BytesAvailable', handle); always gives me a 0.
>
> By the way, whether I use a FTDI USB Serial Emulator or the real FORP
> does not make a difference in this regard.
>
> Best,
> Radek
>
Thanks for the feedback.
-mario

--- In psychtoolbox@yahoogroups.com, "radekcichy" <radekcichy@...> wrote:
>
> hey mario,
> i got the Forp to work.
>
> configstr= 'DataBits=8 Parity=None StopBits=1 DTR=1 RTS=1 FlowControl=Hardware HardwareBufferSizes=16,16 BaudRate=19200'
>
> [handle, errmsg] = IOPort('OpenSerialPort', 'COM3' , configstr);
>
> IOPort('Read', handle);
>
>
> It was the DTR and RTS which have to be set.
>
> Thanks a lot, i appreciate it!
> Radek
>
> --- In psychtoolbox@yahoogroups.com, "Mario Kleiner" <mario.kleiner@> wrote:
> >
> > Ja, that's worth a try. Setting the DTR=1 and RTS=1
> > in the config string for the open function.
> >
> > IOPorts default settings can be found in
> > IOPort OpenSerialPort?
> >
> > It does set baudrate 9600 bits, 8 databits, 1 stopbit,
> > no parity, no flow control by default, as these are
> > the most common settings. It doesn't touch the
> > DTR and RTS settings, just uses whatever the os
> > default is, so if these would be wrong, it might
> > cause communication failure. The Matlab serial
> > object sets RTS and DTR to 1 by default.
> >
> > PsychSerial only sets your given baudrate, and
> > hard-codes parity to none, 8 databits, 1 stopbit,
> > receive- and sendbuffersize = 16 bytes. It ignores
> > all other settings and commands - the help text
> > mostly describes options for PsychSerial on MacOS-9,
> > the Windows implementaiton is much more primitive.
> >
> > The other problem is that there is a serious memory
> > corruption bug in the setup code of PsychSerial which
> > means that in practice it is completely unknown and
> > unpredictable what it really sets on a given Windows
> > system and Matlab version. It may set the settings
> > mentioned above, or only part or none of them. It
> > may also initialize random other settings with random
> > trash values. Depending on driver and hardware, the
> > operating system may or may not ignore or accept
> > part of these settings.
> >
> > So the only information that we get
> > from success with PsychSerial is that there exists at
> > least one unknown set of settings that can make your
> > device work somewhat.
> >
> > IOPort has been extensively tested with FTDI
> > USB-Serial port drivers on all operating systems,
> > so i'm confident that at least that should work
> >
> > What response button boxes did you test?
> >
> > Specifically the RTBox from Xiangrui Li and the
> > Cedrus RB-x30 response pads work (If one
> > can actually use the term "work" for any Cedrus
> > response box). Both devices however don't use
> > flow control or DTR & RTS lines, so the DTR and
> > RTS proposal of Erik is worth a try.
> >
> > What is the Matlab output of IOPort if you use it?
> > Aren't there any error messages? Does it make
> > a difference if you write 'Com3' vs. 'COM3'? Does
> > it make a difference if you restart Matlab or reboot
> > the machine between tries? Are you absolutely
> > sure that you use the latest IOPort driver, i.e. does
> > "which IOPort" really point to the expected file
> > location and is the creation/modification date on that
> > file (if you check with Windows Explorer) what you
> > would expect?
> >
> > Either the problem is something embarassingly
> > trivial of the category "did you turn it off and on again?",
> > "did you actually plug it in?", or it is Eriks DTR and RTS
> > idea.
> >
> > If everything else fails, we could go low-level:
> >
> > <http://www.serial-port-monitor.com/>
> >
> > Provides an app called free-serial-port-monitor.exe.
> > They have a limited free trial version that should be
> > sufficient to observe the low level data exchange between
> > the application and the windows kernel level driver. You'd
> > need to learn how to use that application to create logs
> > for both successfull comm. via PsychSerial and for IOPort,
> > or i'd need remote access to your computer (and you
> > to live in a compatible time zone).
> >
> > -mario
> >
> >
> > --- In psychtoolbox@yahoogroups.com, "e_flister" <e_flister@> wrote:
> > >
> > > can't remember if the symptoms were like yours, but to read from a
> > > parallax rfid reader, i had to set stopbits and the 'data terminal
> > > ready' bit like so:
> > >
> > > port='COM1';
> > > config='BaudRate=2400 DataBits=8 Parity=None StopBits=1 DTR=1';
> > > IOPort('OpenSerialPort',port,config);
> > >
> > > i'm thinking it might be relevant to the difference you're seeing with
> > > psychserial, because in this case matlab's serial object apparently
> > > assumes those settings but IOPort doesn't (the following works):
> > > serial('COM1','BaudRate',2400,'Timeout',.0001);
> > >
> > > maybe the same is also true of psychserial? psychserial's help says:
> > > You can control many of the configuration parameters, including
> > > two of the handshaking lines (DTR and RTS). See PsychSerial Params.
> > > but i can't figure out how to get more information.
> > >
> > > -e
> > >
> >
>