Multi Threads operation SERIAL PORT in Parallel
15 次查看(过去 30 天)
显示 更早的评论
Hi guys,
I hope you're all safe and healthy during this period.
I have something to ask you. I need to open two serialport objects in order to communicate with 2 Arduinos. One of that has to send me Data every 10 ms (100Hz) and the other one each 100us (10KHz). These data reiceved have to be synchronize. I wouldn't like to use 2 PCs and I'd like to know if there's some way to read data in this way.
So basically 2 obj and 2 Arduino to read at the same moment. I need to do post analysis and they need to be sync.
Thanks a lot, your help will be really appreciated.
0 个评论
回答(2 个)
Walter Roberson
2020-5-27
That cannot be done with asynchronous serial ports such as RS232 and USB. In order to synchronize reception of data, you need to use synchronous data ports such as Bisynch https://en.wikipedia.org/wiki/Binary_Synchronous_Communications
But supposing you had synchronous data ports, and supposing you had a hardware trigger such as Genlock and suppose it was sourced "exactly" half-way between the two arduino so that they could be fired at "exactly" the same time to send the signal out over the synchronous data connection.
Supposing you had those things: you will still have the problem that the receiving processes are going to react a different speeds. That is true even if you have used your operating system to pin each process to a different processor that has its own interrupt subsystem so that they can react at the same speed (and you have no hope if you have not taken that step.)
Really synchronizing events between processes is difficult to do on a general purpose operating system. If you really need to synchronize then you should not be using standard MATLAB to run the tasks -- you should be using a real-time operating system, perhaps programmed using Simulink Real-Time. Which runs on dedicated hardware.
A lot of the time, it turns out that you do not need to get two sources at the same time: that instead, you need to get from them within a particular non-zero time window of each other. Depending on the time window sometimes it is not especially difficult to do.
And a lot of the time, it turns out that what you need is not really that you get the sources at the same time, but rather that you timestamp when you got the samples, that by knowing when things were happening, you can make appropriate predictions.
If you have a situation where the inputs from the arduinos are being used to calculate thrust for multiple rocket motor and so it is vital that the relative timings of the actions be known, then (A) MATLAB is not certified for that kind of work! and (B) you need real-time computing, not a general purpose system like MATLAB.
0 个评论
valio
2020-5-27
3 个评论
Walter Roberson
2020-5-27
编辑:Walter Roberson
2020-5-27
The connection between Arduino and MATLAB is not by standard serial port: it is by serial over USB.
The limit for USB is 1000 transactions per second. This is the basic polling rate built in to USB.
Normally the USB serial drivers buffer data until a timeout or the packet is full, designed for efficient use of the USB bus bandwidth and NOT designed for real time use. USB 3 adds some possibilities for interruptions that were not present before, but that requires a USB 3 port being run at USB 3 rates, which arduino is not designed for.
1000 Hz is thus the maximum rate. The actual rate that can be achieved is noted variously as roughly 4 Hz (if you use read analog) or 40 Hz (if you just have the arduino send bytes) with some hints that a few people reach closer to 75 to 100 Hz.
There is an Arduino clone, I think it is Redboard, that can archive higher rates. The way that most official Arduino communicate with USB internally is not so good.
I recently received my arduino but have not had a chance to test it yet, so I am relying on what I have read... but what I have read suggests that 10kHz is Definitely Not Going To Happen with arduino. I have not read as much about Raspberry Pi; I do not have figures at the moment about rates actually achieved.
Oddly enough, if you were using a device with a true serial port then 4 bytes at 10 kHz would be comparatively easy. However that would still require 400000 baud and if you want to avoid buffering then it is probably better to switch to Ethernet. Ethernet can handle 10 kHz without difficulty (but timestamps as they come in can be a problem.)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!