xpress_embedo
Advanced Member level 4
Hello! Everyone
I am trying to make a GUI, which periodically sends some packet over Serial Port.
I need to send this packet 10 times, and if there is no response from Serial Port the timeout will occur.
I made a variable *handles.TimeOutCounter* in the Opening Function, with the intention that, i will increment it every time and reset it to zero whenever there is data from Serial Port.
Currently i haven't implemented the Serial Reception CallBack function yet, so this function must transmit the data for 10 times, but it is sending data just one time and after that it gives some error.
The code is as follow:
Hope you all understands my intention, what i want to do.
Please help me, i am stuck in this problem last few days.
I am trying to make a GUI, which periodically sends some packet over Serial Port.
I need to send this packet 10 times, and if there is no response from Serial Port the timeout will occur.
I made a variable *handles.TimeOutCounter* in the Opening Function, with the intention that, i will increment it every time and reset it to zero whenever there is data from Serial Port.
Currently i haven't implemented the Serial Reception CallBack function yet, so this function must transmit the data for 10 times, but it is sending data just one time and after that it gives some error.
_Error while evaluating TimerFcn for timer 'timer-1'
H must be the handle to a figure or figure descendent._
The code is as follow:
Code Visual Basic - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 % --- Executes just before UltraSonicSensor is made visible. function UltraSonicSensor_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to UltraSonicSensor (see VARARGIN) % Choose default command line output for UltraSonicSensor handles.output = hObject; axes(handles.axes1) [SensorImage, SensorMap] = imread('hc-sr04.bmp'); imshow(SensorImage, SensorMap) axis off axis image % Delete any opened Ports delete(instrfind); % Create Serial Object at 9600 BaudRate handles.serial = serial('COM1','BaudRate', 9600); handles.serial.BytesAvailableFcnMode = 'terminator'; % Serial Reception Callback Funtion handles.serial.BytesAvailableFcn = @Serial_Receive; % Open Serial Port fopen(handles.serial); % Counter Variable handles.TimeOutCounter = 1; % Delete any previously defined timer delete(timerfind); % Starts a 1 second timer handles.timer = timer; handles.timer.TimerFcn = {@Timer_1Second, handles}; handles.timer.StartDelay = 5; handles.timer.ExecutionMode = 'FixedRate'; start(handles.timer); % Update handles structure guidata(hObject, handles); % UIWAIT makes UltraSonicSensor wait for user response (see UIRESUME) % uiwait(handles.GUI1); % --- Outputs from this function are returned to the command line. function varargout = UltraSonicSensor_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % Function gets called after 1 second function Timer_1Second(hObject, evendata, handles) handles.output = hObject; % Construct Packet to Send My_Address = 14; % 0x0E Trans_Header = 44; % 0x2C Trans_Destination = 36; % 0x24 Trans_Source = My_Address; Trans_Length = 2; Trans_OpCode = 1; Trans_Data = 1; data = [Trans_Header,Trans_Destination,Trans_Source,Trans_Length ... ,Trans_OpCode,Trans_Data]; % Computer Checksum Trans_Checksum = Compute_Checksum(data,6); % Append Checksum at the end of the Packet data(end+1) = Trans_Checksum; % Check whether the Serial Port is Open if strcmp(get(handles.serial,'Status'),'open') % Send data over Serial Port fwrite(handles.serial,data,'uchar'); % Increment Counter Variable handles.TimeOutCounter = handles.TimeOutCounter + 1; % Debugging Purpose, Print the value on Serial Port fprintf('Value is = %d\n ',handles.TimeOutCounter); end if handles.TimeOutCounter > 10 % Timeout Error, No Data receive in desired time. stop(handles.timer); fclose(handles.serial); delete(instrfind); end % Update handles structure guidata(hObject, handles);
Hope you all understands my intention, what i want to do.
Please help me, i am stuck in this problem last few days.