Shop OBEX P1 Docs P2 Docs Learn Events
Hexadecimal Reading with the Serin command — Parallax Forums

Hexadecimal Reading with the Serin command

Manuel HenriquesManuel Henriques Posts: 4
edited 2014-04-02 23:27 in BASIC Stamp
I need the following help...

I have a display that send and receive the command in hex , and I'm having trouble to read with serin command (BS2)...


Example


Reading date and time sending the command (SEROUT) A5 5A 03 81 20 07

The response of the display (SERIN) A5 5A 0A 81 20 07 01 04 14 05 21 24 12

01 04 14 - DATE (D-M-Y)
05 - DAY WEEK (W)
21 24 12 - TIME ( H-M-S)

I need your help to learn how to send and receive the data used Serin and Serout Command and put one of the fields (D) (M) (Y) - (H) (M) (S) - (W) individually in the following variables (DAY) (MOTH) (YEAR) - (HOUR) (MIN) (SEC) - (WEEK).

If someone can post an example I am very grateful,

Best Regards,

Manuel Henriques





Comments

  • Hal AlbachHal Albach Posts: 747
    edited 2014-04-01 18:25
    Is your display using an asynchronous interface (RS232) ? If not then you may need to use Shiftin and Shiftout instead. Can you provide some additional information about the display and the interface?
  • SapphireSapphire Posts: 496
    edited 2014-04-01 19:02
    Have you tried this?
    SERIN RX_PIN,BAUD,[HEX2 day,HEX2 month,HEX2 year,HEX2 dow,HEX2 hour,HEX2 minute,HEX2 second]
    

    The HEX2 modifier makes sure the data is 2-digit hex before placing it into the variables. The assumes the data stream is as you showed in color. What are the bold values? If the seven variables of the date are encoded in the six bold hex bytes, then you will have to translate it according to how it is encoded. In that case, you would have to read the six hex bytes into an array and manipulating it later. SERIN can't parse data down to the bit level.
    SERIN RX_PIN,BAUD,[STR string\6]
    

    where string is a six element array of bytes.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-04-01 19:21
    Welcome to the forums Manuel!

    I'll echo the questions for more information from Hal and Sapphire. Here is another possibility.

    If those are binary coded decimal values (which are closely related to hex, and commonly used by real time clocks) then here is some alternative code:
    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    day   VAR BYTE
    month   VAR BYTE
    year   VAR BYTE
    dweek   VAR BYTE
    hour   VAR BYTE
    minute   VAR BYTE
    second   VAR BYTE
    
    baudmode CON 396   ' 2400 baud true
    
    SEROUT 0,baudmode,[ $A5, $5A, $0A, $81 $20, $07]
    SERIN 1,baudmode,[ STR day\7]
    

    The data ends up as BCD values in the successive memory locations named from "day" to "second". The variables have to be defined in the order shown and they make what is called an "implicit array".
  • Manuel HenriquesManuel Henriques Posts: 4
    edited 2014-04-01 19:50
    Thank you... Hal, Sapphire and Allen


    Because of the time difference, morning will be posting more information about the display and control to guide me in the best way ...


    In the office tomorrow I will test some of this information...

    A5 5A 0A 81 20 07 01 04 14 05 21 24 12 This is the information I get from the display, only care the last 7 bytes. With this command SERIN 1,baudmode,[ STR day\7], information received in bold is ignored?

    Thank you very much,

    Best regards
  • SapphireSapphire Posts: 496
    edited 2014-04-01 19:57
    Yes, if you only want the last seven bytes, then use this:
    SERIN RX_PIN,BAUD,[SKIP 6,HEX2 day,HEX2 month,HEX2 year,HEX2 dow,HEX2 hour,HEX2 minute,HEX2 second] 
    

    which will ignore (skip) the first 6 bytes, and then read in the next seven into the variables shown.
  • Manuel HenriquesManuel Henriques Posts: 4
    edited 2014-04-02 13:28
    Dear Sapphire and Allen,

    Thanks for your explanations. I tried both options and both work.

    Command to receive the date and time:

    SEROUT Tx,br,[$A5, $5A, $03, $81, $20, $07]

    Return with date and time

    SERIN Rx,br,[skip 6, Year]
    In this example only did the reading year

    My doubt is the following, the information in this variable year is 20, which corresponds to the year 14 ... I need to compare the year 14 ... as if I'm inside my variable 20... How can I make this check.

    Exemple:

    If Year = 14 Then Start

    Thank you very much,

    Best regards


  • SapphireSapphire Posts: 496
    edited 2014-04-02 13:35
    Use
    SERIN Rx,br,[skip 6, DEC2 year]
    

    as it appears the data is DECimal, not HEX. Then year = 14. The "2" in DEC2 means two digits. You must include this otherwise the DEC command doesn't know when the digits end if they are not separated by a space.
  • Manuel HenriquesManuel Henriques Posts: 4
    edited 2014-04-02 17:18
    Dear Sapphire,

    I did the command, but the variable year is "0" .... SERIN Rx,br,[skip 6, DEC2 year]

    There are some test for me to do,

    Best regards



  • SapphireSapphire Posts: 496
    edited 2014-04-02 17:50
    Well then it looks like what Tracy said earlier may be the case. The data you are receiving is binary, and probably binary coded decimal (BCD). When you read in year without HEX or DEC then you are getting the binary value. If you get 20, which is the same as HEX 14, it's probably BCD. There is no direct way to read in BCD. So read it in as a binary number and then convert it:
    SERIN Rx,br,[skip 6,year]
    
    year = 10*year.HIGHNIB + year.LOWNIB
    

    This should give you the correct decimal value.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-04-02 23:27
    If it is in fact BCD data, then follow up the variables and code from post #4,

    SEROUT tx,br,[ $A5, $5A, $0A, $81 $20, $07] 
    SERIN rx,br,[ SKIP 6, STR day\7]
    
    DEBUG hex2 day,"/",hex2 month, "/",hex2 year,32
    DEBUG hex2 hour,":",hex2 minute, ":",hex2 second,13
    DEBUG "day of week =",hex dweek,13
    

    The BCD values are stored in the named Stamp variables starting at "day". Use the formula suggested by Sapphire if you need to convert them from BCD to straight binary. You might want to do that for purposes of elapsed time computation.

Sign In or Register to comment.