How send ASCII request to serial device connected to my Raspberry Pi ?

2 次查看(过去 30 天)
Hello,
For a university project, I need to transfer a Matlab program to my Raspberry card, to which 2 USB devices are connected.
For the communication, I thought to use the Matlab serial function, but this one is not compatible with the Rasberry environment.
I tried to use the serialdev function, but I have some difficulties because I need to send an ASCII request, and the write function of the serialdev class does not allow to do it.
The serial class had the fprintf function which allowed me to send ASCII.
Is there a way to accomplish this? Thanks in advance.

回答(1 个)

Sachin Lodhi
Sachin Lodhi 2023-12-29
Hello Alexandre,
In the Raspberry Pi environment with MATLAB, you can send ASCII data by converting your string into the appropriate uint8 (unsigned 8-bit integer) format before using the ‘write’ function.
asciiString = 'AB';
dataToSend = uint8(asciiString)
write(serialDevice, dataToSend, "uint8");
Converting data to uint8 before sending it over a serial connection is a common practice because serial communication fundamentally operates on bytes. When you convert an ASCII string to a uint8 array in MATLAB, each character in the string is converted to its corresponding integer value as defined by the ASCII standard.
For example, the ASCII value for the character 'A' is 65, 'B' is 66, and so on. When you convert the string 'AB' to uint8, you get an array with elements [65, 66]. These values can be directly transmitted over a serial connection as bytes, and the receiving device, which also understands ASCII, will interpret these bytes back into the corresponding characters.
Hope this helps.
Best Regards, Sachin

类别

Help CenterFile Exchange 中查找有关 Installation Setup and Configuration 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by