serialport upload data using Ymodem protocol

13 vues (au cours des 30 derniers jours)
LucasBar
LucasBar le 9 Mai 2022
I must upload a data from a scientific device trought a serial port to my computer. I must use command line to be able to automatize this process. Therefore, I can not use the teraterm Ymodem sender and receiver function and I woud like to use matlab in this task.
The dataset is in C:\data\MyFile.dat inside the scientific device.
The device manufactor said me: you should use the Y-modem protocol.
I am able to read and write command to the scientific device sensor using fwrite, query, and fread.
However, I do not understand how communicate from sender and receiver.
Can you help me on that?

Réponses (1)

Sivapriya Srinivasan
Sivapriya Srinivasan le 6 Oct 2023
Modifié(e) : Sivapriya Srinivasan le 9 Oct 2023
Hello,
To communicate using the Y-modem protocol in MATLAB, you can use the serialport object and the write and read functions. Here's a step-by-step guide on how you can implement the Y-modem protocol for sending and receiving data:
1.Open the serial port:
port = 'COMX'; % Replace 'COMX' with the appropriate serial port name
baudrate = 9600; % Set the baud rate to match the device's configuration
s = serialport(port, baudrate);
2.Prepare the file for sending or receiving:
Sending: Open the file you want to send using fopen and obtain the file ID.
Receiving: Create a file to save the received data using fopen and obtain the file ID.
3.Sending data using Y-modem:
Send the initial file transfer request to the device:
fwrite(s, 'C'); % Send the initial request
pause(1); % Wait for the device to respond
4.Wait for the device to acknowledge the request:
response = read(s, 1); % Read the response from the device
if response == 'C'
% Device acknowledged the request, continue with the transfer
else
% Device did not acknowledge the request, handle the error
end
5.Send the file using the Y-modem protocol:
% Use the 'write' function to send the file data
write(s, fread(fileID, Inf, 'uchar'));
Receiving data using Y-modem:
6.Wait for the device to initiate the transfer:
response = read(s, 1); % Read the response from the device
if response == 'C'
% Device initiated the transfer, continue with the reception
else
% Device did not initiate the transfer, handle the error
end
7.Receive the file using the Y-modem protocol:
% Use the 'read' function to receive the file data
fwrite(fileID, read(s, Inf, 'uchar'));
8.Close the file and serial port:
Sending: Close the file using fclose.
Receiving: Close the file using fclose and save the received data.
Remember to replace 'COMX' with the correct serial port name and customize the code according to your specific requirements. Additionally, make sure to handle any errors or exceptions that may occur during the communication process.
By following these steps, you should be able to communicate using the Y-modem protocol in MATLAB to send and receive data from your scientific device through the serial port.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by