Hserin parsing string...again?


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Location
    Salt Lake City, Ut, USA
    Posts
    108

    Question Hserin parsing string...again?

    Hello all,

    This has probably been answered in a million different threads...all of which I have read, which leads me to my question.
    What I'de like to do is receive a string into the usart similar to this:

    ABC123

    What I would like to do is store the ABC in a word var to send through a
    select case statement and...
    store the 123 in a byte var to use later in code.

    The commands coming into the usart will always be this format, for ex:

    ABC123
    XYZ255
    PQR002

    LETTER-LETTER-LETTER-NUMBER-NUMBER-NUMBER

    NUMBER-NUMBER-NUMBER not to exceed 256.

    Any help parsing these from the RCREG or a 6 byte ring buffer?

    Thanks to all,
    Chris

  2. #2
    Join Date
    Dec 2005
    Location
    Salt Lake City, Ut, USA
    Posts
    108

    Default Follow up?

    Am I on the right track?

    Code:
    Ser_Cmd_Word var word
    Ser_Cmd_Val var byte
    
    Start:
        ...	
        If PIR1.5 = 1 Then Goto New_SerIn    
       ...
    GOTO Start
    
    New_SerIn:
        PIE1 = %00000000                                  'Disable interrupts
            hserin [dec3 Ser_Cmd_Word, dec3 Ser_Cmd_Val]
            LCDout $fe, 1, "COMMAND RECEIVED"
            Lcdout $fe, $c0, "-----", Ser_Cmd_Word, Ser_Cmd_Val, "-----" 
            pause 3000                 'Pause for 3 seconds for user viewing
        PIE1 = %01110000                                  'Enable interrupts
    Return
    Thanks much

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    3 letters won't fit in a word variable very easily.
    Although it can be done, with limitations.

    The second part ...
    dec3 Ser_Cmd_Val
    should work fine.

    Is there a limit to the 3 letter sequences, or could it be anything?

    Do you have PBP 2.60?

    DT

  4. #4
    Join Date
    Dec 2005
    Location
    Salt Lake City, Ut, USA
    Posts
    108

    Default Thanks Darrel

    If I understand your question correctly...yes, 3 characters- no more - no less. I did discover the 16 bit word size after my post...newbie move, i admit . How about me changing the command to:

    LETTER LETTER NUMBER NUMBER NUMBER

    Easier coding that way? The ultimate goal is to run the LETTER LETTER through
    a select case statement like:
    Code:
    Chk_Cmd:
        select case Ser_Cmd_word
            case TT    'Torch Target
                TTV_val = Ser_Cmd_Val
            Case XX    'Another Command
                'SomeByteVar = Ser_Cmd_Val
            Case else
                LCDout $fe, 1, " COMMAND NOT IN "
                Lcdout $fe, $c0, " DEFINED SCOPE! "
        END SELECT
    RETURN
    Am I on the right track?
    Thanks again.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Well, I was referring to how many 3-letter combinations, and if you have PBP 2.60 it could be done differently.
    However, let's go with what you have ...

    First, I'd recommend starting the command sequence with a known character, like "$". This can keep the data "synced" by making it easy to find the beginning of each "packet". Then use the WAIT("$") modifier in HSERIN.

    If you only have a few different commands, they can be reduced to 1 letter, which makes it work well with the Select Case statement. Then the packets might look like ...

    $T123
    $P255

    Also, don't enable interrupts.
    HSERIN does not use interrupts. And enabling them without handler routines being defined will cause the program to lock up.

    If you want to keep the 2-letter commands, that can be done too, but it's a little trickier.

    DT

  6. #6
    Join Date
    Dec 2005
    Location
    Salt Lake City, Ut, USA
    Posts
    108

    Default

    Will this fly...nearest you can tell?

    Code:
    'PIC 16F917
    
    '------------------------------------------------------------------------------------------
    
    'Clock setup
        Define OSC 20
    'Interrupt setups
        INTCON = %11000000      'Enable: Global interrupts, Peripheral interrupts
        PIE1 = %01110000        'Enable: ADC interrupt, Rx interrupt, Tx interrupt
    'Pin setups
        ANSEL = %10000000       'Analog input on E.2/AN7, all others digital
        TRISA = %11000000       'PortA: 0-5 = output, 6-7 = input
        TRISB = %11111111       'PortB: 0-7 = input (defaults)
        TRISC = %11011111       'PortC: 0-5&7 = input, 6 = output
        TRISD = %11111111       'PortD: 0-7 = input (defaults)
        TRISE = %00000100       'PortE: 0&1 = output, 2 = input, 3-7 = N/A
    'Vref setups
        VRCON = %10001111       'Enable Vref, set high range, set value (3.59 Vdc)
    'Usart setups
        SPBRG = %00011001       'Set 9600 baud w/20MHz clock
        TXSTA = %00100110       'Enable transmit, set BRGH high
        RCSTA = %10010000       'Enable Usart, enable receiver
    'ADC setups
        ADCON0 = %10011101      'Right justify (8 bit result in ADRESL), Select ch.7, enable ADC
        ADCON1 = %00100000      'Set ADC clock to 625kHz
    
    '------------------------------------------------------------------------------------------    
            
    'LCD setup (16 x 2 lines)
        Define LCD_BITS 4       '4 data interface lines
        Define LCD_DREG PORTA   'LCD data port
        Define LCD_DBIT 0       'LCD data bits 0-4
        Define LCD_RSREG PORTA  'LCD register select port
        Define LCD_RSBIT 4      'LCD register select bit
        Define LCD_EREG PORTA   'LCD enable port
        Define LCD_EBIT 5       'LCD enable bit
    
    '------------------------------------------------------------------------------------------
    Ser_Cmd_Numb var byte
    Ser_Cmd_Val var byte
    THC_Enable_In var PORTC.0
    TTV_Val var byte
    HYS_Val var byte
    Torch_V_Now var byte
    Torch_Up var PORTE.0
    Torch_Dn var PORTE.1
    TTV_Val = 120
    HYS_Val = 0
    
    pause 1000                  'Pause for 1 second for LCD warmup
    LCDout $fe, 1, "---LCD WARMUP---"
    Lcdout $fe, $40, "----FINISHED----"
    pause 5000                 'Pause for 5 seconds for user viewing
    
    Start:
        IF THC_Enable_In = 0 then
            PIE1.6 = 0        'Turn off ADC interrupt
            PIE1.5 = 1        'Turn on Rx interrupt
            LCDout $fe, 1, "READY TO RECEIVE"
            Lcdout $fe, $40, "COMMAND:C###V###"
            If PIR1.5 = 1 Then Goto New_SerIn
            PAUSE 500
            GOTO START
        ELSE
            PIE1.6 = 1        'Turn on ADC interrupt
            PIE1.5 = 0        'Turn off Rx interrupt
            ADCON0.1 = 1    'Start ADC conversion        
            if PIR1.6 = 1 THEN GOTO New_ADC
            LCDout $fe, 1, "TARGET VDC : ", dec TTV_Val
            Lcdout $fe, $40, "PLASMA VDC : ", dec Torch_V_Now
            select case Torch_V_Now
                case Torch_V_Now > TTV_Val + hys_val
                    high torch_dn
                    low torch_up
                case Torch_V_Now < TTV_Val - hys_val
                    high torch_up
                    low torch_dn
                case else
                    low torch_up
                    low torch_dn
            end select
        endif
    GOTO Start
    
    New_ADC:
        Torch_V_Now = ADRESL    
        PIR1.6 = 0
    Return
    
    New_SerIn:
    '******Command structure is: C###V###    
        PIE1 = %00000000                                                    'Disable interrupts
            hserin 100, start, [wait ("C"),dec3 Ser_Cmd_numb]
            hserin 100, start, [wait ("V"),dec3 Ser_Cmd_val]
            LCDout $fe, 1, "COMMAND RECEIVED"
            Lcdout $fe, $40, "-- C", dec Ser_Cmd_numb,"  V", dec Ser_Cmd_val, " --" 
            pause 3000                                                  'Pause for 3 seconds for user viewing
            GOTO Chk_Cmd                                                      
        PIE1 = %01110000                                                    'Enable interrupts
    Return
    
    Chk_Cmd:
        select case Ser_Cmd_numb        '001 to 256
            case 1                      'Torch Target Voltage
                TTV_val = Ser_Cmd_Val
            case 2                     'Hysteresis Value
                HYS_Val = Ser_Cmd_Val
            Case else
                LCDout $fe, 1, " COMMAND NOT IN "
                Lcdout $fe, $40, " DEFINED SCOPE! "
        END SELECT
    RETURN
    Cut and shuffle as you will . Thanks guys.

    Chris

Similar Threads

  1. HSERIN for variable length string
    By Pic2008 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 19th February 2010, 05:58
  2. parsing string from hserin
    By xxxxxx in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 2nd April 2009, 18:42
  3. Visual Basic 6 & Access 2000
    By Demon in forum Off Topic
    Replies: 33
    Last Post: - 7th September 2006, 04:39
  4. String Parsing
    By eoasap in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th February 2006, 17:20
  5. serial string parsing
    By billybobbins in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 8th September 2004, 21:34

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts