Home > brain, matlab, nirs > Using serial port to control ETG4000

Using serial port to control ETG4000

February 25th, 2011

To synchronize NIRS recording and your stimuli (visual, auditory, button press etc), your presentation program needs to talk to the NIRS machine. Hitachi ETG4000 allows serial communication between the NIRS machine and an external computer. You can use either MatLab or Eprime to achieve this communication.

MatLab

If you use Matlab to deliver the stimuli, then controlling NIRS machine is very simple. Here is a minimum example.

In Matlab, you use the following commands to control the NIRS machine:

% Script SerialOT
% Serial control function for OTsystem using MATLAB
%
% Written by S.Kawasaki
% First completed 2006/04/30
%

clear all;

%/////////// Open MATLAB Serial Port //////////////
com1 = serial('COM1','BaudRate',9600,'DataBits',8,'Parity','none','StopBits',1);
fopen(com1);

%//////////////// Command List //////////////////
st   = sprintf('ST\r\n');%Start
mark1 = sprintf('A \r\n');%Mark A
mark2 = sprintf('B \r\n');%Mark B
mark3 = sprintf('C \r\n');%Mark C
mark4 = sprintf('D \r\n');%Mark D
ed   = sprintf('ED\r\n');%End

%/////////// Send to Commands for OT /////////////
fwrite(com1,st);disp('Send ST');    %Start
pause(10);                          %10s Prescan
pause(5);                          %5s Rest

for time=1:5
    fwrite(com1,mark1);disp('Send A ');  %Mark A Start
    pause(5);                          %5s Task
    fwrite(com1,mark1);disp('Send A ');  %Mark A End
    pause(5);                          %5s Rest
end

fwrite(com1,ed);disp('Send ED');    %End

%//////////// Close Serial Port /////////////////
fclose(com1);
%delete(com1);
clear com1;clear st;clear mark;clear ed;

EPrime

To allow your EPrime script to control ETG-4000, you may follow the instructions below:

First, make sure “Port” and “SRBOX” are not checked under tab “Devices” in window “Properties: Experiment Object Properties”.

Then,
1. Insert an “InLine” object into the beginning of your experiment procedure and copy the ST.txt script into the object; Rename the object to ‘StartETG’
2. Insert a “Wait” object right after “StartETG” and change the duration to 20 seconds. This step is necessary!
3. Insert an “InLine” object into the end of your experiment procedure and copy the ED.txt script into the object; Rename the object to ‘EndETG’

Hitachi also allows EPrime to send the marker information (i.e., event timing information) to ETG4000. To achieve that, you
1. Insert an “InLine” object into the place where you want to put a marker. This is usually when a trial starts and ends. Copy F1.txt into that object. You may need to change the marker String (here “F1″). If you are measuring in stim mode, then change the marker string to “A ” (note, ‘A’ and space), or “B “, or “C “, …; If you measure in event mode, change to “F1″, “F2″, “F3″, … Different string corresponds different types of event.
2. Reopen the StartETG object and comment out the line “dim myPort as SerialDevice”
3. Copy “dim myPort as SerialDevice” to the User window in Script window.

Then run your script.

If you encourter errors such as “constant” or “variable” error, you can move (not copy) the variable declaration lines (below) to User Window.
const header2 as string = “A ” ‘ can be B, F1 etc; can be header3 etc
Dim header2Bytes(1) As Integer

For each type of event, use a different variable name such as header2, header3, header2Bytes etc.

Here are sample EPrime scripts to control NIRS machine.

START

' E-prime inLine code for Sending ST
'
' written by Shingo Kawasaki

