![]() ![]() indicate good connection status, current state, etc.). I would like to a create a status window for this program such that a user knows that it is function properly (e.g. This script contains an infinite loop that monitors a communication thread for incoming commands. Now your main function, which starts when you click the start button, runs the loop and just checks the toggle buttons' states to determine whether to send and receive.I'm currently working on a MATLAB script call manager.m. Guidata(hObject,handles) % Update GUI data In the Callback, you can change what they display: function send_Callback(hObject,~,handles) This will make them switch their 'Value' property (which you read in the above loop) between 0 and 1 when you click them. ReceiveFlag = get(handles.receivetoggle, 'Value') Īlso, create send and receive toggle buttons and set their 'Min' to 0 and 'Max' to 1. Handles = guidata(hObject) % Updates handles structure I don't exactly know how GUIDE handles the OpeningFcn, so you should probably put a "Start" button into your GUI which basically executes the above program with a few changes: function startbutton_Callback(hObject,~,handles) ![]() Using GUIDE, you want to use toggle buttons. Then in the Callbacks for your Buttons (you could use Toggle Buttons by the way), you simply set the sendFlag and receiveFlag, respectively. If you're fine with building a GUI programmatically, try this: function main() Nesting the Callbacks will make sure they can access and change the local variables without making them global. What you need is a main program where you execute your loop. The first loop will wait for the second Callback to terminate until it can resume. So when the second button is clicked, the second callback will start and loop infinitely, until it is ended. Your problem is that you're executing the loop inside the Callbacks. ![]() You can of course lower the period to your needs, I chose the above for testing purposes. SendFcn and recvFcn here then should contain the code you have within your according while loops. Uicontrol(f, 'position',, 'Callback', stop(recvTimer), 'string', 'stop2') Uicontrol(f, 'position',, 'Callback', start(recvTimer), 'string', 'start2') Uicontrol(f, 'position',, 'Callback', stop(sendTimer), 'string', 'stop1') Uicontrol(f, 'position',, 'Callback', start(sendTimer), 'string', 'start1') RecvTimer = timer('TimerFcn', 'ExecutionMode', 'fixedDelay', 'Period', period, 'TasksToExecute', Inf) SendTimer = timer('TimerFcn', 'ExecutionMode', 'fixedDelay', 'Period', period, 'TasksToExecute', Inf) Period = 0.2 % period in seconds, in which the timer shall execute as in the following minimalistic example: function cbtest() If you want to stick with matlab, the closest to thread would be timers.Į.g. You can't do this matlab however - unless you're e.g. ![]() In a "proper" programming language, you'd have a sending and a receiving thread running in parallel. You can only slightly modify this procedure using the BusyAction property: So when callback1 is running and you trigger callback2, callback2 will interrupt callback1.Ĭallback1 will then only proceed once callback2 is finished. But even if my requirements are fulfilled (as per my question) it would work.Ĭallbacks in Matlab all run on the same thread. It would be better if I could work without global variables. I have tried to find a solution referring to internet, but most of the solutions are using global variable. My sample code for clarification: function send_Callback(hObject, eventdata, handles)įunction haltSend_Callback(hObject, eventdata, handles)įunction receive_Callback(hObject, eventdata, handles)įunction stopReceive_Callback(hObject, eventdata, handles) But I need to use them both at the same time and both of them will continuously send and receive data until I click other button (s) to exit the infinite loop. Now if I click portion2 then portion1 will be stopped. My question: Problem is when I am using global variables and the above mentioned way of using infinite loop, if I click portion1, portion1 will start iterate. I have used global variables and while loop (with boolean) to exit the infinite loop, as I need to send the data or receive data continuously. Both the portions are using two buttons one to start the work of that particular portion and one to stop the work. Another portion (portion2) would receive signal from serial port which have been received from other devices. So once I will click send it would send one data to my serial port. In 1 portion (portion1) I have text input and send button. Background: I have several portion in an GUI to handle different tasks. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |