How to Read and Write Serial Port Data in Raspberry Pi

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:
r = targetHardware('raspberry')
deploy(r,'tablelook')
function tablelook()%#codegen
r = raspi;
pc = serialdev(r,'/dev/serial0');
dataToSend = 1;
byteStream = typecast(dataToSend, 'uint16');
write(pc, byteStream);
output = read(pc,5,'uint16');
end
Raspberry Pi receives a 5 * 1 matrix from the PC and sends a 0/1 logic judgment to the PC.The code can run,but the data is incorrect,it's all zero.I dont know why it cannot work.
Many thanks,
Bang

8 Comments

dataToSend = 1;
byteStream = typecast(dataToSend, 'uint16');
byteStream
byteStream = 1×4
0 0 0 16368
That is a 1 x 4 array of uint16, not a 5 x 1 matrix of anything.
Note: when you use write() then no delimiter is sent. And if a delimiter were being sent you would have to worry about the fact that delimiters are typically one byte long, uint8, not uint16 like the other data you send.
What I mean is that I want to put this code into Raspberry Pi and run it independently,My input is a 5 * 1 double precision matrix, which needs to be converted. I just gave an example here.When I send data from my PC, my serial communication assistant can detect data transmission, but the transmitted data is 0.
I can change the data type to double,but my question is why the data I detected through my assistant is always zero?
(All my code is deployed to run in Raspberry Pi, not in Matlab)
function tablelook()%#codegen
r = raspi;
pc = serialdev(r,'/dev/serial0');
dataToSend = 1;
byteStream = typecast(dataToSend, 'double');
write(pc, byteStream);
output = read(pc,5,'double');
That code is expecting the other end of the serial port to be writing back something -- but you do not show us the code sending data to the raspberry.
I know what your mean,here is my Complete process:
Firstly, I created a connection with Raspberry Pi in MATLAB and sent a matrix
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
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
read(pc,1,'double')
But I get wrong data during transmission,all of them is zeros.
I recommend you set the baud rate when opening the serial ports.
There is anthoer way to detect the raspberry pi's serial port is open?My baud rates same,and others is same too.Here is my raspberrry pi parameter and PC parameter:

Sign in to comment.

Answers (0)

Asked:

on 13 Nov 2023

Commented:

on 13 Nov 2023

Community Treasure Hunt

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

Start Hunting!