'''''''''''''''Begin Code

const header as string = "ST"
Dim headerBytes(1) As Integer
headerBytes(0) = 2  ' This specifies the type of string (2=Marker)
headerBytes(1) = 1  ' This specifies the number of bytes

' initialize the serial port by creating a serialdevice object and setting its properties
dim myPort as SerialDevice
Set myPort = New SerialDevice
dim myPortInfo as SerialDeviceInfo
myPortInfo.CommPort = 1
myPortInfo.BaudRate = 9600
myPortInfo.DataBits = 8
myPortInfo.Parity = ebParityNo
myPortInfo.StopBits = ebStopBits1_0

'Open the port and flush the buffer
myPort.open myPortInfo
myPort.FlushOutBuffer

' End section...Port is now ready to send the packet

' Write the packet  itself
myPort.WriteString header  'Header string
myPort.FlushOutBuffer

' close the port and clear the object when you are finished
' (when the last packet has been sent)
'myPort.close
'Set myPort = Nothing

END

' E-prime inLine code for Sending ED
'
' written by Shingo Kawasaki

const header4 as string = "ED"
Dim header4Bytes(1) As Integer
header4Bytes(0) = 2  ' This specifies the type of string (2=Marker)
header4Bytes(1) = 1  ' This specifies the number of bytes

' Write the packet  itself
myPort.WriteString header4  'Header string
myPort.FlushOutBuffer

' close the port and clear the object when you are finished
' (when the last packet has been sent)
myPort.close
Set myPort = Nothing

F1

' E-prime inLine code for Sending F1
'
' written by Shingo Kawasaki

const header2 as string = "F1"
Dim header2Bytes(1) As Integer
header2Bytes(0) = 2  ' This specifies the type of string (2=Marker)
header2Bytes(1) = 1  ' This specifies the number of bytes

' Write the packet  itself
myPort.WriteString header2  'Header string
myPort.FlushOutBuffer

' close the port and clear the object when you are finished
' (when the last packet has been sent)
'myPort.close
'Set myPort = Nothing
Author: Xu Cui Categories: brain, matlab, nirs Tags:
Try Stork, a research tool we developed

Stork is a publication alert app developed by us at Stanford. As a researcher we often forget to follow up important publications - and it's practically impossible to search many keywords or researchers' names everyday. Stork can help us to search everyday and notifies us when there are new publications/grants. How Stork helped me?

About the author:

Xu Cui is a human brain research scientist in Stanford University. He lives in the Bay Area in the United States. He is also the founder of Stork (smart publication alert app), PaperBox and BizGenius.

 

He was born in He'nan province, China. He received education in Beijing University(BS), University of Tennessee (Knoxville) (MS), Baylor College of Medicine (PhD) and Stanford University (PostDoc). Read more ...
  1. Rototok
    April 19th, 2012 at 01:40 | #1

    Thank you for your great website. Do you by chance happen to have a code for using the serial port to control the Hitachi ETG 4000 by the presentation software? Thank you very much!

  2. Rototok
    April 19th, 2012 at 01:42 | #2

    I mean the “Presentation” Software, that was perhaps a bit ambiguos in my above comment :-)) Sorry.

  3. April 19th, 2012 at 09:06 | #3

    @Rototok
    Rototok -

    Unfortunately I have no experience in using Presentation.

    Xu

  4. anon
    May 14th, 2012 at 18:42 | #4

    Do you happen to have the ETG-4000 software manual? I have a lot of questions about triggering, but can’t find anybody with the manual.

    My most pressing question: are triggers necessarily strings “A” etc for blocks and “F1″ etc for events? Can integers be used?

  5. May 14th, 2012 at 20:32 | #5

    @anon
    In my experience, only “A” etc and “F1″ etc be used for markers for ETG4000. I never used integers.

    As to the 1st question, if you have ETG4000, the company should give you the manual.

  6. NiuXin
    May 31st, 2012 at 03:45 | #6

    Hi, Mr. Cui,
    I am writing a matlab script which needs to send marks to the ETG 4000 device. And I wonder if I can customize the string assigned to the mark(e.g.mark1 = sprintf(’Response\r\n’)). Should the string contain just 2 characters?
    Thanks for your attenion~

  7. May 31st, 2012 at 10:32 | #7

    @NiuXin
    I don’t think so. There are only a few strings you can use such as ST, ED, F1, F2.., A , B etc

CAPTCHA Image