Slow communication speed with Arduino

27 次查看(过去 30 天)
Hi,
I am using a MAX6675 to measure the temperature of thermocouple. I tried to measure the temperature in two ways. Measure ways: 1. Arduino IDE and 2. Matlab read temperature by using a MAX6675 connected to Arduino. Their execution time is quite different.
%Arduino
void loop() {
unsigned long timeBegin = micros();
float temperature = module.readCelsius();
unsigned long timeEnd = micros();
unsigned long duration = timeEnd- timeBegin;
Serial.print("Temperature: ");
serial.print(temperature);
Serial.println(F("c"));
serial.print("Duration time (us):");
serial.println(duration);delay(1000);
}
The exercution time for Arduino IDE is about 520 microseconds.
%Matlab read temperature by using a MAX6675 connected to Arduino
tic;
T=readCelcius(a,cs,sclk);
toc;
disp(T);
The Matlab elapsed time is 1.616992 seconds.
The execution speed of these two ways are different by 3100 times!! While they are using the same function readCelcius.
How can I make the speed of connecting Arduino to the Matlab execute much more faster?
Any suggestion will be appreciated. Thank you!

采纳的回答

Walter Roberson
Walter Roberson 2022-12-24
To use readCelcius from MATLAB you need to have used arduino() first. The function then places a spiread() call on the arduino object and manipulates the results.
The arduino() interface works by having a command monitor on the arduino side, and the MATLAB side sends a command to the arduino side, which the arduino side has to interpret and place the appropriate commands locally, and then the arduino side has to package the results and send them back, and the MATLAB side has to decode and return appropriate results.
This requires multiple encoding and decoding steps, and requires two or more trips across the USB/serial interface, which involves a packetized command-and-response protocol.
This is, of course, much slower than native code on the arduino that can make direct calls.
I would not expect the slowdown to be as high as you are observing, but it is going to be notably slower.
You can reduce the time by having a custom sketch on the arduino side that listens for requests (anything i/o will do) and runs the spi commands directly and decodes to a temperature that you send back. This would reduce encoding and decoding time but not bus time.
  3 个评论
Walter Roberson
Walter Roberson 2022-12-24
It would be similar to what you already have, but you would add code that looped waiting for input, calling module.readCelsius() and writing out the response. To reduce decoding time, just send the temperature back without the text header. For technical reasons if you can get the response to be 4 bytes or fewer then the exchange can be more efficient (at least in theory)
gdz
gdz 2022-12-25
you would add code that looped waiting for input, calling module.readCelsius() and writing out the response
Everything of the code is in Matlab now. Is this means I have to adding code in the arduino loop, that calling for module.readCelcius? Also the code in the arduino have to run first (module.readCelcius is reading now) and the matlab will read the reading on arduino side and display on Matlab.
However, this is different from understood. From my understanding, it is not able to run both arduino and matlab at the same time, because there is only one port for my PC.
Please correct me if I'm wrong.

请先登录,再进行评论。

更多回答(1 个)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022-12-24
编辑:Sulaymon Eshkabilov 2022-12-24
Note that in Arduino IDE: delay() command does not give a fixed sampling time.
You had better use: millis()
You can also try to use Data Streamer MS Excel app to record your Arduino Data.
  2 个评论
gdz
gdz 2022-12-24
I have tried to replace micros() to millis(), millis() gives 0 or 1 only.
With the same function readCelcius, they both give a very different elaspse time.Weird....
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022-12-24
Use Data Streamer MS Excel app to record your Arduino Data that gives very accurate data export from Arduino into MS Excel.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Instrument Control Toolbox 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by