Com Port for Scilab / Xcos

188 Views Asked by At

What is the easiest way to get a Block in Xcos that can communicate via Com Port.

I implemented a PID Controler in XCOS. Now I would like to switch from Model to the real world.

Before I start implementing my Control Paramters on the embedded device, i would like to send the calculatet set value to the COM Device and read the Control variable back to Xcos in order to calculate new parameters.

I would like to bulid a customized block with Parameters like Port, Baudrate etc. I am not very good in c programming. This is one reason why I choose Scilab.

scifunc_block seems to be a solution, which can use a scilab script calling functions from the serial toolbox.

What are your best ways to commmunicate in XCOS with Com Ports?

Thank you!

2

There are 2 best solutions below

0
Valeria Ganbat On

I figured out two solutions so far:

a) https://atoms.scilab.org/toolboxes/wgserialxcosio Unfortunately it is using base64, so you need to adjust the code in the embedded system, which is no solution for me

b) Use scifunc_block

**My problem is, how to handover the argument "serialport" from function connectCOM to serialblock **

function [result]=serialblock(data)
  serialport=TCL_GetVar("porthandle")
  result = writeserial(serialport,'>TF0')
endfunction

function [serialport,status]=connectCOM(connect)
// Todo check which Com port are available
    COMport=2;
    baud='19200';
    parity='n';
    data='8';
    stop='1';
    smode = string(baud+","+parity+","+data+","+stop); 
    status=0;

if connect=="on" then
  if exists("COMport","local") then
    try // Todo check if Port is connected already
      serialport = openserial(p=COMport, smode, translation='crlf',timeout=10);
      disp('Com Port ' + string(COMport) + ' is connected.');
      status=1;
    catch
      disp('ERROR: Com Port ' + string(COMport) +' not available!'+...
      ascii(10) + ascii(10)+  'Choose different com port or try to replug the device.'+ascii(10) + ascii(10));
    exit
    status=2;
  end
end
[error_message,error_number]=lasterror(%t)

elseif connect=="off" then
  serialport=TCL_GetVar("porthandle");
  if closeserial(serialport)==0 then
    disp('Com Port '+ string(COMport)+ ' is closed.');
    status=3;
  else
    disp('ERROR: Com Port cannot be closed!');
  end
else
  disp('Com Port error')
end

endfunction

[![Xcos two Coolterm][1]][1]

c) Build you own xcos block gui -- Unfortunately I do not know how to do so?

4
davidcl On

Xcos ARDUINO toolbox also includes ARDUINO_ANALOG_READ and ARDUINO_ANALOG_WRITE blocks using OS-specific functions (not TCL).