Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

tcl - replicate Arduino's serial monitor on Scilab consol

If I use the Arduino IDE's Serial monitor I can read the pair of comma separated values as below:

enter image description here

I want to first replicate this behavior in SciLab terminal. I used the Serial Communication Toolbox:

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

which returns either empty or

, 169

228, 179

228,

228, 205

228, 209 228,

putting the disp(readserial(h)) in a while loop also doesn't help. Not only there are too many empty lines, if I stop the while loop it does not close the port (something like try-catch should be used I think). I would appreciate if you could help me know how I could get the same behavior as Arduino's serial monitor?

P.S. Next I want to plot these two values in realtime. So maybe using the csvTextScan function to split the string into two integers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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), ',')

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...