How to Read and Write Serial Port Data in Raspberry Pi

41 views
Skip to first unread message

Jackson Zhang

unread,
Nov 13, 2023, 4:56:35 AM11/13/23
to Android-rpi
Hi,
I want Raspberry Pi to run a piece of code independently, read and write data from the serial port separately, and the other recipient is my PC. Can I directly deploy this code into it?I followed the Mathworks tutorial, but I don't know where the problem occurred. My current steps are as follows
Firstly, I created a connection with Raspberry Pi in MATLAB and sent a matrix
ThemeCopy
r = raspi;
pc = serialdev(r,'/dev/serial0');
dataToSend = [22;4;5;6;7];
byteStream = typecast(dataToSend, 'double');
Then,I will deploy the following code to Raspberry Pi to read data from the serial port and send some data to the serial port
ThemeCopy
function tablelook()%#codegen
r = raspi;
rsp = serialdev(r,'/dev/serial0');
output = read(rsp,5,'double');
...%my system code
write(rsp,1,'double')
(I obtain my final 0/1 through the five input parameters and output it to the serial port)
Finally,I read the data in MALTAB
ThemeCopy
read(pc,1,'double')
But I get wrong data during transmission,all of them is zeros.

Can you give me some advice?

Many thanks,
Bang
Reply all
Reply to author
Forward
0 new messages