Programmable power supply not able to send value through COM
3 次查看(过去 30 天)
显示 更早的评论
%Create a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM10', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('COM10');
else
fclose(obj1);
obj1 = obj1(1)
end
% Connect to instrument object, obj1.
fopen(obj1);
% Communicating with instrument object, obj1.
a=20;
a=num2str(a);
data1 = query(obj1, "VSET1:a");
I am not bale to send integer values to a , can anybody help please
0 个评论
回答(1 个)
Sivapriya Srinivasan
2023-10-6
Hello,
I understand to send integer values to your instrument using serial port object in matlab, want to convert integer to string before sending it .In this updated code, the integer value a is converted to a string a_str using the num2str function.
% Create a serial port object
obj1 = instrfind('Type', 'serial', 'Port', 'COM10', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('COM10');
else
fclose(obj1);
obj1 = obj1(1);
end
% Connect to the instrument object, obj1.
fopen(obj1);
% Communicate with the instrument object, obj1.
a = 20;
a_str = num2str(a); % Convert the integer to a string
command = "VSET1:" + a_str;
data1 = query(obj1, command);
% Close the serial port connection
fclose(obj1);
Then, the command string is constructed by concatenating the value with the command prefix. Finally, the command is sent to the instrument using the query function. By using the query function, you can send a command to the instrument and receive the response from it. The specific command and its expected response should be defined in the instrument's documentation or communication protocol.
Make sure to replace "VSET1:" with the appropriate command for your instrument. This code should allow you to send integer values to your instrument successfully.
Hope this helps!
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!