Sending values using slider in MATLAB GUI to Arduino using serial communication

조회 수: 2 (최근 30일)
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);

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 23일
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
amin ali
amin ali 2022년 11월 1일
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
Walter Roberson 2022년 11월 1일
편집: Walter Roberson 2022년 11월 1일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 6월 23일
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
ATMAKURI V B S SATHKEERTH
ATMAKURI V B S SATHKEERTH 2021년 6월 23일
This is the Arduino code
// Using Pin 3
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
int dataNumber = 0; // new for this version
void setup() {
Serial.begin(9600);
Serial.println("Enter milliVolts 0 to 5000");
}
void loop() {
recvWithEndMarker();
showNewNumber();
int volts = (dataNumber * 0.051);
analogWrite(3, volts);
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
if (Serial.available() > 0) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewNumber() {
if (newData == true) {
dataNumber = 0; // new for this version
dataNumber = atoi(receivedChars); // new for this version
newData = false;
int volts = (dataNumber * 0.051);
float dataNumber2 = (dataNumber*0.001);
Serial.println(dataNumber2);
}
}

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by