1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Serial comm between uC and PC...

Discussion in 'Microcontrollers' started by koolguy, Apr 26, 2013.

  1. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    You aren't using this code on PC to PC transfers are you??? That code was for Hitech on a pic16... PC to PC needs different coding...
     
  2. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    OK, this code was for comparing two string not for PC to PC....the problem in this code is that while scanning/reading the string it depend on size i mean on loop/array indexing i want to make it as per user size like pressing enter will terminate string like this..
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Our biggest problem here is... You are struggling to program one device!!! Programming two.... To talk together... You really need to get one half working as you need it first....
    Going off you "Window" thread, you can't get windows to send a string to the outside world... With VB6 you just chuck the MScomm control on a form and write

    Code (text):

    MSComm1.output " Hello outside world " & CRLF
     
    It extremely simple...

    You can chuck a button on the form and put the line in the button handler and keep repeating...
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9

    I would like to stop you for talking about PC to PC transfer...
    I am thinking that i should first clear few doubt then move to data transfer.
    I am trying to compare two string one is stored in uC memory and 2nd string is readied from PC to UART ten uC here i am not talking about PC to PC only one PC to uC...
    for example as we do for making password, the problem i am repeating again is how to store string of variable size if enter is press then it should end after that comparing of that two string is to be done...
    i hope now you got my point.
     
  6. Brian Griffin

    Brian Griffin Member

    Joined:
    May 23, 2013
    Messages:
    49
    Likes:
    8
    Location:
    Kuala Lumpur
    Comparing strings and all the other stuff should be learned, from a C language book, instantly. Do you have a semester in programming?
     
  7. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Please have a look on previous post i have done that asking for something else..
    i want to make read char independent of array size i.e. real time or on pressing enter it should end reading/storing string from terminal window.
    Code (text):
    unsigned char b[10];
    unsigned char *c="Hello";
     
    for(char x=0;x<6;x++){
    b[x]=HSerin();
     
    }
     
    int result = strncmp(c,b, compareLimit);
     
    if(result > 0){
    ch='a';
    HSerout(ch);
    }
     
    else if(result < 0)
            {
    ch='b';
    HSerout(ch);
            }
    else{
    ch='c';
    HSerout(ch);
    }
     
  8. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,

    i have corrected the code working cool, this code will match the password in terminal window....after pressing ESC the string will terminate!

    Code (text):
    #define ESC 27
    unsigned char *a=" Hello! Ask me a question!\n ";
    unsigned char *w=" WRONG PASSWORD ";
    unsigned char *m=" MATCHED PASSWORD ";
    unsigned char b[10];
    unsigned char *c="Hello";
    unsigned char  HSerin(void);
     void HSerout(unsigned char ch),
     HSerinit(void);

     void main(void)                        // program entry  
       {  
    int compareLimit = 100;  
       TRISB=0x00;
       unsigned char ch ;         // <- LOOK HERE.
       ADCON1 = 0x6;                    // Analogue off    
       HSerinit();      
      __delay_ms(1);

    while(*a!=0)
    {
        ch=*a++;  
        HSerout(ch);  
         __delay_ms(150);      
      }
    while(1)                        // endless Loop      
      {  

    for(char x=0;x<9;x++){
    b[x]=HSerin();
    if(b[x]== ESC){
    b[x]=0;
    break;
    }

    }

    int result = strncmp(c,b, compareLimit);

    if(result > 0 || result < 0){
    while(*w!=0)
    {
        ch=*w++;  
        HSerout(ch);  
         __delay_ms(150);      
      }
    }

    else if(result==0)
            {
    while(*m!=0)
    {
        ch=*m++;  
        HSerout(ch);  
         __delay_ms(150);      
      }
            }



    }
        }
     
  9. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Strings should be null-terminated.. simple as that. The array should be always long enough to hold any string ever imaginable in the particular application. You just compare up to the null character when you do string-to-string comparison.
    http://en.wikipedia.org/wiki/Null-terminated_string
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Ritesh!!! A string compare is as simple as a number compare

    Take "Password"

    P = 80
    a = 97
    s = 115
    s = 115
    w = 119
    o = 111
    r = 114
    d = 100

    You can either check each number one by one
    Or for a simple ( but not always correct ), solution. Add them all up.

    The problem with the last solution is they can be out of order and still work..

    The best way is to use strcmp(); in the string library

     

Share This Page