How send ASCII request to serial device connected to my Raspberry Pi ?
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Sachin Lodhi
on 29 Dec 2023
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
0 Comments
See Also
Categories
Find more on Installation Setup and Configuration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!