Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Serial communication with matlab problem in writing

Status
Not open for further replies.

bouvett

Newbie level 2
Joined
Aug 23, 2008
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,315
Hi guys,
I have already read many of the links available on this forum and on the web in general, but unfortunately i still haven't solved the problem in my code - I'm sure a geek out there will solve this in minutes but i'm still a newbie unfortunately :sad: .

i am trying to read data from a serial port, process the data and then transferring it back serially. Now the read is surely right, but unfortunately when i go to write a problem arises. I am in actual fact calibrating a touch screen to a glcd.. now the transmission of data between the embedded board i am using and the matlab is wrong when i write back to the serial port but good when i just read.

The code is:
Code:
function touch_screen_calibration ()
%This function will calibrate the touch screen for the natural input
%calculator. What it does is to accept input serially from the glcd which
%will be of the form x,y and then provide a calibrated output x_cal,y_cal
%again serially to the fpga.

%calibration constants
x_d0 =12;
x_d1 =114;
x_d2 =63;

y_d0 =6;
y_d1 =30;
y_d2 =57;

% Opens a serial communication object
SPort=serial('COM7','BaudRate', 9600,'Parity', 'none','DataBits',8,'StopBits', 1);
set(SPort,'InputBufferSize',20480000);
set(SPort,'Timeout',0.00000001);
fopen(SPort)

coordinates = zeros(1,5000);
counter =1;

% asynchronous mode
SPort.ReadAsyncMode = 'continuous';
readasync(SPort);

%Setup calibration variables
cal_counter = 6; %used to keep track of which calibration point has been received
                 %and whether calibration is ready.               
A=0.0;
B=0.0;
C=0.0;
D=0.0;
E=0.0;
F=0.0;
K=0;

    while(1)
        IN_DATA=fread(SPort,1,'uchar');
    
        if(IN_DATA ~= -1)  
            IN_DATA
            
            switch (cal_counter)
                case 0
                    x_0 = IN_DATA;
                    cal_counter = cal_counter +1;
                case 1
                    y_0 = IN_DATA;
                    cal_counter = cal_counter +1;
                case 2
                    x_1 = IN_DATA;
                    cal_counter = cal_counter +1;
                case 3
                    y_1 = IN_DATA; 
                    cal_counter = cal_counter +1;
                case 4
                    x_2 = IN_DATA;
                    cal_counter = cal_counter +1;
                case 5
                    y_2 = IN_DATA;
                    cal_counter = cal_counter +1;
                    
                    K = (x_0 -x_2)*(y_1 - y_2) - (x_1 - x_2)*(y_0 - y_2);
                    
                    A = ((x_d0 - x_d2)*(y_1 - y_2) -(x_d1 - x_d2)*(y_0 - y_2))/K;
                    B = ((x_0 - x_2)*(x_d1 - x_d2) -(x_d0 - x_d2)*(x_1 - x_2))/K;
                    C = (y_0*(x_2*x_d1 - x_1*x_d2) + y_1*(x_0*x_d2 - x_2*x_d0) + y_2*(x_1*x_d0 - x_0*x_d1))/K;
                    
                    D = ((y_d0 - y_d2)*(y_1 - y_2) -(y_d1 - y_d2)*(y_0 - y_2))/K;
                    E = ((x_0 - x_2)*(y_d1 - y_d2) -(y_d0 - y_d2)*(x_1 - x_2))/K;
                    F = (y_0*(x_2*y_d1 - x_1*y_d2) + y_1*(x_0*y_d2 - x_2*y_d0) + y_2*(x_1*y_d0 - x_0*y_d1))/K;
                    
                case 6 %calibration ready therefore start reading X and Y co-cordinates
                    X = IN_DATA;
                    cal_counter = cal_counter +1;
                case 7
                    Y = IN_DATA;
           
                    if( (X<128  && X>=0) && (Y<64  && Y>=0))
                        %Process data and send it serially
                        %x_cal = A*X + B*Y + C;
                        %y_cal = D*X + E*Y + F;
                        
                        x_cal = X;
                        y_cal = Y;

                        coordinates(1,counter) = X;
                        coordinates(1,counter+1) = Y;

                        counter = counter +2;

                        %slow down transmission for send
                        stopasync(SPort);
                        set(SPort,'Timeout',1);
                        SPort.ReadAsyncMode = 'continuous';
                        %readasync(SPort);

                        x_cal = round(x_cal);
                        fwrite(SPort,x_cal);

                        y_cal = round(y_cal);
                        fwrite(SPort,y_cal);

                        stopasync(SPort);
                        set(SPort,'Timeout',0.00000001);
                        SPort.ReadAsyncMode = 'continuous';
                        %readasync(SPort);

                        cal_counter = cal_counter -1;
                        save  coordinates coordinates
                    end
                    
                    
                otherwise cal_counter = cal_counter; %dummy
                    
            end
        end
    end
end

i suspect that the problem is that i am all the time changing the timeout period. The reason for doing this is because i want to read the port continously but if i keep the same time out period for writing an error is returned.


Thanks for your time guys.

regards,
bouvett

---------- Post added at 21:20 ---------- Previous post was at 21:09 ----------

if i just write to the serial port without changing the timeout i get an error:


??? Error using ==> serial.fwrite at 199
A timeout or Ctrl-C occurred during the write operation.

Error in ==> touch_screen_calibration at 102
fwrite(SPort,x_cal);

I hope this helps as a clue.
thanks guys

---------- Post added at 21:30 ---------- Previous post was at 21:20 ----------

I have confirmed that the problem is whit how i am handling the timeout period. when i set the time out period to 0.01, i would not need to change it before writing to the serial port but unfortunately this is quite slow for my application.. so what i really need is a proper handling of the changing of the timeout period.


thanks again

---------- Post added at 22:37 ---------- Previous post was at 21:30 ----------

The error is very strange since it can read very fast but it cannot write that fast not even 5 times slower... this is constantly giving me a timeout error when trying to write to the serial port
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top