Tutorial by Examples

% Define serial port with a baud rate of 115200 rate = 115200; if ispc s = serial('COM1', 'BaudRate',rate); elseif ismac % Note that on OSX the serial device is uniquely enumerated. You will % have to look at /dev/tty.* to discover the exact signature of your % serial device ...
Assuming you created the serial port object s as in this example, then % Read one byte data = fread(s, 1); % Read all the bytes, version 1 data = fread(s); % Read all the bytes, version 2 data = fread(s, s.BytesAvailable); % Close the serial port fclose(s);
Assuming you created the serial port object s as in this example, then to close it fclose(s) However, sometimes you can accidentally lose the port (e.g. clear, overwrite, change scope, etc...), and fclose(s) will no longer work. The solution is easy fclose(instrfindall) More info at instrfin...
Assuming you created the serial port object s as in this example, then % Write one byte fwrite(s, 255); % Write one 16-bit signed integer fwrite(s, 32767, 'int16'); % Write an array of unsigned 8-bit integers fwrite(s,[48 49 50],'uchar'); % Close the serial port fclose(s);
Matlab supports synchronous and asynchronous communication with a serial port. It is important to chose the right communication mode. The choice will depend on: how the instrument you are communicating with behave. what other functions your main program (or GUI) will have to do aside from managi...
Some devices connected through a serial port send data to your program at a constant rate (streaming data) or send data at unpredictable intervals. You can configure the serial port to execute a function automatically to handle data whenever it arrives. This is called the "callback function&quo...

Page 1 of 1