Problems with GUI Callback Error

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Error produced once pressing the button "verbinden"
>> versuchsstand
Serial Port Object : Serial-COM1
Communication Settings
Port: COM1
BaudRate: 9600
Terminator: 'LF'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
Serial Port Object : Serial-COM2
Communication Settings
Port: COM2
BaudRate: 9600
Terminator: 'LF'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
Error using serial/fopen (line 72)
Open failed: Port: COM2 is not available. No ports are available.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.
Error in versuchsstand>verbinden_Callback (line 98)
fopen(steuerung);
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in versuchsstand (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)versuchsstand('verbinden_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback

Answers (1)

Harneel - rather than pasting the m file in your question, just attach it to your question using the paperclip button. Or just copy and paste the verbinden_Callback callback. There is nothing wrong with the callback but there is a problem with the serial port as it seems to still be available even though you have closed it. You may have to delete it as described at a similar question http://www.mathworks.com/matlabcentral/answers/9509-serial-port-is-not-available-in-gui-but-it-actually-available-in-my-pc. Try changing your code to the following
function verbinden_Callback(hObject, eventdata, handles)
% Kraftmesser initialisieren
kraftmesser = instrfind('Type','serial','Port','COM1','Tag','');
if ~isempty(kraftmesser)
fclose(kraftmesser);
delete(kraftmesser);
end
kraftmesser = serial('COM1');
% Steuerung initialisieren
steuerung = instrfind('Type','serial','Port','COM2','Tag','');
if ~isempty(steuerung)
fclose(steuerung);
delete(steuerung);
end
steuerung = serial('COM2');
fopen(steuerung);
fwrite(steuerung,'#1g=+1/r');
fclose(steuerung);
What happens when you now run your GUI? Also review your code for the COM1 serial port. It isn't clear why you need it since you never actually try to write anything to it.

10 Comments

Hi Geoff - I tried to upload the .m file however it does not seem to work. I edited the program according to your sugestion however i get more or less the same error:
I am very new to matlab and programming, however i have to use the above program which was give to me to run a test rig for my Thesis.
I get the following error after testing the program.
>> versuchsstand *Error using serial/fopen (line 72) Open failed: Port: COM2 is not available. No ports are available. Use INSTRFIND to determine if other instrument objects are connected to the requested device.
Error in versuchsstand>verbinden_Callback (line 102) fopen(steuerung);
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in versuchsstand (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)versuchsstand('verbinden_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
>>*
Harneel - use the paperclip button to select your file, and then press the Attach File button. As for your error message, what are you trying to read off of COM2? Is there anything already connected or using it?
Hi Geoff- I have attached the files.
I now tested the program on the Computer connected to the test rig (sorry for the confussion). I only got access to the test rig today.
So the two devices that are connected are the following:
a. COM1 is a Force Measuring Device (PCE) Instrument. b. COM2 is a Controller that runs a Motor on the test rig.
after running the program and pressing 'Verbinden' i got a simular error:
_>> versuchsstand Error using serial/fopen (line 72) Open failed: Port: COM2 is not available. Available ports: COM5, COM6, COM7. Use INSTRFIND to determine if other instrument objects are connected to the requested device.
Error in versuchsstand>verbinden_Callback (line 102) fopen(steuerung);
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in versuchsstand (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)versuchsstand('verbinden_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
>> instrfind
Instrument Object Array
Index: Type: Status: Name:
1 serial closed Serial-COM1
2 serial closed Serial-COM2
Should i therefore just Change the COM port numbers in Matlab to match the COM port numbers assigned to the devices? i.e
COM7 for the Controller and COM5 for the force measuring device
Harneel - are the two devices actually connected on ports 5 and 7, or 1 and 2?
Ok the devices have there stand alone programs i.e
a. Force Gauge has a software called PCE-FG
b. Controller has a Software called Nano Pro
Through the individual Softwares i checked the COM ports
a. The Force gauge is actaully configured to COM port 6 with a Baud rate of 57600 bps
b. The Motor is actually configured to COM port 7 with a Baud rate of 115200 bps
So i changed this accordingly on the matlab Program i got the following message when pressing "Verbinden"
>>versuchstand
Serial Port Object: Serial-COM6
Communication Settings
Port: COM6
Baud Rate: 9600
Terminator `LF´
Communication State
Status: Closed
Record Status: Off
Read Write State
Transfer Status: idle
Bytes Available: 0
Values Recieved: 0
Values Sent: 0
Serial Port Object: Serial-COM7
Communication Settings
Port: COM7
Baud Rate: 9600
Terminator `LF´
Communication State
Status: Closed
Record Status: Off
Read Write State
Transfer Status: idle
Bytes Available: 0
Values Recieved: 0
Values Sent: 8
The baud rate is not the same and all the ports are Off and closed
Based on your previous code, the above output occurs before you open the ports. Is this still the case? What happens when you call
instrfind('Type','serial','Port','COM6','Tag','');
instrfind('Type','serial','Port','COM7','Tag','');
after you have opened COM7?
Im not so sure what you mean by opening the ports. But what i always do is test the devices through their individual device manger softwares just to check which com ports get assigned. After which i close the programs and then open matlab.
I tried the above call in the command window and i get the same Output as given before.
So are you still experiencing errors even though you are using different ports? And are they the same errors as before?
Yes i get the same Errors when using COM6 and COM7. Could it be an issue with the Windows Device Manager and not Matlab?

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!