How to Read and Write Serial Port Data in Raspberry Pi
Show older comments
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
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.
靖邦
on 13 Nov 2023
靖邦
on 13 Nov 2023
Walter Roberson
on 13 Nov 2023
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.
靖邦
on 13 Nov 2023
Walter Roberson
on 13 Nov 2023
I recommend you set the baud rate when opening the serial ports.
靖邦
on 13 Nov 2023
Walter Roberson
on 13 Nov 2023
Answers (0)
Categories
Find more on Message Queuing Telemetry Transport 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!

