0

I have a trouble with interfacing pic microcontroller with pc using MATLAB GUI Matlab gives me this error "Warning: A timeout occurred before the Terminator was reached "

Here is the mikroc code:

void main() 
{
    char x,send;
    TRISD = 0x00;
    TRISB = 1;
    while(1)
    {
        if (PORTB.f0==1)
        {
            UART1_Init(9600);
            Delay_ms(100);
            UART_Write('s');
            x = 0;
            while(x==0)
            {
                x = UART1_Read();
                if (x=='y')
                {
                    PORTD.f0=1; Delay_ms(1000); PORTD.f0=0;  
                }
                else if (x=='n')
                { 
                    PORTD.f1 = 1;  
                    Delay_ms(1000);  
                    PORTD.f1 = 0;
                }
            } 
       }
  }

}

and here is the matlab code :

function pushno_Callback(hObject, eventdata, handles)

delete(instrfind);
s = serial('COM4');
set(s,'BaudRate',9600,'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl',   none','Terminator','CR');

s.BytesAvailableFcnCount = 1;
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcn=@mycallback;
handles.pushno=s;
fopen(s);

function mycallback(hObject, event, handles)
s=handles.pushno;
out=fscanf(s);
if (out=='s')
pause(1);
fprintf(s,'y');
end 
fclose(s);
delete(s);
clear s;
Mike
  • 4,041
  • 6
  • 20
  • 37
  • In the matlab code, you create the `serial` object and open it _inside_ the `while` loop (and yet you only close and clear it once _outside_ the `while` loop). You should only declare your serial object one time (outside of any loop). Also your loop is infinite (`x==0` is always true), so you'll never close you port properly. It is better to assign a callback to the `BytesAvailableFcn` of your serial port. This will be executed whenever the serial port receive something. There you can check what is it then send back the appropriate reply. – Hoki Nov 05 '14 at 15:02
  • i edited the matlab code . I get no errors but still the microcontroller cannot receive any data from the matlab . – user3411910 Nov 05 '14 at 16:43
  • does Matlab receive the message from the micro ? you can try to add something like `disp(['COM4 RX msg: ' out])` instruction in your callback to be notified in the console of what the serial is seeing on the Matlab side. Also, I would not close the serial port at the end of `mycallback` (or it will only run once)... I would only close it when I am sure I won't need it anymore. – Hoki Nov 05 '14 at 17:24

0 Answers0