read/write multiple serial devices in matlab
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am writing an algorithm in MATLAB that needs to extract data from three serial devices. I have been able to write interface for each device separately, however I need a multi - threaded code that will read each port at its natural update rate. The update rates vary from 1 Hz - 100 Hz. Can this multi- threading be performed in MATLAB? If yes how?
0 commentaires
Réponses (3)
Walter Roberson
le 20 Mar 2012
If that can be done, it would require the Parallel Processing Toolbox and one core (or hyperthread) per device (plus one main MATLAB thread).
The approach that would normally be used for a matter such as this would be to establish a BytesAvailableFcn callback for each of the serial devices. In general terms, that would cause a callback to be generated when data is ready from the device. The difference between this and what you asked is that MATLAB device callbacks are not multithreaded: you do not literally have several of them executing at the same time. Instead, generally speaking, one callback executes to completion, the next pending callback executes to completion, and so on.
Except... that there is no documentation about what happens inside device callbacks or timer callbacks if there are statements that would have the effect of voluntarily releasing control (e.g., drawnow), and there is no documentation about what happens if a timer callback goes off while a device callback is processing. There is some experimental evidence that Jan has put together that is suggestive that in newer versions of MATLAB, timer interrupts might be running in a separate thread, maybe even on a second core, but the few atomicity guarantees that have been made (in support cases) sort of contradict that unless there is an odd arrangement such as the timers are in a different thread but lock the main thread from proceeding at all until the timer thread finishes...
Basically, if you can get along with queued-interrupts of per-device callbacks, then that is the option that is most stable and best documented. But if you need (for example) substantial per-device computation per sample collected and need that computation to happen at literally the same time as the other devices are being handled, then you need to step in to the murkier world of Parallel Processing Toolbox. That or you hire Yair to work some Java-related magic for you.
Say... have you considered using National Instruments devices and R2010b or later with the new session-based interface? It can handle groups of devices at distinct input rates, if I recall correctly. Regrettably I do not have the hardware, toolboxes, or MATLAB version needed to experiment with this myself.
0 commentaires
tlawren
le 26 Avr 2012
How can the Parallel Computing Toolbox help? I am currently working on a project that involves reading two identical serial devices.
3 commentaires
Jerry Trantow
le 17 Mar 2014
I have two devices on two serial ports connected to the same BytesAvailableFcn callback. Each works on it's own but when both devices are sending data, I drop characters. (The input buffers are large enough that this shouldn't occur.) I can live with delays serving each port, but dropping the chars is causing problems.
I'm using R2010a. Serial ports spit out packets of 122 chars at 57600. I can change the packet rate from 25-100msec. Reliable receiving the chars is the first step in putting Matlab in the Middle of the communications.
Any suggestions would be appreciated.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!