Simulink Conversion from uint8 to ASCII/Char

조회 수: 14 (최근 30일)
K John
K John 2024년 9월 24일
답변: Deep 2024년 10월 22일
Hey everyone,
So basically in short what I need to happen is for my Seiral Recieve (Arduino Support Hardware) to take in an input from my Ardunio Mega RX1 pin (which is getting information from the serial monitor from my USB to TTL converter) and convert it to an integer that I can work with later on.
Currently what is happening is, when I inout numbers into the serial monitor, the scope of the Serial Receive output is clearly showing the bytes represntation of the numbers, for example i put in "1" and the Serial Receive is sending "49".
My question is,
How do I convert that data type from uint8 to something i can use later on (Need the actual number).?
How do I make it such it can read double or even triple digits too? (If i put in 24, it only reads the "2" and sends back 50, would love for it to send back 24. Do I use the terminator? if so what do i put in it, and how do i ignore the 0's).
Thanks guys!
  댓글 수: 1
Malay Agarwal
Malay Agarwal 2024년 9월 25일
Could you share more details of what the Data signal of the "Serial Receive" block outputs?
For example:
  • Does it output a ASCII stream which represents a single number?
  • Does it output multiple ASCII streams which represent different numbers?

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

답변 (1개)

Deep
Deep 2024년 10월 22일
The serial data is being sent as a string datatype, which is then interpreted as its ASCII values. It appears that you are using the default configuration of the "Serial Receive" block, which outputs data of fixed length 1. To handle multi-digit numbers, you should configure the block to use variable length data. This tutorial provides a detailed guide on how to achieve this for string data types: https://www.mathworks.com/help/simulink/supportpkg/arduino_ref/send-and-receive-data-of-variable-lengths-using-arduino-serial-communication-blocks.html. The tutorial also includes an example model that you can examine for better understanding.
Once you've configured the "Serial Receive" block to receive a variable-length string like "123", you can convert this string to a numerical data type using the "String to Single" block. More information on this block can be found here: https://www.mathworks.com/help/simulink/slref/stringtosingle.html.

카테고리

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

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by