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.

Get string with Unknown length from serial Port

Status
Not open for further replies.

mo2_star

Newbie level 5
Joined
Feb 1, 2013
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Finland, Porvoo
Activity points
1,340
hello My friends,

I have A problem in my Project

I have A String
I have to receive it From Serial Port And Use It

First Problem is How Receive it? When it is between 0 to 9 it has 9 Characters,And from 10 to 99 it has 10 Characters,And from 100 to 255 it has 11 Characters

This Strring Starts With * And Ends With #
Example:
*11|11|1#
*11|11|9#
*11|11|54#
*11|11|87#
*11|11|110#
*11|11|180#
*11|11|254#
This Project Is With C Language and I use AVR Codevison.


Problem # 2
How Use The numeric variable From the String After Receiving?
sample : 1,9,54,87,110,180,254
I want To use It for OCR1A Register For PWM.
 

You could store all bytes received into a temporary buffer, so that just after detecting of the end-of-stream character '#' you would perform the weighting of each one by a 10th scale factor; something like that ( may contain either syntax or logic errors ):


Code C - [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
bool StoreValue = FALSE ;
if ( ByteReceivedFromUart == '#' )
i=0;
switch ( ByteReceivedFromUart )
{
case : '*' ; i++ ; break ;
case : '1' ; i++ ; break ;
case : '1' ; i++ ; break ;
case : '|' ; i++ ; break ;
case : '1' ; i++ ; break ;
case : '1' ; i++ ; break ;
case : '|' ; i++ ; break ;
default : i=0  StoreValue = TRUE  ; break ;
}
// Store values according sequence of reception 
if ( StoreValue  )
     i=0;
     While ( i =! '#' )   // while not reached end of reception, performs assignent bellow
        Temp[i++] = ByteReceivedFromUart ;  // load each byte received form UART
 
     // Calculate weighted values on inverse order of reception
     j=i ;
     While ( j > 0 )
         Value=(10^i ) * Temp[j-i] ; // note that the last value received is the most significant



Note:
It is assumed that you alredy performed ASCII conversion to decimal
 
Code:
while (1)
      {     
            
            z[11]=0x00;
            i=0;
            delay_ms(100);
            while(z[i]!=0x23){   
            i++;
            z[i]=getchar();
            }   
            
            switch (z[8])
            {    
            case '0':z1=0 ;break;
            case '1':z1=1 ;break;
            case '2':z1=2 ;break;
            case '3':z1=3 ;break;
            case '4':z1=4 ;break;
            case '5':z1=5 ;break;
            case '6':z1=6 ;break;
            case '7':z1=7 ;break;
            case '8':z1=8 ;break;
            case '9':z1=9 ;break;
            case 0x23:z1=0 ;break;  
            default :z1=0 ;break;
            } 
            switch (z[9])
            {    
            case '0':z2=0 ;break;
            case '1':z2=1 ;break;
            case '2':z2=2 ;break;
            case '3':z2=3 ;break;
            case '4':z2=4 ;break;
            case '5':z2=5 ;break;
            case '6':z2=6 ;break;
            case '7':z2=7 ;break;
            case '8':z2=8 ;break;
            case '9':z2=9 ;break; 
            case 0x23:z2=0 ;break;  
            default :z2=0 ;break;
            }
            switch (z[10])
            {    
            case '0':z3=0 ;break;
            case '1':z3=1 ;break;
            case '2':z3=2 ;break;
            case '3':z3=3 ;break;
            case '4':z3=4 ;break;
            case '5':z3=5 ;break;
            case '6':z3=6 ;break;
            case '7':z3=7 ;break;
            case '8':z3=8 ;break;
            case '9':z3=9 ;break;   
            case 0x23:z3=0 ;break;  
            default :z3=0 ;break;
            }  
           
            lcd_clear();
            lcd_gotoxy(0,0);
            lcd_puts("ok:"); 
            lcd_gotoxy(1,1);
            lcd_putchar(z[8]); 
            lcd_putchar(z[9]);
            lcd_putchar(z[10]); 
            lcd_puts("  ");
            z1=z1*100;
            z2=z2*10;
            z3=z3*1;
            sum=z2+z1;
            sum=sum+z3;
            lcd_puts("  "); 
            sprintf(str,"%d",sum);
            lcd_puts(str);
            OCR1A=sum;
      } 
}
I do it.
Thanks :)
Know I have A new Problem
i don't know why happened

When My numeric variable is higher than 99 (100,124,...,255)
It is Ok.
But When numeric variable is lower than 99
it turns to 34#,53#
but my output should be 34,53,...
but it is 340,530,...
In my case I Switch "#" to 0 but it happened.
:(
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top