Sending values from Matlab to arduino using serial communication
이전 댓글 표시
I want to send numeric value from matlab to arduino but code is not working. Matlab code is as:
doi = 3 ;
arduino=serial('COM5','BaudRate',9600); % create serial communication object
fopen(arduino); % initiate arduino communication
fprintf(arduino, '%s', char(doi)); % send answer variable content to arduino
fclose(arduino);
Arduino code is as:
int solenoidPin = 13; //This is the output pin on the Arduino
int doi;
void setup()
{
Serial.begin(9600);
//pinMode(13, OUTPUT);
pinMode(solenoidPin, OUTPUT); //Sets that pin as an output
}
void loop()
{
if(Serial.available()>0)
{
doi = Serial.read();
//Serial.println(doi);
//Serial.println("\n");
//doi = doi - 48;
if (doi == 1)
{
//Serial.println(1);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
delay(1000); //Wait .15 Second
digitalWrite(solenoidPin, LOW);
}
else if (doi == 2)
{
//Serial.println(2);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
delay(2000); //Wait .165 Second
digitalWrite(solenoidPin, LOW);
}
else
{
//Serial.println(3);
digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
delay(3000); //Wait .180 Second
digitalWrite(solenoidPin, LOW);
}
}
}
I used str2num(doi) also instead of fprintf(arduino, '%s', char(doi)) but no output.
Please give suggestion to correct this.
Thanks.
댓글 수: 5
Walter Roberson
2017년 2월 18일
I would probably use
fwrite(arduino, uint8(doi));
Naseeb Gill
2017년 2월 21일
ahmed elshawadfy
2017년 11월 13일
how do you know that it takes 2 sec to open?
Sravani Vanama
2019년 11월 19일
how to print the values send from matlab to arduino??
Walter Roberson
2019년 11월 20일
There are a few possibilities for display:
- you can send the values to some kind of display such as an LCD display; https://www.arduino.cc/en/Tutorial/HelloWorld
- you can send the values back to MATLAB and have MATLAB display them
- if you use a different connection method between MATLAB and arduino, so that the communications between MATLAB and arduino is not through the serial port monitor, then you can send the values to the serial port and use some kind of monitor system on there.
- You can use a serial port (or USB port) monitor on the MATLAB host side to see what is getting sent. https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/capture-and-view-ing-usb-traces-with-microsoft-message-analyzer- . Or see https://freeusbanalyzer.com/ which permits short monitoring for free (and presumably there is a paid version.)
답변 (3개)
Walter Roberson
2017년 2월 21일
0 개 추천
"But what the problem is I do not know how to use _fopen(arduino) command between functions in GUI?"
fopen once and save the object somewhere so that you do not need to fopen again.
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
You could have a look at https://www.mathworks.com/matlabcentral/fileexchange/26371-simple-gui-for-serial-port-communication to see how they set up communications.
댓글 수: 1
Naseeb Gill
2017년 2월 21일
편집: Naseeb Gill
2017년 2월 21일
Rouis Jihene
2017년 5월 31일
0 개 추천
Hello, i tried the code but i don't get the value (doi) in arduino ! why? please help
댓글 수: 3
Walter Roberson
2017년 5월 31일
On the arduino side, you are probably trying to read the data as text instead of as binary. The replacement for
fprintf(arduino, '%s', char(doi)); % send answer variable content to arduino
should be
fprintf(arduino, '%d\n', doi); % send answer variable content to arduino
Rouis Jihene
2017년 6월 4일
Hello Mr Walter Roberson, even i use the instruction that you told me but i doesn't work ! please help
Walter Roberson
2017년 6월 4일
Sorry, I do not have an arduino to test with.
Azrg
2019년 10월 23일
0 개 추천
The thing you want to send has to be in a while loop like
while true
fprint(something);
end
Because Matlab has to keep trying to send the information until Arduino receives it.
카테고리
도움말 센터 및 File 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!