Shop OBEX P1 Docs P2 Docs Learn Events
Reading/Displaying HEX bytes? — Parallax Forums

Reading/Displaying HEX bytes?

FalconFalcon Posts: 191
edited 2013-05-12 15:44 in BASIC Stamp
I'm trying to write code to read PELCO-D code from a PTZ camera interface.

PELCO-D is sent as a string of 7 bytes. The first is always $FF. In my set-up, the second byte is the camera ID of $02. I want to display the 7 bytes in HEX on a line then CR to the next line.

I have communication established between the BS2 and the interface. For testing I am sending the CAMERA STOP command (FF, 02, 00, 00, 00, 00, 02) to the BS2

My code as it is:
' {$STAMP BS2}
' {$PBASIC 2.5}

serStr VAR Byte(7)                                                             ' make 10-byte array

Main:
LOW 1                                                                              ' Enable Receive
SERIN 0, 16780, [STR serStr\7] ' get nine bytes             ' Receive 7 bytes of HEX bytes
DEBUG STR serStr\7, CR                                                  ' Display 
HIGH 1                                                                            '  Disable Receive
GOTO main

The result on the DEBUG Terminal for the CAMERA STOP command is: ?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-12 10:14
    Please read the chapter on the DEBUG and SEROUT statements in the Basic Stamp Syntax and Reference Manual. The DEBUG statement you've used transmits the bytes received from your camera interface exactly as they were received. If you want to display these in hex format, you'll have to process them one byte at a time like this:
    FOR i = 0 to 6
       DEBUG HEX2 serStr(i)
       IF i < 6 THEN DEBUG " "
    NEXT i
    DEBUG CR
    
  • FalconFalcon Posts: 191
    edited 2013-05-12 15:33
    Thanks Mike,

    That sure did make a difference. I've been looking at those two sections but I must have glazed over the parts that would have helped me. I was also using the inverted Baud Rate code so that didn't help.

    With the following code, I'm getting results much more in line with what I should:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    serStr VAR Byte(7) ' make 7-byte array
    Index VAR Byte
    Main:
    LOW 1
    SERIN 0, 396, [STR serStr\7] ' get nine bytes
    
    FOR index = 0 TO 6
       DEBUG HEX2 serstr(index)
       IF index < 6 THEN DEBUG " "
    NEXT
    DEBUG CR
    
    HIGH 1
    GOTO main
    

    Where I should get "FF 02 00 00 00 00 02" though, I get "00 FF 02 00 00 00 00". I'm not sure where that first 00 byte is coming from. The 7th byte is a check sum that includes the 5 bytes after the FF. In my example it should be 02 but I think this it's getting truncated.

    Doesn't it look like I should be receiving 7 bytes? I think I may need to use a WAIT modifier to begin the 7 byte string at the $FF byte.


    falcon
  • Mike GreenMike Green Posts: 23,101
    edited 2013-05-12 15:44
    Yes, you should use a WAIT($FF) to synchronize the two devices, then you only need 6 bytes for serStr.
Sign In or Register to comment.