Shop OBEX P1 Docs P2 Docs Learn Events
Need help (sample code) to convert text string to integer, compare with a table of values — Parallax Forums

Need help (sample code) to convert text string to integer, compare with a table of values

AkbarAkbar Posts: 3
edited 2018-05-21 12:04 in Propeller 1
Problem statement. Need to convert text string to integer, compare them to a table of 14 resistors (rows) with +/-5% tolerance and provide pass or fail result.
Input: From Digital voltmeter (DVM), (14 bytes (time=16msec) at 9600 baud every 720 msec.) as a resistance value in Ohms, in to propeller board.
Input format: Data is format is like (“+0104 3! NULL NULL SPACE NULL CR LF”) Resistance value in Ohms. 3! Is where decimal point need to get inserted into the 4 digit number to become 010.4 Ohms.
Software used: full-duplex_test.spin and full_duplex Communication Engin.spin,
Hardware setup: DVM RS232(Tx) goes to Propeller Rx, then propeller Tx goes to Rx of pc Parallax serial terminal, during the test.
What I got so far: Data from DVM comes to propeller, I remove the "+" from the string, by advancing the address and send the string to Parallax serial terminal now I get 0104 3! ohms.

result := com0.readString(@data, 9) ' wait till string arrive read 9 characters of it (+0104 3!null)(0 terminated string)
com0.writeString(result + 1) ' advance pointer to remove "+" in front of the number.

What it need to be done:
1. I tried many thing to insert the decimal point Into 4 digit base number to get 010.4 Ohms, still got problem. DVM always give 4 digit with decimal point insertion separate like 1!, 2!, 3!. We need to generate fraction number for next steps.
2. I need to make a table with 14 rows and 3 columns. Rows are 14 given resistance, columns (A=resistance value), Column (B= +5% of calculate of column A), Column (C= -5% of calculated value of column A). We could have a one column table if we could calculate the +/- tolerance’s mathematically during the process.
3. Then we can produce a result if DVM reading is in range or not, and report a result over RS232.

Comments

  • Try this forum post (see Duane's solution about 67 posts down from the top: https://forums.parallax.com/discussion/161242/parsing-serial-data-in-spin-converting-ascii-hex-to-a-decimal-number


    dgately
  • AribaAriba Posts: 2,690
    Just calculate with resistor values in mOhm (milliOhm) instead of Ohm , so you don't need to insert a decimal point. Instead you multiply with 10 for every dec-point position.
    This gives you integer values, that can easy be compared to a table.

    Here is a testcode with methodes you can use in your code:
    CON
      _clkmode  = xtal1 + pll16x
      _xinfreq  = 5_000_000
    
    OBJ
      ser : "FullDuplexSerial"
          
    PUB Main : v
      ser.start(31,30,0,115200)
      repeat
        v := getResistance(@TestString1)
        showOhm(v, 0)
        v := getResistance(@TestString2)
        showOhm(v, 1)
        v := getResistance(@TestString3)
        showOhm(v, 2)
        v := getResistance(@TestString4)
        showOhm(v, 3)
        v := getResistance(@TestString5)
        showOhm(v, 4)
        waitcnt(clkfreq*2 + cnt)
    
    
    pub getResistance(strp) : val | dp
      repeat 4
        val := val * 10 + byte[++strp]-"0"      'string to value
      dp := byte[++strp]-"0"                    'decimalpoint postion
      repeat dp
        val := val * 10                         'value in milliOhm (Ohm * 1000)
    
      if byte[++strp] <> "!"
        val := -1                               'return -1 if format is wrong
            
    
    pub checkTolerance(num, measured) : pass
      pass := true
      if measured > Rtable[num] + Rtable[num]/20   '> +5% ?
         pass := false 
      if measured < Rtable[num] - Rtable[num]/20   '< -5 % ?
         pass := false 
    
    
    pub showOhm(val, num)
      ser.dec(val)
      ser.str(string(" = "))
      ser.dec(val/1000)
      ser.tx(".")
      ser.dec(val/100 // 10)
      ser.dec(val/10 // 10)
      ser.dec(val/1 // 10)
      ser.str(string(" Ohm : "))
      if checkTolerance(num, val)
        ser.str(string(" PASS ",13))
      else
        ser.str(string(" FAIL ",13))
    
    DAT
    TestString1  byte "+19995!",0,0,32,0,13,10
    TestString2  byte "+12343!",0,0,32,0,13,10
    TestString3  byte "+43212!",0,0,32,0,13,10
    TestString4  byte "+56781!",0,0,32,0,13,10
    TestString5  byte "+98760!",0,0,32,0,13,10
    
    Rtable       long  199900200, 1234011, 432199, 56780, 9000   'Resistor +-0% values milliOhm
    

    Andy
Sign In or Register to comment.