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

第十一期 fNIRS Journal Club 通知 2020/8/29,10am

北京时间2020年8月29日周六上午10点, 深圳大学成晓君助理教授将为大家讲解她去年发表的一篇用近红外超扫描揭示人际协调的神经机制的文章。同时,她还会和大家介绍如何用Granger causality分析两个信号的因果关系。欢迎大家参加并参与讨论。 她要讲的文献如下: Cheng, Pan, Hu, Hu (2019) Coordination Elicits Synchronous Brain Activity Between Co-actors: Frequency Ratio Matters Frontiers in neuroscience 13() 1071 Abstract: People...
Xu Cui
48 sec read

第十期 fNIRS Journal Club 视频

在2020/7/25日, 北京航空航天大学的汪待发副教授讲解了他发表的一篇BCI文章。视频如下: Youtube:https://youtu.be/gAQQrmbWSOcYouku:https://v.youku.com/v_show/id_XNDc2OTAxNzkzMg==.html
Xu Cui
4 sec read

第十期 fNIRS Journal Club 通知 2020/7/25,10am

北京时间2020年7月25日周六上午10点,北京航空航天大学的汪待发副教授,博士生导师,将为大家讲解他们组去年发表的一篇脑机交互(BCI)的近红外文章。欢迎大家参加并参与讨论。 他要讲的文献如下:Y. Zheng,D. Zhang, L. Wang, Y. Wang, H. Deng, S. Zhang, D. Li, D. Wang, “Resting-State-Based Spatial Filtering for an fNIRS-Based Motor Imagery...
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 *