-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBioSemiSerialPort.m
More file actions
137 lines (133 loc) · 5.16 KB
/
BioSemiSerialPort.m
File metadata and controls
137 lines (133 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
classdef BioSemiSerialPort
% BioSemiSerialPort - function send triggers to BioSemi EEG systems
% This function requires the purchase (or manufacture) of a USB to serial
% hardware device.
% https://www.biosemi.com/faq/USB%20Trigger%20interface%20cable.htm
%
% Syntax: sp = BioSemiSerialPort();
%
% Inputs: none.
%
% Outputs: sp - serial port object
%
% Example:
% sp = BioSemiSerialPort(); % open serial port
% sp.testTriggers; % test pins 1-8
% sp.sendTrigger(5); % send trigger '5' to eeg system
% sp.findSerialPortName(); % for troubleshoot if not connecting
% properly
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
% Toolboxes requires: instrument control toolbox
%
% See also: instrhwinfo,instrreset,serial
% Version: v1.0
% Date: Dec-19 2017, 10:00 AM PST
% Author: Roee Gilron, UCSF, SF, CA.
% URL/Info: github.com/roeegilron/biosemitrigger
properties
sp
end
% The internal data implementation is not publicly exposed
properties (Access = 'protected')
props = containers.Map;
end
methods (Static = true)
function portnames = getPortNames()
% set serial port names for each os, may need modifcation
portnames.mac = '/dev/cu.usbserial-DN17M98C';
portnames.pc = 'COM3';
portnames.linux = '/dev/cu.usbserial-DN17M98C';
end
end
methods
% Overload property names retrieval
function names = properties(obj)
names = fieldnames(obj);
end
% Overload clspass object display
function disp(obj)
disp([obj.props.keys', obj.props.values']); % display as a cell-array
end
function obj = BioSemiSerialPort(~)
sp = [];
instrreset
serialInfo = instrhwinfo('serial');
avportst = serialInfo.AvailableSerialPorts;
pnms = obj.getPortNames;
% get serial port name(serial COM name different across OS's)
if ismac % mac systems
% check to see if cable is connnected
sercheck = cellfun(@(x) any(strfind(x,pnms.mac)),...
avportst);
elseif ispc % pc's
sercheck = cellfun(@(x) any(strfind(x,pnms.pc)),...
avportst);
elseif isunix & ~ismac % linux ?
sercheck = cellfun(@(x) any(strfind(x,pnms.linux)),...
avportst);
end
% open serial port
if sum(sercheck) == 0
error('trigger cable not connected');
else
obj.sp = serial(avportst{sercheck},....
'BaudRate',115200,...
'DataBits',8,...
'StopBits',1);
fopen(obj.sp);
if isvalid(obj.sp)
fprintf('succesfully connected to serial port %s\n',obj.sp.Port);
end
end
end
function findSerialPortName(obj)
fprintf('Serial port have different names in each OS\n');
fprintf('This process helps you discover serial port name for BioSemi Cable on your OS\n');
fprintf('Make sure you have your cable and its not connected OS\n');
takemeasure = 0;
while takemeasure == 0
takemeasure = input('is cable idsconnected?\n(1 = yes)');
pause(3);% makes sure if cable disconnected recently it is purged
instrreset
serialInfo = instrhwinfo('serial');
avportst1 = serialInfo.AvailableSerialPorts;
end
fprintf('All connected serial objects recorded\n');
fprintf('Now please connect BioSemi trigger cable\n');
takemeasure = 0;
while takemeasure == 0
takemeasure = input('is cable connected?\n(1 = yes)');
pause(3); % gives system time to load driver
instrreset
serialInfo = instrhwinfo('serial');
avportst2 = serialInfo.AvailableSerialPorts;
end
fprintf('\n\n BioSemi serial port name is:\n')
cellfun(@(x) fprintf('%s\n',x),setxor(avportst1,avportst2))
fprintf('Please check one of these strings matches correct OS on lines 40-42\n')
end
function wipePorts(obj)
instrreset
end
function testTriggers(obj)
for i = 1:7
fwrite(obj.sp,uint8(2^i))
pause(0.2);
end
end
function sendTrigger(obj,code)
try
fwrite(obj.sp,uint8(code))
if code > 255
warning('This cable only supports triggers in the range of 1-255 (int) - 8 bits');
end
catch
if ~isvalid(obj.sp) % calbe not connected
error('cable is not connected anymore / was disconneted. Please delete object and reconnect cable');
end
end
end
end
end