필터 지우기
필터 지우기

make a string to send it as a serial data.

조회 수: 15 (최근 30일)
Bhargav Jha
Bhargav Jha 2015년 9월 13일
댓글: Kubilay DUMAN 2021년 5월 10일
Hello I want to control servo from simulink using Serial Send block. The servos are connected to an arduino which receives position values in following format.
servoID:pos&servoID:pos&servoID:pos
for example 1:255&2:123&3:120 I have a code that calculates these values but i don't know how to send this values in precise string format. Please help me with this. Its really urgent and part of my final year project.
Thank you!

답변 (2개)

the cyclist
the cyclist 2015년 9월 13일
You should be able to use the sprintf command to format your string:
sprintf('%d:%d&%d:%d&%d:%d',[1 255 2 123 3 120])
I just used some explicit values for the second argument, but you can use variables there instead. If you carefully read the documentation, you'll see that the command has a lot of flexibility.
  댓글 수: 2
Bhargav Jha
Bhargav Jha 2015년 9월 16일
Thank you for the answer. But I need to do it using a simulink block. I tried to output a string from the matlab function block and it doesn't allow me to do that. I want to send this string to a arduino board using the serial port. Is there a way to do this in simulink?
Walter Roberson
Walter Roberson 2017년 5월 19일
Use a MATLAB function block and
output = uint8( sprintf('%d:%d&%d:%d&%d:%d',[1 255 2 123 3 120]) );
The result would be a uint8 vector, and those can be passed between blocks in Simulink.
Just watch out for fixed length vs variable length at the signal level.
Also if you are using any of the acceleration modes, then you will need to use one of the methods of declaring the vector to be variable length; see https://www.mathworks.com/help/fixedpoint/ug/defining-variable-size-data-for-code-generation.html

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


Iris José do Amaral Neto
Iris José do Amaral Neto 2017년 5월 19일
I want to send this string to a arduino board using the serial port. Is there a way to do this in simulink? Has anyone ever managed?
  댓글 수: 1
Kubilay DUMAN
Kubilay DUMAN 2021년 5월 10일
By using above blocks, I was able to send following string by serial communication;
"Pot_value:544 PWM:17 Rot:1"
Note that compose string block does not take unsigned values.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by