ESenza to Matlab communication

This file implements the required steps in order to achieve communication to and from ESenza nodes PT130

Contents

Requirements

Script organization

The communication parameters setup is performed and the serial port communication is opened. Then the IDs of the devices in use are defined and the acquisition loop is started.

The main loop is structured as follows. Polling is actuated over the serial port in order to retrieve and process the message at the time it is received. The message structure is described in 'AT Command Protocol' provided by ESenza and into the devices user manual. The user should refer to those resources for a detailed description. The parsing implemented in this code extract the time-stamp and the temperature sensor measurements. Then, the plot corresponding to the device that sent the last message is updated.

Finally the connection with the serial port is closed and the serial port is released.

Data Acquisition Setup

Close any connection to the serial ports that could possibly be still opened

cClean;

Set the COM port to which the USB is linked

serial_obj = serial('COM3');

Setup serial connection parameters

BaudRate=115200;
DataBits=8;
StopBits=1;
Parity='none';
TimeOut=1e3;
Flowcontrol='none';
Terminator='CR';%*
set(serial_obj,'BaudRate',BaudRate,'Flowcontrol',Flowcontrol,'Terminator',Terminator);
set(serial_obj,'DataBits',DataBits,'Parity',Parity,'StopBits',StopBits);
set(serial_obj,'TimeOut',TimeOut);

Port Open and Connection Start

fopen(serial_obj)

Cell array of Nodes IDs

ID = {'001EAA000000055D','001EAA00000006AA','001EAA00000006AB','001EAA00000006AC','001EAA00000006AD'};

Variables used by the plot

values=zeros(2,1);
times=zeros(1,1);
data=[];
ii=1;
T=[];
t=[];
tInit=[];
for i=1:length(ID)
    T{i}=[];
    t{i}=[];
end

OPTIONAL: test the writing capabilities to the serial port SPOS = '3'; ss=['AT+3_' ID{1} '_' SPOS '_0*' ]; fprintf(serial_obj,ss); fprintf(serial_obj,ss); fprintf(serial_obj,ss);

while ii<=50

Ensure to wait al least 100 ms before check for new messages in the serial port

    pause(.1);

Polling over the serial port and return the raw data of the first message

    data = fscanf(serial_obj);

Determine if received data is a measurement by checking the command header

    if (length(data)>=3)&&(strcmp(data(1:3),'+51'))

Parse the raw data, splitting over the underscore

        parts = strsplit('_',data);

Make sure all the requred fields are present

        if length(parts)>7
            i=1;

Determine the ID of the sender node

            while ~strcmp(parts{2},ID{i}), i=i+1; end

Check for vector consistency in the plots

            if isempty(tInit)
                tInit = toSeconds(parts{3})/2;
                if isempty(tInit)
                    tInit=0;
                    if isempty(t{i})
                        t{i}=0;
                    end
                    t{i}=t{i}+1;
                else
                    t{i}=0;
                end
            else

Convert the string into a number which indicates the elapsed seconds

                sup = toSeconds(parts{3})/2;
                if isempty(sup)
                    if length(t{i})==0
                        t{i}=1;
                    else
                       t{i}(end+1)=t{i}(end)+1;
                    end
                else
                    t{i}(end+1)= sup-tInit;
                end
            end

Get the measurement into numeric format

            T{i}(end+1)=sensorConverter(parts{8});
            ii=ii+1;

Plot

            hold on
            for i=1:length(ID)
                switch i-1
                    case 0
                        plot(t{i},T{i},'d-b');
                    case 1
                        plot(t{i},T{i},'s-k');
                    case 2
                        plot(t{i},T{i},'+-g');
                    case 3
                        plot(t{i},T{i},'o-r');
                    case 4
                        plot(t{i},T{i},'*-m');
                end
            end
            hold off
        end
    end
end

Port Close and Connection End

fclose(serial_obj);

Close any connection to the serial ports that might be left opened

cClean