Thread Subject: Serial Port Communication

Subject: Serial Port Communication

From: Gisnara

Date: 15 Jun, 2009 09:11:01

Message: 1 of 5

Hi,
I have a problem with serial communication via RS232 port.
I am working with Xilinx's software (SDK) and Matlab.
I am programming a Microblaze in language C and I need to send and receive data through serial port.
I need to send Matlab and to receive to FPGA in 4.350 times. I created a loop to send and receive data, but it's only working 50 times.

Can you help me?
Can you look at my code?

====================================
In Matlab, I have (9600 bps):
====================================
s1 = serial('COM1');
set (s1, 'timeout', 60);
set (s1,''terminator',0);
set(s1,'inputbuffersize',1024);
fopen(s1);
% I have a ciclo the calculate the inputs data of the microprocessor

for i=3:N1-2 % it's 4.350
    finputs = []; % for the first time the inputs are [0,0,0,0]
    finputs = [y(i-1);y(i-2);u(i-1);u(i-2)];

    ynew = nnoutput(NetDeff,W1f,W2f,finputs);% ynew = 1.9965e-004 here is a floating point
    ynew2 = num2str (ynew); % here I convert floating point to string, because in the microprocessor only receive string
    y(i)= ynew;

    fprintf(s1,ynew2); %here I send the string to microprocessor

    % I must receive the data of the microprocessor. The microprocessor sends to Matlab a string

    unew = fscanf(s1,'%f'); %here I convert the string to floating point
    u(i)= unew;

end

fclose (s1)

plot (u);
plot(y);

delete(s1)
clear s1

====================================
In SDK, I have:
====================================
      
    //LOOP TO SEND AND TO RECEIVE
    for(ind = 0; ind < size_ind; ind++)
    {
    
        ReceivedCount = 0;
        ReceivedCount = XUartLite_Recv(&UartLite, RecvBuffer, TEST_BUFFER_SIZE); //I'm receiving here
        double entr2 = atof(RecvBuffer);
    
        ...
  
        result = neur->activ;
    
        char data[15];
        sprintf(data,"%f",result);
              
        print(data);
        SentCount = XUartLite_Send(&UartLite, SendBuffer,TEST_BUFFER_SIZE); //I'm sending here
                                
                    
        }
        
=====================================
 
Thanks

Subject: Serial Port Communication

From: Sprinceana

Date: 15 Jun, 2009 09:45:02

Message: 2 of 5

"Gisnara " <brasergis@hotmail.com> wrote in message <h15375$cc7$1@fred.mathworks.com>...
> Hi,
> I have a problem with serial communication via RS232 port.
> I am working with Xilinx's software (SDK) and Matlab.
> I am programming a Microblaze in language C and I need to send and receive data through serial port.
> I need to send Matlab and to receive to FPGA in 4.350 times. I created a loop to send and receive data, but it's only working 50 times.
>
> Can you help me?
> Can you look at my code?
>
> ====================================
> In Matlab, I have (9600 bps):
> ====================================
> s1 = serial('COM1');
> set (s1, 'timeout', 60);
> set (s1,''terminator',0);
> set(s1,'inputbuffersize',1024);
> fopen(s1);

Can you list your error messages from the 2 files to anyone have better chances to help you?

From your 2 files.

Thanks for understanding!


> % I have a ciclo the calculate the inputs data of the microprocessor
>
> for i=3:N1-2 % it's 4.350
> finputs = []; % for the first time the inputs are [0,0,0,0]
> finputs = [y(i-1);y(i-2);u(i-1);u(i-2)];
>
> ynew = nnoutput(NetDeff,W1f,W2f,finputs);% ynew = 1.9965e-004 here is a floating point
> ynew2 = num2str (ynew); % here I convert floating point to string, because in the microprocessor only receive string
> y(i)= ynew;
>
> fprintf(s1,ynew2); %here I send the string to microprocessor
>
> % I must receive the data of the microprocessor. The microprocessor sends to Matlab a string
>
> unew = fscanf(s1,'%f'); %here I convert the string to floating point
> u(i)= unew;
>
> end
>
> fclose (s1)
>
> plot (u);
> plot(y);
>
> delete(s1)
> clear s1
>
> ====================================
> In SDK, I have:
> ====================================
>
> //LOOP TO SEND AND TO RECEIVE
> for(ind = 0; ind < size_ind; ind++)
> {
>
> ReceivedCount = 0;
> ReceivedCount = XUartLite_Recv(&UartLite, RecvBuffer, TEST_BUFFER_SIZE); //I'm receiving here
> double entr2 = atof(RecvBuffer);
>
> ...
>
> result = neur->activ;
>
> char data[15];
> sprintf(data,"%f",result);
>
> print(data);
> SentCount = XUartLite_Send(&UartLite, SendBuffer,TEST_BUFFER_SIZE); //I'm sending here
>
>
> }
>
> =====================================
>
> Thanks

Subject: Serial Port Communication

From: Sprinceana

Date: 15 Jun, 2009 09:54:01

Message: 3 of 5

I posted by mistake sry:(. I'm a little tired.

Can you show us your message of erros from the 2 files so that anyone who worked before has better chances to help you!

Thanks for your understanding!

Subject: Serial Port Communication

From: Gisnara

Date: 15 Jun, 2009 10:15:02

Message: 4 of 5

ok.
Sending and receiving data occurs 50 times without problems, after it indicates that:
" ??? In an assignment A(I) = B, the number of elements in B and I must be the same
Error in ==> u(i) = unew; "
and not complete the cycle 4350 times.
I can't get points for building the plots u and y.

Thanks.

Subject: Serial Port Communication

From: Ernesto del Toro

Date: 20 Nov, 2009 15:35:27

Message: 5 of 5

Error in ==> u(i) = unew;
You start "i = 3" What about the u(1),u(2) elements?
You should also try:
u=[];
For...
    ....
    u = [u;element_received];
end
regards

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
serial port com... Sprinceana 15 Jun, 2009 05:54:06
serial communic... Sprinceana 15 Jun, 2009 05:49:09
rssFeed for this Thread

Contact us at files@mathworks.com