Shop OBEX P1 Docs P2 Docs Learn Events
spin trying to send and receive bytes to linux host — Parallax Forums

spin trying to send and receive bytes to linux host

manitoumanitou Posts: 29
edited 2012-11-24 13:48 in Propeller 1
Here is spin program that awaits 4 bytes from host over serial port (they come every 10 seconds) and the propeller (quickstart) sends back 4 bytes of the long for the elapsed milliiseconds
'  drift run with  hostdrift, send millis on recv 4 bytes
CON

  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000

VAR
  long ms,t0,t1

OBJ

   pst : "Parallax Serial Terminal"


PUB drift                       ' Method declaration
    pst.start(115200)                 ' Start PST on another cog.
    dira[17] :=1
    t0 := cnt

    repeat                            ' Endless loop prevents
                                      ' program from ending.

        ms.byte[0] := pst.CharIn           ' Get value
        ms.byte[1] := pst.CharIn           ' Get value
        ms.byte[2] := pst.CharIn           ' Get value
        ms.byte[3] := pst.CharIn           ' Get value
        t1 := cnt
        ms :=  (||(t1-t0)/(clkfreq / 1_000))  ' ms
 '      ms := 70000

        pst.Char(ms.byte[0])
        pst.Char(ms.byte[1])
        pst.Char(ms.byte[2])
        pst.Char(ms.byte[3])
        !outa[17]               ' toggle

Here is the output from the host, the 2nd column should be milliseconds (bout 10000 more each time) from the propeller, and the first few lines are OK, but then it goes funky, but every so often you will again see 10000 increments.
1353788943.317606  3080 0.000000 0.000000 -2147483648 
1353788953.319655  13082 10.002049 10.002000 4 
1353788963.321925  23084 20.004319 20.004000 15 
1353788973.323339  20600 30.005733 17.520000 416111 
1353788983.324908  10599 40.007302 7.519000 812059 
1353788993.326382  598 50.008776 -2.482000 1049631 
1353789003.328036  9403 60.010430 6.323000 894634 
1353789013.329704  19404 70.012098 16.324000 766840 
1353789023.331784  24280 80.014178 21.200000 735046 
1353789033.333597  14278 90.015991 11.198000 875599 
1353789043.335052  4277 100.017446 1.197000 988032 
1353789053.336930  5724 110.019324 2.644000 975967 
1353789063.338710  15725 120.021104 12.645000 894643 
1353789073.340377  25727 130.022771 22.647000 825822 
1353789083.342016  17958 140.024410 14.878000 893747 
1353789093.343525  7956 150.025919 4.876000 967498 
1353789103.345161  2044 160.027555 -1.036000 1006473 
1353789113.346580  12045 170.028974 8.965000 947273 

The LED is flashing, so the propeller is getting a host message every 10 seconds (and the host program) has been used with other boards (maple, uno). If I send back a constant value, that seems to work, or if just send back the 4 bytes from the host, that seems to work. But somehow CNT or something isn't working like I expect ??

help.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-11-24 13:23
    cnt wraps around from $ffff_ffff to zero. When that happens, your ms values will be incorrect. You could try this instead:
    ms = ((t1 - t0) >> 1) / (clkfreq / 500)
    

    But that only delays the problem. After about 53 seconds, t1 will wrap around and past t0, and your results will be off again. So you need to keep track of those wraps-arounds in order to correct the results.

    -Phil
  • Heater.Heater. Posts: 21,230
    edited 2012-11-24 13:48
    manitou,

    Your current code is structured so:
        t0 := cnt
        repeat
            'stuf
            t1 := cnt
            ms :=  (||(t1-t0)/(clkfreq / 1_000))  ' ms
    	'stuff
    
    Clearly the difference between t1 and t0 will get bigger and bigger until it overflows 32 bits, about 4 billion ticks or 40 odd seconds.

    You could arrange to count milliseconds per loop, something like
        t0 := cnt
        repeat
            'stuf
            t1 := cnt
            msPerLoop :=  ((t1 - t0)/(clkfreq / 1_000))  ' ms
            t0 := t1
    	'stuff
    
    You could then add something like "msTotal += msPerLoop" as well and now you measure total run time up to 40 of thousand seconds. If you want to time longer runs than that the start tallying up seconds, minutes, hours, days, etc separately.
Sign In or Register to comment.