Go Down

Topic: Serial parsing with Serial Proxy (Read 2546 times) previous topic - next topic

MobileWill

#15
Jul 15, 2011, 05:17 am Last Edit: Jul 15, 2011, 05:40 am by MobileWill Reason: 1
Darn it! I just realized even though it seemed to sort of work the main loop isn't running, the LCD isn't updating until I click a command so back to the drawing board!!!  =(

Edit:
I tired this program is this post and the same result. So weird!!

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1218590225

SerialServer
http://itp.nyu.edu/~dbo3/SerialServer/SerialServer.html

At least now I can see the data in the debug window.
Current Projects:                    Arduinos:
Security Robot Tank               Uno
Security Robot II 4WD            Mega2560

http://mobilewill.blogspot.com

PaulS

Quote
I think it is hanging up here

Nope. Incoming serial data triggers an interrupt. During that interrupt, the incoming character is added to a buffer. All that Serial.available() is doing is looking to see if the head and tail of the ring buffer used are at the same place (no data available) or at different places (some data available - the amount being the difference in the head and tail values).

MobileWill

#17
Jul 15, 2011, 08:53 pm Last Edit: Jul 15, 2011, 08:56 pm by MobileWill Reason: 1
Thanks. that helps.

For now I did this.

Code: [Select]
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(vbCrLf + cmdCamera + vbCrLf + vbCrLf)


I added another Carriage Return at the end of the command sequence.

It came to me in my sleep. Good thing cause I didn't want to think about it all weekend since I won't be able to work on it.

Now in the mean time to speed up my code. I am thinking of moving this function into the loop function. Would that make a difference?
Current Projects:                    Arduinos:
Security Robot Tank               Uno
Security Robot II 4WD            Mega2560

http://mobilewill.blogspot.com

jraskell


Thanks. that helps.

For now I did this.

Code: [Select]
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(vbCrLf + cmdCamera + vbCrLf + vbCrLf)


I added another Carriage Return at the end of the command sequence.

It came to me in my sleep. Good thing cause I didn't want to think about it all weekend since I won't be able to work on it.

Now in the mean time to speed up my code. I am thinking of moving this function into the loop function. Would that make a difference?


Actually, I'd have to recommend the opposite.  Your code is a bit too disorganized and redundant.  You have a getMessage function that does a lot more than just get a Message.  It gets it, and conditionally processes based on the first character.  Actually, it doesn't even get the whole message first. You also have two separate blocks of code that both are reading in the serial data, and then processing it.  That's just a bad design and prone to errors.

You really should refactor your code.  First, modify getMessage so it does just one single thing.  Get an entire message.  Don't look at what's in the message.  Just get the entire string (without code duplication), ideally with both a start and end character, but still workable with just an end character. 

After you have your entire message, then you can parse and process it.  Check the first character, if it's your 'command' character, send the string to a separate command process function.  If it isn't, send it to your speech processing function.

The overhead involved with function calls is effectively non-existent when dealing with serial data.  You aren't going to see any sort of performance gain by eliminating a few functions, but you will significantly impact code readability and the likelihood of introducing more bugs into your code.

MobileWill

Thanks for all the help. Here is my rewrite, that works well, very well. Now I can string commands together.

Code: [Select]
boolean getMessage2()
{
 char in_char = '\0';    //Create a character to store the incoming byte from the serial port.
 inCount = 0;

 if (Serial.available() > 0) {
   in_char=Serial.read();
  if (in_char=='#'){
   while(in_char != '!'){
     while(Serial.available() <=0);
      in_char = Serial.read();  
      inString[inCount]=in_char;
     inCount++;
     //Serial.println(in_char);
   }
       
   inString[inCount+1]='\0';    //Strings must end with a Null terminator so we need to add this to our message.
 
   return true;
  }
 }
 
return false;
}

void parseCmd()
{
   char cmd[5]="";
   char cmdChar;
   int cmdValue=0;
   int code=0;
 
     if (inString[0] == '/'){
       
       for (int x = 0; x < inCount; x++)
       {
          cmd[x] = inString[x+1];
       
       }
       
       cmdChar = cmd[0];
       
       for (int x = 0; x < inCount-1; x++)
       {
          cmd[x] = cmd[x+1];
       }
       
       cmdValue = atoi(cmd);
       
       pCmd(cmdChar, cmdValue);

     }
     
     else {
     
     int x=0;
     for (int i = 0; i < inCount-1; i++){
       
      speakCmd[x]=inString[i];
       
     if (inString[i] == 0x5C){
     
     cmd[0]=inString[i+1];
     cmd[1]=inString[i+2];
     cmd[2]=inString[i+3];
     
     code = atoi(cmd);
     
     speakCmd[x]=code;

     i = i + 3;
     }
     x++;

     }
               inString[inCount-1]='\0';
               speakCmd[inCount-1]=0x0D;
               speakCmd[inCount]='\0';
               speak();
   
     }
}


Just need to comment the code.
Current Projects:                    Arduinos:
Security Robot Tank               Uno
Security Robot II 4WD            Mega2560

http://mobilewill.blogspot.com

Go Up