Packaged App - Read from serial port

304 views
Skip to first unread message

Andy Weaver

unread,
Aug 1, 2013, 8:04:20 AM8/1/13
to chromi...@chromium.org
I can use chrome.serial.read to read data from serial port. However, There's no way I can get notified as soon as new data arrives. In Java, I can use a seperate thread and a blocking method do to port mornitoring. Now in JS, How can I add a real time port listener? I don't want to use a timer because there's always delays. I need real time communication. Any idea?

Renato Mangini

unread,
Aug 1, 2013, 1:49:49 PM8/1/13
to Andy Weaver, Chromium Apps

Andy,

The chrome.serial.read notifies your callback when there is content available. Since the JavaScript in the browser is mostly single threaded, your callback will be queued in the running queue and called as soon as the main loop allows it. Unless you are doing CPU-heavy tasks in the main thread, that usually doesn't take more than a few milliseconds. If you are experimenting a high latency to have your callback called, check if there is anything in your code, running while the serial is receiving data, that is taking too much processing time (check these articles at HTML5Rocks) and consider moving CPU-intensive code into a WebWorker.



Renato Mangini | Chrome Developer Programs Engineer | man...@google.com | +55 11 2395-8608


On Thu, Aug 1, 2013 at 9:04 AM, Andy Weaver <andy...@googlemail.com> wrote:
I can use chrome.serial.read to read data from serial port. However, There's no way I can get notified as soon as new data arrives. In Java, I can use a seperate thread and a blocking method do to port mornitoring. Now in JS, How can I add a real time port listener? I don't want to use a timer because there's always delays. I need real time communication. Any idea?

--
You received this message because you are subscribed to the Google Groups "Chromium Apps" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-app...@chromium.org.
To post to this group, send email to chromi...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-apps/.
For more options, visit https://groups.google.com/a/chromium.org/groups/opt_out.
 
 

Andy Weaver

unread,
Aug 1, 2013, 10:59:00 PM8/1/13
to chromi...@chromium.org, Andy Weaver
Thanks Renato,

I've checked WebWorker just now. It seems chrome.serial API is only available from the Main Thread. Is there any reason why serial api can't run on Worker threads?

Renato Mangini

unread,
Aug 2, 2013, 9:26:49 AM8/2/13
to Andy Weaver, Chromium Apps

This is by design. Web workers run in a sandbox. They only communicate with the "outside world" via async postMessage, so you guarantee that they won't disrupt the single-threaded assumption that most JavaScript code make. My suggestion wasn't to put your serial I/O code in the web worker, but to move your CPU-intensive code there, so you can free the main thread to process serial communication faster. First things first: use the performance analysis (about:tracing, devtools timeline and profiles) to make sure this is your problem. You can find that your problem is that the serial API in JavaScript is too slow for your real time requirements. In that case, open a bug at crbug.com/new . But you can also find that the time your JavaScript code spend processing the data of each serial read is too high. In this case, you can move this processing code to a worker, keep the main thread reading from the serial port and sending incoming serial data to the worker via postMessage.


Renato Mangini | Chrome Developer Programs Engineer | man...@google.com | +55 11 2395-8608


Andy Weaver

unread,
Aug 2, 2013, 10:50:37 PM8/2/13
to chromi...@chromium.org, Andy Weaver
Hi Renato,

I've have a try and let you know if there's any problem. Thanks a lot for your kindly help!

Renato Mangini

unread,
Aug 5, 2013, 9:52:18 AM8/5/13
to Andy Weaver, Chromium Apps

Great! If you can, post your findings here, so everyone can learn from it.


Renato Mangini | Chrome Developer Programs Engineer | man...@google.com | +55 11 2395-8608


Reply all
Reply to author
Forward
0 new messages