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!
0 个评论
采纳的回答
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
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)
更多回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Instrument Control Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!