Daten einer spezifischen Adresse eines COM-Eingangs auslesen

1 回表示 (過去 30 日間)
Daniel Fischer
Daniel Fischer 2024 年 1 月 23 日
回答済み: Omega 2024 年 1 月 31 日
Ich möchte aus einem serialport-Objekt Daten einer spezifischen Adresse (dahinter befindet sich ein Motor, dessen Daten ich auslesen möchte) auslesen. Das Objekt zu erzeugen funktioniert, die Baudrate ist bekannt und es wird auch angezeigt, dass der COM-Port verwendet werden kann und nicht geblockt ist. Weitere Versuche, Daten aus dem Port zu ziehen, haben bisher nicht funktioniert und die Dokumentation zu 'serialport' hat mir da auch keinen Aufschluss geben können.
Über Impulse/Ideen würde ich mich freuen!

回答 (1 件)

Omega
Omega 2024 年 1 月 31 日
Hi Daniel,
You can follow the below mentioned steps in order to read data from a specific address on a serial port in MATLAB:
1. Create and configure the "serialport" object
% Replace 'COM3' with your actual COM port and '9600' with your baud rate
device = serialport('COM3', 9600);
% Configure additional serial port settings if necessary
configureTermination(device, "CR/LF");
set(device, 'Timeout', 5);
2. Open the connection: The "serialport" object in MATLAB automatically opens the connection upon creation, so no additional step is required to open the connection.
3. Send a command to request data: This step depends on the protocol used by your motor's controller. You'll need to send a command according to the device's protocol. For example, if the motor controller requires a specific string or binary command to send back data, you can use the "write" function.
% Example: Send a command to the device
% This command should be whatever your device expects
command = 'GET DATA'; % This is a placeholder; replace it with your actual command
write(device, command, "string"); % The data type depends on your device's protocol
4. Read the response: Now you can read the response from the device. The amount of data you want to read will depend on what you expect from the device.
% You can specify the number of bytes to read or read until a terminator is encountered
data = readline(device);
% If you know the exact number of bytes to read, you can use read() instead
% numBytes = 10; % Replace with the number of bytes you expect
% data = read(device, numBytes, "uint8");
5. Close the connection: After you're done communicating with the device, it's good practice to close the connection.
% The serialport object will be cleaned up automatically when it goes out of scope,
% or you can explicitly delete it if you want to close the port before that.
delete(device);
clear device;
Make sure you replace "GET DATA" with the actual command your motor controller expects, and adjust the read and write functions according to the expected data types and amounts.
If you are still having trouble, ensure that you are using the correct commands as per your device's manual, and that the device is properly connected and configured to communicate at the baud rate you have set. If your device uses a binary protocol, you will need to use the appropriate binary commands with "write" and "read" instead of "writeline" and "readline".
For reference you can go through this example of "Read streaming data from arduino": https://www.mathworks.com/help/matlab/import_export/read-streaming-data-from-arduino.html
Here's the documentation link for "serialport" https://www.mathworks.com/help/matlab/ref/serialport.html

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!