Skip to content

Instantly share code, notes, and snippets.

@dirkjanfaber
Last active April 22, 2021 13:57
Show Gist options
  • Save dirkjanfaber/79d212b1d735fb070c90ef31f3d6ad45 to your computer and use it in GitHub Desktop.
Save dirkjanfaber/79d212b1d735fb070c90ef31f3d6ad45 to your computer and use it in GitHub Desktop.
Plot incoming serial port data in Octave
pkg load instrument-control
% got this function from https://www.edn.com/read-serial-data-directly-into-octave/
function [char_array] = ReadToTermination (srl_handle, term_char)
% parameter term_char is optional, if not specified
% then CR = 'r' = 13dec is the default.
if (nargin == 1)
term_char = 13;
end
not_terminated = true;
i = 1;
int_array = uint8(1);
while not_terminated
val = read(srl_handle, 1);
if(val == term_char)
not_terminated = false;
endif
% Add char received to array
int_array(i) = val;
i = i + 1;
endwhile
% Change int array to a char array and return a string array
char_array = char(int_array);
endfunction
% prompt for the serial port to use
[sel, ok] = listdlg ("ListString", seriallist,
"SelectionMode", "Single");
if (ok == 1)
serial_port = sprintf ("/dev/%s", seriallist{sel});
else
disp ("You cancelled.");
endif
% pick the right one
s = serialport(serial_port, 115200)
br = get (s, "BaudRate")
y_temp = cell(10,1)
y = 0
while true
for i = 1:10
y_serial = str2num(char(ReadToTermination(s)));
y_temp {i,1} = y_serial;
endfor
y = cat(1, y, y_temp{1:10});
pause(0.01)
plot(y)
endwhile
fclose(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment