Scilab Home Page | Wiki | Bug Tracker | Forge | Mailing List Archives | Scilab Online Help | File Exchange
ATOMS : Serial Communication Toolbox details
Login with GitLab

Serial Communication Toolbox

A toolbox for communication over a Serial Port in Scilab
(33806 downloads for this version - 95252 downloads for all versions)
Details
Version
0.4.1
A more recent valid version exists: 0.5
Authors
Enrico Segre
Aditya Sengupta
Owner Organization
Indian Institute of Technology Bombay
Maintainer
Aditya Sengupta
Categories
License
Creation Date
October 2, 2012
Source created on
Scilab 5.4.x
Binaries available on
Scilab 5.4.x:
Windows 64-bit Windows 32-bit Linux 64-bit Linux 32-bit macOS
Scilab 5.5.x:
Windows 64-bit Windows 32-bit Linux 64-bit Linux 32-bit macOS
Install command
--> atomsInstall("serial")
Description
            Serial Communication Toolbox for Scilab
=======================================

This toolbox enables the use of Serial Communication in
[Scilab](http://www.scilab.org/) and
[Scicoslab](http://www.scicoslab.org/).
This is a port of the __Portable Serial Toolbox__ for Scilab originally written
by [Enrico Segre](http://www.weizmann.ac.il/home/fesegre/)
and has been ported 
to Scilab 5.x and released with his kind permission. It is licensed, at his 
request, under the GPLv3. 

Enrico does not wish to provide support for this
toolbox anymore. Therefore, for any support related queries or otherwise,
please raise an issue on the
[Github project page](https://github.com/sengupta/Scilab-Serial/).
You can reach me at [this address](mailto:aditya@sengupta.me), but I would
prefer you raise an issue on Github for problems with the toolbox. 

This toolbox is known to have worked so far on Linux based systems and on
Windows (up till Windows 7). 

The implementation via TCL wrappers is certainly suboptimal, but shouldn't be
such
a penalty, given the low bandwidth and the intrinsic asynchronous mode of serial

communication. And mainly, it relieves the developer from supporting many
platforms 
at once. 

Release Notes: 
==============

15/4/2012:
----------
v.0.4: Toolbox updated to be compatible with Scilab 5.4 

14/5/2011: 
----------
(First Release by Aditya Sengupta) 

v.0.3: Toolbox is now compatible with Scilab 5.x, along with help files. 

12/1/2009:
----------
(Final Release by Enrico Segre)

v.0.2: corrected version after report of [bug
3829](http://bugzilla.scilab.org/show_bug.cgi?id=3829):
readserial/writeserial
confused by strings containing nonprintable ascii characters, 

Toolbox essentially still geared for scilab 4.x (4.1.2, scicoslab) in the sense
that:

- ``builder.sce`` and ``loader.sce`` make use of ``xmltohtml()``,
``add_help_chapter()``; xml files are based on the 4.x dtd (IF I would be able
to make doc in scilab5 I might even consider to package a new help page set
together with the old)
- in many points the detour ``TCL_EvalStr("set answ [....]");
  TCL_GetVar("answ")`` is still implemented (was mandatory prior to [request
319](http://requestzilla.scilab.org/show_bug.cgi?id=319)),
it is not required
since scilab 4.1.2
 
However, the toolbox is reported to work with Scilab 5.0.2 final [I suppose,
without help pages readable in the help widget, but that's minor; html files
provided, anyway]
            
Files (3)
[65.12 kB]
Source code archive
makes it available for Scilab 5.4
set the same case to serial as in the portal
[48.14 kB]
OS-independent binary for Scilab 5.4.x
Binary version
Automatically generated by the ATOMS compilation chain

[46.61 kB]
OS-independent binary for Scilab 5.5.x
Binary version (all platforms)
Automatically generated by the ATOMS compilation chain

News (0)
Comments (28)     Leave a comment 
Comment from ivan stanisheuski -- October 4, 2012, 10:14:36 PM    
I have an experience in data acquisitions via serial port using Matlab. 
I've tried to implement something like that in Scilab and found that the readserial 
function does not flush the serial buffer for next reading of a data stream, so to acquire

the next number of bytes one needs to re-open serial port via 'closeserial-openserial'. 
For acquisitions at the high baud rates such the re-open procedure is undesirably long. 
Am I right? If yes, is there a solution of the problem?
Comment from Petrúcio Medeiros -- November 26, 2012, 01:49:13 AM    
I'm trying to use the Toolbox, but when I put: h = openserial (3, "9600, n, 8,1")
only
displays the error:
! - error 999
TCL_EvalStr, at line 1
could not open "COM3": no such file or directory
     while executing
"open COM3: + r"
     invoked from within
"set porthandle [open COM3: + r]"
at line 17 of function openserial called by:
h = openserial (3, "9600, n, 8,1 ');
My version is 5.2.0 scilab.
What should I do?
Answer from Samuel Gougeon -- November 26, 2012, 01:57:55 AM    
Either the COM3 is not enabled on your computer or COM3 is already 
open (not sure that there is a specific error message for this case).
You should check for both these points.
Answer from Samuel Gougeon -- November 26, 2012, 02:01:49 AM    
> My version is 5.2.0 scilab.
By the way, you must upgrade to 5.4.0: A Scilab-TCL bug preventing
the toolbox to work properly has been fixed since 5.4.0-alpha1
Comment from Samuel Gougeon -- August 6, 2013, 01:43:43 PM    
Hello Aditya,
TCL/Tk is not available (because it does not work) on Mac OS.
So this package should not be tagged for MacOS.
Regards
Samuel
Comment from Samuel Gougeon -- November 7, 2013, 07:05:03 PM    
Aditya,

I am wondering about adding two features:

* openserial(): add a endOfCommand parameter, of type string.
  This would be a string (ascii(10) or ascii([10 13])) to be automatically
   added to every command string accepted by writeserial()
  For an instrument plugged to a port, it is always the same eoc sequence.
  Adding it by hand each time could therefore be avoided just by declaring it once
  when opening the port, and implementing its concatenation in writeserial().

* queryserial(..) function is missing. Query function functions are usually ~ half of 
  the set of functions addressing an instrument. Such a function would be more handy 
  than an explicit sequence of writeserial() + readserial(..). 
  A possible implementation could be as hereafter.

Please get in touch with me whether you would prefer that i implement that and the
new related help page. I would send to you new and updated macros and help file in
order to upgrade the toolbox.

Cheers
Samuel
-----------------
function [r, err] = queryserial(h, command [ , nbytes, swait, errquery])
  // nbytes: maximal number of bytes to read. 0 means All bytes in buffer
  // swait: duration of the pause after the write command, before reading the buffer [s]
  // errquery (string): command to send in case of error status after the write command,
  //    in order to get from the device some information about the error that occurred.
  // r: result (string)
  // err: if errquery is provided: the result of error query command (string)
  //      else: output status as from writeserial (decimal)
  if argn(2)<3
     nbytes = 0
  end
  if argn(2)<4
     swait = 0.002
  else
     swait = min([abs(swait) .002])
  end
  if argn(2)<5
     errquery = ""
  end

  s = writeserial(h, command)
  if s==-1
     if errquery~=""
       [r, err] = queryserial(h, errquery, nbytes, swait) // no <errquery> =>
can't loop
     else
        r = ""
        err = -1
     end
  end
  if swait
     sleep(swait*1000)
  end
  r = readserial(h,nbytes)
  err = 0
endfunction
Answer from Samuel Gougeon -- November 7, 2013, 07:07:48 PM    
>   s = writeserial(h, command)
>   if s==-1
>      if errquery~=""
>        [r, err] = queryserial(h, errquery, nbytes, swait) // no <errquery> =>
can't loop
>      else
>         r = ""
>         err = -1
          return    // was missing.
>      end
>   end

To be tested
Answer from Aditya Sengupta -- November 7, 2013, 07:20:06 PM    
Hi Samuel, 

Could you email me at aditya@sengupta.me? I'll add you to the Github repository for this
project. 
Answer from Samuel Gougeon -- November 7, 2013, 07:31:37 PM    
This answer has been deleted.
Comment from Giovanni Licitra -- January 24, 2014, 09:43:01 AM    
Hi everyone!!!

someone could explain because I am not able to install serial communication toolbox? 

I am  working with ubuntu 2.6.32-54-generic-pae. I have installed other toolbox by ATOMS 
without any problem. when I try to install Serial Communication Toolbox by ATOMS, 
apparently it seem installed but when I restart scilab in the console appear:

'atomsLoad: An error occurred while loading 'serial-0.4'.

Comment from Silvio Kuehn -- July 13, 2014, 07:56:34 PM    
If someone has problems to open Ports with numbers > 9 or in general, you can download 
serial-0.4.1-2-src.zip and comment or replace following line in file serial-0.4\macros
\openserial.sci:

      port="COM"+string(p)+":"

with

      port="\\\\.\\COM"+string(p);

It'll also work for port names <=9

Regards
Answer from Olivier Leman -- May 13, 2015, 05:05:35 PM    
Dear Silvio, dear all,

I use Scilab V5.5.2 under Windows 7 64-bits, and I have a bluetooth 2 SPP module that is
listed as COM14 within the bluetooth manager, and works fine when interfaced with a COM
terminal (termite in my case). With the original archive automatically installed via
atomsInstall('serial'), Scilab replies with the following error message:
could not open "COM14": no such file or directory
I also made sure that the donnection with the terminal was terminated, so that COM14 was
available.
So I downloaded the archive and proceeded with the modification that you recommend within
the openserial.sci file. Then I re-created the .zip archive with 7-zip and tried a
atomsInstall() on that new archive. Scilab replied the following:

 !--error 10000 
atomsDESCRIPTIONread : Le fichier
"C:\Users\lma\AppData\Roaming\Scilab\scilab-5.5.2\.atoms\x64\tmp_1431527582\Serial-0.4\DESCRIPTION"
n'existe pas.
at line      46 of function atomsDESCRIPTIONread called by :  
at line     160 of function atomsInstall called by :  
atomsInstall('C:\Users\lma\Desktop\serial-0.4.1-2-src.zip')

There is no such DESCRIPTION file in the archive ... does anyone know where does the
problem come from?
Thank you, best regards
Olivier


> If someone has problems to open Ports with numbers > 9 or in general, you can
> download 
> serial-0.4.1-2-src.zip and comment or replace following line in file
serial-0.4\macros
> \openserial.sci:
> 
>       port="COM"+string(p)+":"
> 
> with
> 
>       port="\\\\.\\COM"+string(p);
> 
> It'll also work for port names <=9
> 
> Regards
Answer from Olivier Leman -- May 13, 2015, 06:12:56 PM    
Dear Silvio, dear all,

I use Scilab V5.5.2 under Windows 7 64-bits, and I have a bluetooth 2 SPP module that is
listed as COM14 within the bluetooth manager, and works fine when interfaced with a COM
terminal (termite in my case). With the original archive automatically installed via
atomsInstall('serial'), Scilab replies with the following error message:
could not open "COM14": no such file or directory
I also made sure that the donnection with the terminal was terminated, so that COM14 was
available.
So I downloaded the archive and proceeded with the modification that you recommend within
the openserial.sci file. Then I re-created the .zip archive with 7-zip and tried a
atomsInstall() on that new archive. Scilab replied the following:

 !--error 10000 
atomsDESCRIPTIONread : Le fichier
"C:\Users\lma\AppData\Roaming\Scilab\scilab-5.5.2\.atoms\x64\tmp_1431527582\Serial-0.4\DESCRIPTION"
n'existe pas.
at line      46 of function atomsDESCRIPTIONread called by :  
at line     160 of function atomsInstall called by :  
atomsInstall('C:\Users\lma\Desktop\serial-0.4.1-2-src.zip')

There is no such DESCRIPTION file in the archive ... does anyone know where does the
problem come from?
Thank you, best regards
Olivier


> If someone has problems to open Ports with numbers > 9 or in general, you can
> download 
> serial-0.4.1-2-src.zip and comment or replace following line in file
serial-0.4\macros
> \openserial.sci:
> 
>       port="COM"+string(p)+":"
> 
> with
> 
>       port="\\\\.\\COM"+string(p);
> 
> It'll also work for port names <=9
> 
> Regards
Answer from Samuel Gougeon -- May 13, 2015, 09:55:45 PM    
Hi Olivier,

Just keep the original archive, load the serial package in session, and do the following:

edit openserial
// change the line #7: port="COM"+string(p)+":"
// exec the script to redefine openserial in the session
// enter
save openserial.bin openserial
// Move the local file openserial.bin into SCI\contrib\serial...\<version>\macros\
// overwritting its former version

And that's it.

Samuel
Comment from Jeff Waters -- September 4, 2014, 09:25:50 PM    
Having a problem making connection to usb via scilab 5.5.0.    Can connect with MiniCom
2.7.... Get the response below.... any thoughts anyone ????


h=openserial('ttyUSB0',"9600,n,8,1")
 !--error 999 
TCL_EvalStr,  at line 1
	couldn't open "ttyUSB0": permission denied
    while executing
"open ttyUSB0 r+"
    invoked from within
"set porthandle [open ttyUSB0 r+]"
at line      17 of function openserial called by :  
h=openserial('ttyUSB0',"9600,n,8,1")
Answer from Samuel Gougeon -- September 4, 2014, 10:13:31 PM    
> Having a problem making connection to usb via scilab 5.5.0.    Can connect with
MiniCom
> 2.7.... Get the response below.... any thoughts anyone ????
> 
For this Scilab module "Serial" means "RS-232" protocole. Are you using
an adaptator
RS232-USB enabling to use the RS-232 protocole through a USB port?
Answer from Samuel Gougeon -- September 5, 2014, 09:38:11 PM    
After your comment http://atoms.scilab.org/toolboxes/serial/0.4.1/#comment2286:

Under Windows, i have got this error message many times. It occurred because Windows locks
the port after you open it with openserial(). Typically, if you run a second time
openserial() on the same port while it has not been quited, you get this error.
If after opening the port you loose its id "file###" returned by openserial(),
then it's
over: You must quit Scilab (to release and unlock the port) and then run a fresh session.
Sometimes it is not enough: The OS session must be quited, and even sometimes the computer
must be restarted.
So, keep the port handle, and don't try to open a port already open.
HTH
Regards
Samuel

Comment from Jeff Waters -- September 5, 2014, 04:49:08 PM    
yes


ttyUSB is a converter to use usb as a serial 
Comment from Jeff Waters -- September 17, 2014, 11:22:21 PM    
The port is recognized but just not thru Scilab.... Heres what I get via the terminal


bonedaddy@Yeffery:~$ dmesg | grep tty
[    0.000000] console [tty0] enabled
[  480.569584] usb 1-1.1: FTDI USB Serial Device converter now attached to ttyUSB0


Thoughts ????

Jeff

Comment from Tushar Chaskar -- December 25, 2014, 10:35:19 AM    
How can i transmit and receive binary(Non ASCII)values through serial port?
Comment from Tushar Chaskar -- December 25, 2014, 11:01:15 AM    
-->str = ['0x4E','0x58','0x45']
 str  =
 
!0x4E  0x58  0x45  !
 
-->writeserial(h,str)
 ans  =
 
    0.  
 
-->p = readserial(h)
 p  =
 
 0x4E0x580x45   
 
-->size(str)
 ans  =
 
    1.    3.  
 
-->size(p)
 ans  =
 
    1.    1.  

I want size of p same as str
Comment from Huub vN -- March 6, 2015, 10:46:21 AM    
I' m experiencing the same problem using a RS232-USB converter. 

$ dmesg | grep tty
[    0.000000] console [tty0] enabled
[   25.052223] usb 2-1.3.3: pl2303 converter now attached to ttyUSB0

Connecting to the serial port ttyUSB0 using minicom works fine and I can establish and use
a serial terminal through minicom without a problem.

Within Scilab 5.5.1 (x64 Linux):

-->h = openserial('/dev/ttyUSB0',"115200,n,8,1")
 !--error 999 
TCL_EvalStr,  at line 1
	couldn't open "/dev/ttyUSB0": permission denied
    while executing
"open /dev/ttyUSB0 r+"
    invoked from within
"set porthandle [open /dev/ttyUSB0 r+]"
at line      17 of function openserial called by :  
h = openserial('/dev/ttyUSB0',"115200,n,8,1")

Is there any solution to get permission like 'sudo' ?
Comment from James Bond -- April 27, 2015, 09:26:57 PM    
If this command is run- 'h=openserial(1,"19200,n,8,1")' and then the following is
run:



[q,flags]=serialstatus(h);

printf("Port status is %i %i %s\n",q(1),q(2),flags);

writeserial(h,"status#");

response=readserial(h);

printf("%s\n\nend",response);

closeserial(h);



The following is received:



"Port status is 537 0 CTS 0 DSR 0 RING 0 DCD 0

'Status OK'



end"



Which is good, however, if the above is run together in a script:



h=openserial(1,"19200,n,8,1");

[q,flags]=serialstatus(h);

printf("Port status is %i %i %s\n",q(1),q(2),flags);

writeserial(h,"status#");

response=readserial(h);

printf("%s\n\nend",response);

closeserial(h);



The following is received:



"Port status is 0 0 CTS 0 DSR 0 RING 0 DCD 0



end"



Which is not good. This is being run on windows 8 64bit with scilab 5.5.1

a 3second 'xpause' and then also a 'sleep' command were inserted after the 'openserial' 
fxn but that did not change the response.
Comment from James Bond -- April 27, 2015, 10:01:51 PM    
The below appears to marginally work. So a delay is needed after write serial.



h=openserial(1,"19200,n,8,1");

writeserial(h,"status#");

sleep(100);

ree=readserial(h);

writeserial(h,"status#");

sleep(600);

response=readserial(h);

printf("%s\n",response);

closeserial(h);
Answer from Samuel Gougeon -- April 27, 2015, 10:20:22 PM    
> The below appears to marginally work. So a delay is needed after write serial.

Yes, this is typical. After any request, you must leave some time to the device to answer
and feed the output buffer.. Otherwise, if you readout the output buffer too soon, a
typical side effect is to get the end of the answer at the next readserial() instruction.

Comment from Tushar Chaskar -- May 26, 2015, 01:01:12 PM    
Can anyone mention the average millisecond time required to execute following functions

openserial
closeserial
writeserial
readserial
serialstatus

I need time taken by each function to execute. Please help me with this. 
Comment from Tushar Chaskar -- May 26, 2015, 01:17:13 PM    
According to my observation if i do 

tic;a = openserial(3,"57600,n,8,1");toc

I get

ans  =
 
    0.54  

and 

tic;closeserial(a);toc

ans  =
 
    0.124  

To open serial port it takes approximately 500ms and to close it takes 100ms

Now,

b = ascii([78 69 88 5 5 11])

tic;a = openserial(3,"57600,n,8,1");writeserial(a,b);closeserial(a);toc

 ans  =
 
    0.657  

opening com port, writing 6 bytes on com port and closing that com port takes 
approximately 650ms. 
Is there any way that i can reduce this time delay. If yes then please reply to this 
post.
Answer from Samuel Gougeon -- May 26, 2015, 09:27:37 PM    
Hello,
Opening a port is somewhat like opening a communication session. After that, you put and
get a lot of traffic on the port, and only at the end you close the port.
The time taken to open or close the port could be quite heavily OS-dependent.
Regards
Comment from Yann DEBRAY -- September 14, 2015, 05:36:20 PM    
Works on Windows 10
Just tried it to read values on the serial port of the arduino uno
Comment from Jeff Waters -- August 3, 2016, 05:10:58 PM    
is this atom available for Scilab 5.5.2  .... dont see it with atomsList();
Comment from Jeff Waters -- August 4, 2016, 08:57:29 PM    
running Scilab 5.5.2 on SL 7.1 and 3.10.0-229.7.2.el7.x86_64 and Cannot access serial port
... from Scilab 
entering h=openserial("dev/ttyACM0", "115200") yields h=100 ... when
readserial(h) get only undefined operands 
check for overloads 

Anyone have thoughts ???

thanks

J
Comment from pe sa -- February 4, 2017, 08:12:50 PM    
Hello,
I'd like to ask whether it is possible to somehow run the functions of this toolbox in my 
Xcos scheme simulation.
Leave a comment
You must register and log in before leaving a comment.
Login with GitLab
Email notifications
Send me email when this toolbox has changes, new files or a new release.
You must register and log in before setting up notifications.