Shop OBEX P1 Docs P2 Docs Learn Events
Whats wrong with my loop using the FullDuplexSerial and 6 other cogs — Parallax Forums

Whats wrong with my loop using the FullDuplexSerial and 6 other cogs

grasshoppergrasshopper Posts: 438
edited 2008-06-25 02:08 in Propeller 1
I am using several cogs so far and this is my last bug. I am tring to run a display stand alone with main code, but yet keep the communications open on the cog that fullserialduplex is running on; instead of a infinite loop in my main code (with display and other code) with if statements that can break the loop.

here is the main code
Pub  main

  
  ser.start(31,30,0,baud)
  lcd.init
  
  REPEAT

     
      Display(0)
            
      if (ser.lib == 1)
          ser.str(string("ok"))
         
      if (ser.lib == 2)
        ser.str(string("ok2"))






but then i had to modify the FullSerialDuplex object because it runs on a stand alone cog so i figured i could streamline things by adding this little method to the object.


  
pub lib : Rxstring  | RxDat [noparse][[/noparse] 18 ] , stringCount

  Rxstring~~
  StringCount~
  
  repeat until (RxDat[noparse][[/noparse]StringCount] := rx ) == ")"
    StringCount ++

  IF (rxDat [noparse][[/noparse] 0 ] == "(")&(rxDat [noparse][[/noparse] 1 ] == "F")&(rxDat [noparse][[/noparse] 2 ] =="1")* (rxDat [noparse][[/noparse] 3 ] == $20)

          IF (rxDat [noparse][[/noparse] 4 ] == "I")&(rxDat [noparse][[/noparse] 5 ] == "D")&(rxDat [noparse][[/noparse] 6 ] ==$20)&(rxDat [noparse][[/noparse] 7 ] == "?")
                                rxString := 1 
                               
                                 
          IF (rxDat [noparse][[/noparse] 4 ]  == "V")&(rxDat [noparse][[/noparse] 5 ] == "N")&(rxDat [noparse][[/noparse]  6 ] ==$20)&(rxDat [noparse][[/noparse] 7 ] == "?")   
                                rxString := 2





i also tryed doing a


if (ser.rxcheck > -1)
  < run commands here >





That did not work either.
Let me know if there is a better way going about this. or if you see something that can help me.

Post Edited (grasshopper) : 6/24/2008 11:21:30 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-24 23:28
    You need to do a better job of explaining what you're trying to do and how you're trying to do it. I don't understand what's wrong, when it does or doesn't happen, and what behavior you're seeing. Sometimes you can answer your own question by restating it more clearly.
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-24 23:56
    Well Mike
    I am running a "main" program that in turns monitors 7 other cogs and in the "main" program i had this statement


    
      StringCount~
      
      repeat until (RxDat[noparse][[/noparse]StringCount] := ser.rx ) == ")"
        StringCount ++
    
      IF (rxDat [noparse][[/noparse] 0 ] == "(")&(rxDat [noparse][[/noparse] 1 ] == "F")&(rxDat [noparse][[/noparse] 2 ] =="1")* (rxDat [noparse][[/noparse] 3 ] == $20)
    
              IF (rxDat [noparse][[/noparse] 4 ] == "I")&(rxDat [noparse][[/noparse] 5 ] == "D")&(rxDat [noparse][[/noparse] 6 ] ==$20)&(rxDat [noparse][[/noparse] 7 ] == "?")
                                    rxString := 1 
                                   
                                     
              IF (rxDat [noparse][[/noparse] 4 ]  == "V")&(rxDat [noparse][[/noparse] 5 ] == "N")&(rxDat [noparse][[/noparse]  6 ] ==$20)&(rxDat [noparse][[/noparse] 7 ] == "?")   
                                    rxString := 2
    
    
    


    rxdat [noparse][[/noparse] 20 ] is my array
    stringcount increments the array so the characters can be separated later for a command structure library.
    the if statements do the logic "filtering" and RxString is my output command for "main" code to execute using more logic.

    basically puts the ser.RX into an array where i can decipher it as various commands. The problem is that my "main" code will run until it gets to this line

      repeat until (RxDat[noparse][[/noparse]StringCount] := rx ) == ")"
    
    



    Then it stops and waits. Well i want other things in my main code to keep running. For example: to up date my display. Also what if i get no command through the serial port. I don't want the code to sit her for ever unless I get a command. Ther must be a different way to use the FullDuplexSerial object and still filter out strings of commands. Some of this will include the example you helped me out with - numbers to floating point.

    So I have tried to modify the FullDuplexSerial object by putting the above code (my first post) in the object. My logic was that since the cog was running may as well put this type of string filter in it. But i am still have the wait problem in my main code.

    hope this is a better explanation

    Post Edited (grasshopper) : 6/25/2008 12:06:56 AM GMT
  • TimmooreTimmoore Posts: 1,031
    edited 2008-06-25 00:27
    lets assume you have a start and end char for your commands, then have a variable that has 2 states - waitingforstart. incommand
    repeat
    if variable == waitforstart
    if ser.rxcheck == startchar
    variable := incommand
    startcount := 0
    buffer[noparse][[/noparse]startcount++] := startchar
    else
    if char := ser.rxcheck > -1
    buffer[noparse][[/noparse]startcount++] := char
    if char == endchar
    processcommand in buffer
    variable := waitingforstart
    do other stuff
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-25 00:49
    You still want this code in the main program, not in FullDuplexSerial. You want to use rxcheck rather than rx so it doesn't hang waiting for something. FullDuplexSerial is buffered (16 characters) so it'll keep receiving bytes and holding them until your program can get to them. If rxcheck doesn't have a character, it'll return -1. You can check for that and do something else if you want.

    It's possible to have FullDuplexSerial do the format checking (like for the ")"), but you'd have to do it in assembly since that's what's running in the cog that FullDuplexSerial starts. The Spin code in FullDuplexSerial runs in the same cog as the part of your program that calls it (probably the main cog).
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-25 01:00
    Thanks, to both of you. I'll implement the concepts in the main code. But I ask a favor...

    ...stay tuned for more questions on this topic. I have actual done some of this type of checking and still it did not work correctly, however you both have cause a light bulb to shine brighter and thus i shall try yet again.
  • TimmooreTimmoore Posts: 1,031
    edited 2008-06-25 01:09
    The other thing you should do is loop around the rxcheck until it returns -1 then the do other stuff. If your other stuff takes long enough for 2 characters to get into the fullduplexserial rx buffer, you need the loop to get htem out otherwise you may overflow the rx buffer. If you want an example of what you are trying to do. search for a thread on multiple serial port driver and look in the example app. It has a cog running a loop calling processing routines on a gps and a camera working in the way I just explained.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-06-25 01:13
    Another thing you can do is to expand the receive buffer. They can be any reasonable power of 2 in size. BoeBotBasic has a modified FullDuplexSerial with a larger buffer and you can use that as a model.
  • grasshoppergrasshopper Posts: 438
    edited 2008-06-25 02:08
    OK thanks fellas i got it working and now I owe some of you a beer or two.

    Special thanks to Mike Green - You have thought me more in the last few days than any book or person.

    I have accomplished a PID controlled 4 channel temperature control station +/- .04 degrees, with 3 independent motors that have variable speeds, and 2 com ports 1 serial and 1 usb along with EEPROM read/write abilities and a 2x20 lcd display.

    Phew thanks...

    My confidence as a new electronics designer is growing.
Sign In or Register to comment.