Go Down

Topic: C++ or MATLAB Communication Speed (Read 496 times) previous topic - next topic

nadarkins

I am using Arduino to control a broken out atomic force microscope. I have created a MATLAB GUI to control it. However, serial communication between MATLAB and Arduino is very slow. I am using a scanning motion most of the time so I need to send a lot of voltages back and forth.

The MATLAB command readVoltage() currently takes .3 seconds to complete. Running a scan of 10,000 data points is very time consuming.

I am considering controlling it using C++. Will it be any faster than MATLAB? Are there any other languages with relatively simple GUI functions that would work faster?

spycatcher2k

What speed have you set the uart to?
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.


spycatcher2k

Have you tried going up to 2500000
Mrs Drew
-=-
http://www.uk-pcb.co.uk - UK PCB Fab Company
I will design & code for you, but I will also charge you (PM me)
If you don't like my answers, realize : I'm not being cheeky, I'm Just trying to prompt you to use your own brain/google etc.

PieterP

What Arduino are you using? I've noticed that using a Leonardo is extremely slow (when compared to an ATmega16U2 or FT232), regardless of the software being used (I tried MATLAB, C++, Python, NodeJS and even just cat).

Pieter

horace

the MATLAB Support Package for Arduino Hardware function readVoltage() could well take .3 if the protocol for error correction etc is complex.
try increasing the baudrate as suggested by @spycatcher2k in post #2
alternatives to build your GUI  are Visual Studio (C++, C#, VB using the SerialPort component), Java (using jSerialComm), Python, etc but you would probably have to write the Arduino code and design you own protocol to transfer the data

Robin2

What Arduino are you using? I've noticed that using a Leonardo is extremely slow (when compared to an ATmega16U2 or FT232),
I obviously do not know what part of the process you are referring to when you say "extremely slow" but it is unlikely to be serial communication because a Leonardo does that at USB speed regardless of the baud rate setting.

@nadarkins, if you are sending small packets of data (less than 64 bytes) then USB can be surprisingly slow because it works in packets. If that is the cause of the poor performance try accumulating several pieces of data and sending them as a single message.

I have not tried communicating with a Mega or Uno at 2,500,000 baud as suggested by @Spycatcher but my regular baud rate for communication with my Python programs is 500,000 baud.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PieterP

I obviously do not know what part of the process you are referring to when you say "extremely slow" but it is unlikely to be serial communication because a Leonardo does that at USB speed regardless of the baud rate setting.
Exactly, that's why it surprised me that it was so slow.

I just used a sketch that used Serial.write(0); in a while loop, and I struggled to get more than a 100 bytes/second out of it on the Leonardo. The UNO on the other hand, had no problem sending the 720 bytes per second I needed @1Mbaud (as one would expect).

However, I think the problem the OP is facing lies in the fact that he uses the MATLAB Arduino functions, rather than just sending data over the Serial link directly.

horace

if data transfer speed is critical why not transmit in binary taking note of possible data size differences, numeric representation, big or little endian, etc

nadarkins

Setting the baud rate to 2500000 helped speed up the voltageRead() to .2 seconds. Still not a huge change though. Can someone using python or C++ with 2500000 baud rate time their voltage read? I curious to see if MATLAB is whats causing it to calculate so slowly.

PieterP

Don't use the voltageRead() MATLAB function (or whatever it's called). Read and write to the serial port directly.

Doing so, in my experience, there is no difference between Python, C++ and MATLAB.

Using the Arduino MATLAB package is going to slow things down a lot.

nadarkins

@pieterP How do I communicate with the port directly. Any instructional links you have would be much appreciated.

PieterP


nadarkins

I am using MATLAB and Arduino to control an atomic force microscope I am doing research on. I am running a GUI through MATLAB. I was previously using the Arduino IO Support package, however my imaging software requires a lot of data collection and analogread() takes a long time to communicate to the device. (I have increased baud rate to no avail).

The communication between MATLAB and Arduino is very slow using the support package so I have begun using serial communication to run both Arduino IDE and MATLAB simultaneously. Sending variables generated by my GUI to Arduino IDE, using the serial communication, gives me a lot faster results. However each time I have to open the serial connection it is very slow.

The serial communication will give me faster results for things like imaging and engaging my probe because I have to use large for loops to step through my controllers resolution values. My GUI also allows for immediate control of the probe. If I only need to send one writeDigitalPin() it is much faster to use that than wait for the serial port to open.

My serial communication works properly when I don't open the arduino communication function written into the support package. However, opening the arduino() function in matlab closes my arduino.ino file because the port can't open both methods at the same time.

Is there a way to use both methods of communication. Can I close the arduino function and open the .ino file with MATLAB? How is this done? I know you can open .exe files but the .ino files do not seem to work. All advice is appreciated.

Go Up