From Part I
of this topic, we learn the basics of Scilab serial communication toolbox
and construct a simple GUI to handle the connection and data transfer in form of ASCII strings. Things seem to work fine with general commands and responses, which so far are displayed on Scilab console as status message or parameter value retrieval.
For control system analysis and design purpose, data need to be plotted and processed in Scilab. So the received data string must be in some valid format ready to be executed. For example, suppose we request the CM1 board for a set of sampled input and output values, say, 100 points. It is preferable that the returned string be in a matrix format so that Scilab command execstr
could be used to construct a matrix in the workspace easily. For such convenience, RED
provides a command “scilab on”
to append a variable name dstr
and square brackets to the raw data. Then we can pass the whole received string to execstr
without having to edit anything.
The data sampling on CM1 board can be set by a command “datasamp,”
that indicates how frequent data is captured with respect to the controller interrupt cycle. Setting datasamp=5
, for example, means data is sampled each 5 interrupts. To demonstrate without overwhelming ourselves by too many data points, we also issue a command datasize=10
to capture only 10 points of data.
With capture mode 1, data is sent back anytime reference command is changed. Figure 1 shows how the data string looks like on CoolTerm
Figure1 data string received after ‘cmd 300’
Notice that the data string is organized properly, enclosed by dstr = [ ]
. A matrix dstr
can be created easily in Scilab by copy and paste operation.
Figure 2 shows the same data capture operation, this time read using the RED Command Window in Scilab.
Figure 2 data string from readserial()
Obviously from Figure 2, data string shown on Scilab console does not look neat like that in Figure 1. Knowing that this string is contained in variable rcvstr
, we want to construct a matrix using Scilab command execstr
, as in Figure 3.
Figure 3 problem with execstr command
The operation fails! The error message itself does not help much, but from the error location and the line breaks in the string display, we guess that some extra character might somehow be embedded in the received data string. This could be a carriage return ascii(13) and/or line feed ascii(10). Interestingly enough, this problem does not appear in the CoolTerm
program, so it is unlikely to originate from the RED
Anyway, once we have some idea of the error source, a quick fix is straightforward. Simply eliminate any ascii(10) and ascii(13) in the received string with the following Scilab script cleanstr.sce
, which makes use of Scilab command strtok()
// clean ascii(10), ascii(13) from a string variable rcvstr
delims = ascii(10)+ascii(13);
Tokens = ;
token = strtok(rcvstr,delims);
Tokens = [Tokens,token];
ix = 1;
while (token <> '')
token = strtok(delims);
Tokens = [Tokens,token];
ix = ix+1;
crcvstr = Tokens;
After running this script, the cleaned data string is contained in crcvstr
. Pass it to execstr
as in Figure 4
Figure 4 using execstr command with cleaned data string
Bravo! The operation now succeeds without any error.
The data contained in dstr
consists of the command, plant output, and controller output, in column 1,2,3, respectively. A step response can be easily plotted by first creating the time vector with 0.05 second increment (equal to data sampling interval)
then the plot command
resulting in the step response as shown in Figure 5.
Figure 5 step response plot from dstr
In this 2-part article, we examine the Serial Communication Toolbox
in the ATOMS package for Scilab. The toolbox consists of only 4 essential functions, which are used successfully to establish a UART connection between PC and our controller target board and exchange ASCII strings. A simple GUI is constructed to ease the operations. We also address some extra character problem in the received string and suggest a way to fix it.
E.Segre and A. Sengupta. Serial Communication Toolbox