Shop OBEX P1 Docs P2 Docs Learn Events
working with HEX — Parallax Forums

working with HEX

docwisdomdocwisdom Posts: 28
edited 2009-10-09 22:38 in BASIC Stamp
I am trying to do a few things with HEX and am having no luck so far.

I am doing a SEROUT command for our dispenser (who requires hex)
so I need to calculate some HEX values to generate a checksum.

I also am sending a status request to the unit and am using SERIN to get the status, but I am not getting the information I need.

___________________________________________
the checksum
B3 01 0E 7F 01 01 01 01 01 01 01 01 ___ 0D

the blank is the checksum location and is an addition of the previous bytes then %FF (47 in this example)

my attempt
fk_chksm = $B3+$01+$0E+$7F+fk_hopr(0)+fk_hopr(1)+fk_hopr(2)+fk_hopr(3)+fk_hopr(4)+fk_hopr(5)

but the fk_hopr variables are decimal :P

___________________________________________
the status check

SEROUT 4, 16416, [noparse][[/noparse]$B3, $01, $06, $80, $3A, $0D] 'send request for status

the unit then sends
88 01 07 80 00 10 0D
I need to grab the 00 byte

My attempt was
SERIN 3, 16416,[noparse][[/noparse]HEX J(0), HEX J(1), HEX J(2), HEX J(3), HEX VSTATUS]

with no luck. I also tried using a WAIT(128) to look for HEX 80 but that didnt work either.

Post Edited (docwisdom) : 10/8/2009 9:44:00 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-08 23:00
    You probably don't so much want to send hex as the actual values of the bytes.

    Try leaving out the HEX. That way J(0) will be $88, J(1) will be 1, J(2) will be 7, J(3) will be $80, and VSTATUS will be 0
  • docwisdomdocwisdom Posts: 28
    edited 2009-10-09 04:31
    I was able to solve the problem with the checksum by doing two separate sums then adding the sums together.

    fk_chksm_init = $B3+$01+$0E+$7F
    fk_chksm_hopr = fk_hopr(0)+fk_hopr(1)+fk_hopr(2)+fk_hopr(3)+fk_hopr(4)+fk_hopr(5)
    fk_chksm = fk_chksm_init + fk_chcksm_hopr

    interestingly it provided only the last two digits of the addition, 45 instead of 145 which is what I expected. I was planning on having to apply FF to it to get 45. Is this a characteristic of addition in PBASIC?



    Im a little confused on formatting and how PBASIC displays the various formats. How is it that I will get a HEX output without telling the code to operate in HEX? Isnt everything binary in the core anyway?

    Post Edited (docwisdom) : 10/9/2009 4:42:43 AM GMT
  • docwisdomdocwisdom Posts: 28
    edited 2009-10-09 15:50
    I am having an odd phenomenon happening. In this code, say the input QTY is 6. It then populates each fk_hopr variable with 1. When it runs the first debug, everything shows up properly. I then do the addition and the addition is proper. After running the SEROUT I publish the debug again and for some reason fk_hopr(5) has the same value as fk_chksm.

    I cant figure out why this variable is showing up with the wrong value.

    RVEND:
      fk_rolls = QTY
    
      fk_hopr(0) = $00
      fk_hopr(1) = $00
      fk_hopr(2) = $00
      fk_hopr(3) = $00
      fk_hopr(4) = $00
      fk_hopr(5) = $00
      FOR q = 1 TO fk_rolls
        fk_hopr(i) = fk_hopr(i)+1
        i = i+1
        IF i = 6 THEN i = 0
      NEXT
      DEBUG ? fk_hopr(0)
      DEBUG ? fk_hopr(1)
      DEBUG ? fk_hopr(2)
      DEBUG ? fk_hopr(3)
      DEBUG ? fk_hopr(4)
      DEBUG ? fk_hopr(5)
    
    
      fk_chksm_start = $B3+$01+$0E+$7F
      fk_chksm_hopr = fk_hopr(0)+fk_hopr(1)+fk_hopr(2)+fk_hopr(3)+fk_hopr(4)+fk_hopr(5)
      fk_chksm = fk_chksm_start+fk_chksm_hopr
      DEBUG HEX ? fk_chksm_start
      DEBUG HEX ? fk_chksm_hopr
      DEBUG HEX ? fk_chksm
    
    
      DEBUG "Vending ", DEC fk_rolls, " Rolls",CR
      SEROUT 8, 84, [noparse][[/noparse]"VENDING ", DEC fk_rolls, " ROLLS"] 'line 1
      SEROUT 4, 16416, [noparse][[/noparse]$B3, $01, $0E, $7F, HEX fk_hopr(0), HEX fk_hopr(1), HEX fk_hopr(2), HEX fk_hopr(3), HEX fk_hopr(4), HEX fk_hopr(5), $00, $00, HEX2 fk_chksm, $0D] 'vend rolls
      DEBUG HEX ? fk_hopr(0)
      DEBUG HEX ? fk_hopr(1)
      DEBUG HEX ? fk_hopr(2)
      DEBUG HEX ? fk_hopr(3)
      DEBUG HEX ? fk_hopr(4)
      DEBUG HEX ? fk_hopr(5)
      DEBUG HEX2 ? fk_chksm
    
    
  • docwisdomdocwisdom Posts: 28
    edited 2009-10-09 21:53
    By reordering the declaration of the variables I was able to get this working. I must have been running into some kind of variable spillover or something.

    New question:

    I am successfully sending my serial data to the vending unit. It then sends back an acknowledgment string.

    ie
    send: B3 01 06 80 3A 0D
    should receive: 88 01 07 80 00 10 0D
    actually receive 2C 07 00 D4

    so im not getting enough bytes and they are not what I am expecting. I am running at 19200, N8 as noted in the vending manual. The only thing I am not sure on is the 1 stop bit it says to use.

    SEROUT 4, 16416, [noparse][[/noparse]$B3, $01, $06, $80, $3A, $0D]
    SERIN 3, 16416,[noparse][[/noparse]SRC, DST, JNK1, JNK2]
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-09 22:00
    A BS2 really won't work reliably at 19200 Baud. As I've mentioned, 9600 Baud is dicey. Don't even bother trying to receive at 19200 Baud. The BS2 just isn't fast enough to handle it.

    Don't forget that you have to declare fk_hopr as a byte array of the correct size for your code.
  • docwisdomdocwisdom Posts: 28
    edited 2009-10-09 22:31
    Do you recommend a BS2sx then? or some other?
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-09 22:38
    Personally, I'd go for a BS2px. Once you get the application running at full speed, you could try slower and less expensive Stamps like the BS2sx or BS2p, but I'd use the fastest, most full featured Stamp for any kind of development where speed, features, or memory capacity may be an issue. For anything faster than 19200 Baud, you'd need to use something like a Propeller.
Sign In or Register to comment.