Raspberry pi stand alone UART

6 vues (au cours des 30 derniers jours)
Darren DeBattista
Darren DeBattista le 11 Juin 2021
HI,
I have an application where I'm using the Pi 2 to communicate with an MCU through UART to perform some tests. I've build a script to test the setup and all is fine. I than followed the example in "Getting Started with Deploying a MATLAB Function on the Raspberry Pi Hardware" to modify the script to be able to generate code for stand alone usage on the Pi 2.
When I run the script from the desktop (using Pi 2's UART, hardware in the loop), everything works. From there, I deploy the function and matlab tells me its succesfull without error. The issue is that the serial 'read' function is not working in standalone and it is returning a value of '0'. I am using the following syntax for thesting this issue.
Note that at the end, I save in a file on the Pi 2 in order to check the data. To be sure about the read values, I made an 'if' condition and save 0 or 1 depending on if the data recieved is 0 or not.
Anyone can help please?
Thanks
Edit: The read functions that are not working are the three consecutive read functionds. I now also tried to time the execution of these fucntions by placing 'tic' before the first function and reading 'toc' after the third and print the time in the file on the Pi 2. The time is around 300us. Consideriug that I set a timeout of 10s, this means that the three read function are being skipped somehow.
% Create a Raspberry Pi object
rpi = raspi();
% Create serial object
sObject = serialdev(rpi,'/dev/serial0', 38400);
sObject.Timeout = 10;
str = ['$SMV:', '4', ',F,', '15', '*', CR_NL]; % create string command for testing
write(sObject,str,'uint8'); % send command for testing
% Read reply until termination character. Automatically discard data
c = uint8(1);
while c ~= 10
c = read(sObject,1);
end
% Pause
pause(0.1);
% Another testing command
str = ['$SEV:', num2str(2), '*', 13, 10]; % 13, 10 means '\r \n'
write(sObject,str,'uint8');
% Read reply until termination character. Automatically discard data
c = uint8(1);
while c ~= 10
c = read(sObject,1);
end
% Give start command: MCU will now continously transmit data
str = ['$PRM:', '4', ',', '100', ',', num2str(1), '*', 13, 10]; % 13, 10 means '\r \n'
write(sObject,str,'uint8');
pause(0.2);
v = read(sObject, 1, 'int16');
cc = read(sObject, 1, 'int16');
w = read(sObject, 1, 'int32');
pause(1);
% Stop Test
str = ['$SMP:', '4', '*', 13, 10];
write(sObject,str,'uint8');
filename = 'motor_data.csv';
fileID = fopen(filename, 'w+');
if(v == 0)
fprintf(fileID,'%d',int16(0));
else
fprintf(fileID,'%d',int16(1));
end

Réponses (0)

Catégories

En savoir plus sur Installation Setup and Configuration dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by