필터 지우기
필터 지우기

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

조회 수: 5 (최근 30일)
Alexandre SPRICH
Alexandre SPRICH 2022년 4월 28일
답변: Sachin Lodhi 2023년 12월 29일
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에서 Raspberry Pi Hardware에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by