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

2 Ansichten (letzte 30 Tage)
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.

Antworten (1)

Sachin Lodhi
Sachin Lodhi am 29 Dez. 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

Kategorien

Mehr zu Installation Setup and Configuration finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by