replicate Arduino's serial monitor on Scilab consol

萝らか妹 提交于 2019-11-29 13:09:15

OK, after a couple of days struggling I figured this out. It turns out that SciLab doesn't have native serial communication functionality and the Toolbox developer has used TCL_EvalStr to run Tcl commands externally. I had to dig into the Tcl serial communication syntax (i.e. read, open, gets, fconfigure ... ), ask another question, get some help and then finally end up with a new function for the Toolbox which I have committed as a pull request:

function buf = readserialline(h)
    tmpbuf = emptystr();
    while tmpbuf == emptystr()
        TCL_EvalStr("gets " + h + " ttybuf");
        tmpbuf = TCL_GetVar("ttybuf");
    end
    buf = tmpbuf;
endfunction 

now one can get the above behavior by running:

h = openserial(7, "9600,n,8,1") // open COM7

for ii = 1:40
    disp(readserialline(h))
end

closeserial(h)

to read the serial port line by line and print it to the SciLab console. Now to parse the CSV data you may use:

 csvTextScan(part(readserialline(h), 1:$-1), ',')

P.S.1. I have used a virtual Arduino board inside SimulIDE and used com0com to create virtual serial ports. More information here on SourceForge.

P.S.2. More discussion with Toolbox developer Aditya Sengupta here on Twitter.

P.S.3. More discussions here on Tcl Google group

P.S.4. A full demonstration plus instructions here on Reddit

P.S.5. For those who might endup here I have decided to fork Aditya Sengupta's repository here with several improvements.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!