Using serial port to control ETG4000

2 min read

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


Don't want to miss new papers in your field? Check out Stork we developed:

nirs2img, create an image file from NIRS data

I was asked where to get nirs2img script. Here it is. The download link is at the bottom of this article. nirs2img is to...
Xu Cui
51 sec read

Phrase frequency analysis on fNIRS hyperscanning with Stork Big…

In the previous blog, we have used Stork’s big analysis to analyze the literature of fNIRS. At that time, Stork only analyzed the frequency...
Xu Cui
1 min read

9 Replies to “Using serial port to control ETG4000”

  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. I mean the “Presentation” Software, that was perhaps a bit ambiguos in my above comment :-)) Sorry.

  3. 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?

  4. @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.

  5. 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~

Leave a Reply

Your email address will not be published. Required fields are marked *