Serial Port Connection Between Two Computers - MATLAB
Serial Port Connection Between Two Computers - MATLAB
faq, realtime
For the type of connection we are talking about here, you need a so called 'null modem cable'. A null
modem cable is an RS-232 serial cable where the transmit and receive lines are crosslinked.
To check that the serial connection between a linux computer and a windows computer is functional,
you can do the following:
● On the linux PC, open up a putty session by typing putty. Choose serial and type the path for the
serial port (e.g. /dev/ttyS0/). The main thing to consider is the Baudrate(e.g. 115200), which has to
be the same on sending and receiving end.
● On the windows PC, open up a putty session and setup a serial port connection (click on serial),
and specify the name of the port where the serial device is connected to (e.g. 'COM3)', make sure
baudrate is the same as on the linux machine(i.e. 115200).
● Then once the connection is established you can type in the windows putty display and can then
read it from the linux putty display and vice versa. If this doesn't work just check the hardware
connections.
Here we need a computer with two serial ports(or two computers). We can send commands on one
serial port and receive them on the other and then estimate the delay.
This is what I did on my office PC using fieldtrip commands ft_read_event and ft_write_event.
delete(instrfind);
fclose('all');
clear all;
close all;
addpath(genpath('H:\common\matlab\fieldtrip\'));
% addpath(genpath('H:\common\matlab\fieldtrip\private\'));
%% filetype_check_uri and ft_filter_event need to be in the path
%% Note the syntax: serial:<port>?key1=value1&key2=value2&...
%% here key1 is BaudRate and value1 is 115200
figure
plot(tlop*1000,'.');
xlabel('function calls');
ylabel('delay read write event [ms]');
modal_val=mode(tlop(2:end)*1000)
median_val=median(tlop(2:end)*1000)
range_val=range(tlop(2:end)*1000)
Alternatively, one can simply use matlab serial objects and low level reading function fread or fscanf:
%% open it
fopen(serobjw2);
count=0
tlop=[];
while 1
count=count+1;
%% write to COM1
fwrite(serobjw,5); %% can be numeric or a string
tic
if ~isempty(serobjw2.BytesAvailable)
if serobjw2.BytesAvailable~=0
%% read from COM4 (physically connected to COM1)
a=fread(serobjw2,serobjw2.BytesAvailable);disp(a);
clear a;
%% to convet numeric ascii code to char string use a=char(a')
%% the line below will also work for char input
% a=fscanf(serobjw2,'%s\n',serobjw2.BytesAvailable),clear a;
t1=toc;
tlop=[tlop,t1];
if count>1000
break
end
end
end
pause(0.15);%% give serial a break
end
figure
plot(tlop*1000,'.');
xlabel('function calls');
ylabel('delay read write, matlab serial [ms]');
modal_val=mode(tlop(2:end)*1000)
median_val=median(tlop(2:end)*1000)
range_val=range(tlop(2:end)*1000)
From:
http://fieldtrip.fcdonders.nl/ - FieldTrip
Permanent link:
http://fieldtrip.fcdonders.nl/faq/how_can_i_test_the_serial_port_connection_between_two_computers