GUIDE - Callback triggered - How can I start function in callback safely without getting interrupted by same callback?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi everyone, I have a question concerning the handling of incoming serial data. In short: I have a wireless sensor network with xbees (up to 8 end devices and 1 coordinator linked to matlab via a MCU). Each end device sends data packets (8 to 12 Bytes) every now and then, depending on the sensor state. So it's likely that two or more sensors send their data "at the same time" (almost no pause between two packets). In case serial data is available in the Matlab GUI a callback function gets called. Until now, I do the whole processing (reading, validating, etc.) of the incoming data in this callback function [serConn.BytesAvailableFcn=@(src, event) mycallback(src, event, hObject);] which results in loosing a second data package which arrives right after the first one. I think it's because the callback function is too busy validating the data of the first package and simply fails to realize that there's a second packet incoming.
My idea is to build a function (i.e. function output = validatingData(hObject, event, handles)) that does the handling of the incoming data, so that the callback function just fills an array inputData with the incoming data and calls the function validatingData() (or sets a boolean variable which is checked by an infinite loop somewhere...). The function validateData() then copies all inputData to a local array for further processing. Can I lock somehow a function, mutex like? How would you guys suggest solving the processing of incoming data without loosing packets?
Help is appreciated very much! Thanks everyone in advance! Thomas
0 commentaires
Réponses (1)
Adam
le 16 Mai 2017
For a callback, the 'Interruptible' property of the component triggering the callback determines whether or not it can be interrupted. This is 'On' by default. If it is set to 'off' then the 'BusyAction' property becomes important - this is set to 'queue' by default, the other option being 'cancel' which would lose your incoming data I imagine.
For a general function outside of a callback there shouldn't be interruptions anyway, but if the function is called from inside a callback then the interruptible property affecting the callback comes into play.
3 commentaires
Guillaume
le 18 Mai 2017
I think that within the constraints of matlab (single threaded processing), Adam answers is exactly what you need. Set Interruptible to off and BusyAction to queue, so that when the second lots of data arrives when the first one is still being processed, it is queued for processing as soon as the callback finishes.
In other languages you could use a consumer-producer pattern but that would require multithreading which is only available in matlab with the parallel processing toolbox.
Voir également
Catégories
En savoir plus sur Graphics Performance dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!