Sending values using slider in MATLAB GUI to Arduino using serial communication
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
ATMAKURI V B S SATHKEERTH
am 23 Jun. 2021
Bearbeitet: Walter Roberson
am 1 Nov. 2022
I am trying to send the slider values into the arduino using serial communication, but below code is not working. Pls help
function slider1_Callback(hObject, eventdata, handles)
valor=get(handles.slider1,'value');
set(handles.edit1,'string',num2str(valor));
fwrite(handles.ser1,valor,'double');
guidata(hObject,handles);
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 23 Jun. 2021
fwrite(handles.ser1,valor,'double');
That transmits the binary value of valor as an IEEE 754 double precision integer. 64 bits would be transmitted. No line terminator would be sent.
void recvWithEndMarker() {
That code stores up to 32 characters, ending when a \n (newline) is found. The characters are only examined to see if they are \n and are otherwise uninterpreted. If more than 32 characters are found before the \n then it will keep scanning, and the newest character will replace the last character in the buffer.
void showNewNumber() {
That function calls atoi() to attempt to interpret the received characters as the text representation of an integer. If the text contains any characters that are not valid in integers, then 0 will be returned.
int volts = (dataNumber * 0.051);
that will take the interpreted integer, convert it to double precision, multiply by 0.051, convert the result to integer, and store the result in volts . volts will be integer data type. This calculated value will be discarded at the end of the if statement.
float dataNumber2 = (dataNumber*0.001);
that will take the interpreted integer, convert it to double precision, multiply it by 0.001, convert the resulting double precision to single precision, and store the result in dataNumber2 . That number will then be converted to representable text and displayed in the next line.
Notice that none of this code is expecting to deal with an IEEE 754 double precision number having been sent.
You should convert your
fwrite(handles.ser1,valor,'double');
to
fprintf(handles.ser1, '%d\n', round(valor));
3 Kommentare
amin ali
am 1 Nov. 2022
Hello
i want send by matlab 'X=120' to arduino not by fprintf i mean by fwrite(arduino, 'X=120', 'uchar')
Error using serial/fwrite (line 199)
Unsuccessful write: The number of bytes written must be less than or equal to OutputBufferSize-BytesToOutput.
Walter Roberson
am 1 Nov. 2022
Bearbeitet: Walter Roberson
am 1 Nov. 2022
BytesToOutput is the number of bytes already in the output buffer, waiting to be transferred. At the time you requested your fwrite() there were some bytes waiting to go out, and the new data you ask to write would overfill the buffer space.
I can tell from the fact that you are using fwrite() that you are using a serial() object rather than the newer serialport() object. For serial() objects you can configure an output buffer size; see https://www.mathworks.com/help/instrument/outputbuffersize.html
However, if you look at https://www.mathworks.com/help/instrument/fwrite.html you will see that "By default, data is written to the instrument synchronously and the command line is blocked until the operation completes."
That suggests that either you have a very small buffer configured, or else that you do not have an functioning connection to the device and bytes are not being transferred.
Weitere Antworten (1)
Walter Roberson
am 23 Jun. 2021
That code is okay in itself.
You do not need the guidata() call as you are not changing any fields of the structure handles, but having it just leads to slightly worse performance than you could otherwise have.
So what could be wrong? Well, it is common for the arduino side to have been programmed to expect text for values instead of binary. Coding it to expect binary is completely acceptable. But as outsiders we would need to see the code for the receiving side to give further opinion.
1 Kommentar
Siehe auch
Kategorien
Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!