Simulink Conversion from uint8 to ASCII/Char

12 ビュー (過去 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 Center および File ExchangeModeling についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by