Thread: Waiting for a specific character through a port

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    Wink Waiting for a specific character through a port

    i´ve written a program which collects and sorts data coming through a port.

    the problem is that the data coming through is started by a # sign and i want my program to start reading when it encounters this sign or wait if it doesnt.

    i know i can use "waitforsingleobject" but the problem is that i dont have a "handle" defined since i´m using a library called PComm.lib to make life easier. i have managed to tell the program to stop reading when it reaches a terminating character but cant get it to wait until it reaches the start character.

    any help appreciated.

    im using VC6 with windows XP

    thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Post your code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by dwks
    Post your code.
    this is what i have so far.......

    Code:
    Sleep(1250);
    
                memset(streaminput, 0, sizeof(streaminput));
                memset(aux, 0, sizeof(aux));
    
                while (sio_getch(port) != 35) { }
    
                while (aux[0] != 35)
                    {
                    aux[0] = sio_getch(port);
                    aux[1] = 0;
    
                    strcat(streaminput, aux);
    
                    if (aux[0] == 10 || aux[0] == 13)
                        break;
                    }
    
                /*CLose the Port that we opened earlier*/
    			}
                sio_close(port);

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So do you want something like
    Code:
    while(input != '#') ;
    ?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by dwks
    So do you want something like
    Code:
    while(input != '#') ;
    ?
    i have tried that but the thing just doesnt work.

    the program starts up and closes itself after 3 or 4 seconds.

    ok guys, the string starts with a # and finishes with a "newline character"

    so i want to start reading when i encounter the # character and stop when i encounter the newline. So far, the program starts reading at the # and stops reading at the newline HOWEVER, for some reason it doesnt "wait" until the newline character is received before reading.

    thus i have to introduce a "sleep". if i set the sleep to 1ms then it shouldnt affect the program as it should start reading from the #. however, the program reads a few lines and then stops....

    this is my code so far


    Code:
         
    
    //     while(sio_getch(port) != '#'){}
    //commented out because the program starts and stops immediately if this line is left in
    			
    Sleep(1250);
    
                memset(streaminput, 0, sizeof(streaminput));
                memset(aux, 0, sizeof(aux));
    
                while (sio_getch(port) != 35) { }
    
                while (aux[0] != 35)
                    {
                    aux[0] = sio_getch(port);
    
                    strcat(streaminput, aux);
    
                    if (aux[0] == 10 || aux[0] == 13)
                        break;
                    }
    Last edited by shoobsie; 08-25-2005 at 02:47 AM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
                while (sio_getch(port) != 35) { }
    
                while (aux[0] != 35)
                    {
                    aux[0] = sio_getch(port);
    
                    strcat(streaminput, aux);
    
                    if (aux[0] == 10 || aux[0] == 13)
                        break;
                    }
    In order to end the first while loop, the char must be 35. And the next loop starts only if the char isn't 35. Therefore, your second while loop never executes. (Maybe change it to while(1)?)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Reading a file with Courier New characters
    By Noam in forum C Programming
    Replies: 3
    Last Post: 07-07-2006, 09:29 AM
  3. is there a way of checking what program uses a specific port?
    By Xterria in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-27-2004, 10:54 PM
  4. Basic port scanner code .. pls help ???
    By intruder in forum C Programming
    Replies: 18
    Last Post: 03-13-2003, 08:47 AM
  5. Specific Character Text Color
    By TechWins in forum C++ Programming
    Replies: 5
    Last Post: 04-22-2002, 03:42 AM