Sending one digit variable serially

1 回表示 (過去 30 日間)
Leon Ellis
Leon Ellis 2021 年 10 月 9 日
回答済み: Atithi 2023 年 6 月 22 日
Good day, I'm trying to send the variable x through serial to my arduino. The only problem is, when i monitor my serialport, the value of x doesn't appear as it should and I need it to. This is my very simplified code:
clear;
x=6;
x=x+3;
x;
s=serialport("COM1",9600);
pause(1);
write(s,x,"char");
In my serial monitor, it only creates a bunch of spaces and I've tried all the different types of variable types, i.e "uint", "int", "single" etc availeble. However, as you can see the varaible x should be 9 and I want to send 9. When I use single, I get values on the ASCII table, however subtracting the right amount to achieve the '9' doesn't seem to work. Any help would be much appreciated!

回答 (1 件)

Atithi
Atithi 2023 年 6 月 22 日
There might be some error in write() function, it would be better if you send the values using "uint8" instead of using "char"
clear;
x=6;
x=x+3;
x;
s=serialport("COM1",9600);
pause(1);
write(s,uint8(x));
  1. uint8 is a MATLAB function that maps an input number or array onto the range of 0 to 255 using only 8 bits of data.
  2. It converts the input into an unsigned 8-bit integer, where each bit in the binary representation represents a power of two.

カテゴリ

Help Center および File ExchangeSerial and USB Communication についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by