Converting HSERIN Data to Number


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Converting HSERIN Data to Number

    Hi,

    I think it's "the usual" case of not treating the data the same way in both ends.

    It looks to me like you are sending ASCII but you're treating the received data as binary/raw numbers.
    If you send the decimal digit '1', store that in myArray[0] and later multiply that by 100 what you'll get is 4900 because 49 is the ASCII code for '1'.
    To do what you want you can use ARRAYREAD with the DEC modifier or simply use the aproach you already have but "offset" the values to account for the ASCII encoding.
    Code:
    varResult = ((myArray[0]-48)*100) + ((myArray[1]-48)*10) + (myArray[2]-48)
    Or you could subtract 48 when receiving the digits.

    /Henrik.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Converting HSERIN Data to Number

    Quote Originally Posted by HenrikOlsson View Post
    Hi,

    I think it's "the usual" case of not treating the data the same way in both ends.

    It looks to me like you are sending ASCII but you're treating the received data as binary/raw numbers.
    If you send the decimal digit '1', store that in myArray[0] and later multiply that by 100 what you'll get is 4900 because 49 is the ASCII code for '1'.
    To do what you want you can use ARRAYREAD with the DEC modifier or simply use the aproach you already have but "offset" the values to account for the ASCII encoding.
    Code:
    varResult = ((myArray[0]-48)*100) + ((myArray[1]-48)*10) + (myArray[2]-48)
    Or you could subtract 48 when receiving the digits.

    /Henrik.
    Thanks Henrik! I'll give it a go tonight after work.

    Preferably, I'd just send the output of the 8-bit ADC module on the Master chip directly to the Slave (so, 0-255) rather than converting it to ASCII in a 3-digit format (e.g. "027"), but then I can't figure out how to process that in the ISR if I'm only getting one character at a time.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Converting HSERIN Data to Number

    So why don't you? ;-)

    Master:
    Code:
    ADResult VAR BYTE
    ....
    ....
    HSEROUT [ADResult]   '1 byte
    Slave:
    Code:
    Duty VAR WORD
    HSERIN [Duty]   '1 byte
    But now, if you're trying to debug this by sending te value of Duty to the PC terminal you need to use the DEC modifier so it converts the value to ASCII for display to us humans, HSEROUT [DEC Duty]

    /Henrik.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Converting HSERIN Data to Number

    Quote Originally Posted by HenrikOlsson View Post
    So why don't you? ;-)

    Master:
    Code:
    ADResult VAR BYTE
    ....
    ....
    HSEROUT [ADResult]   '1 byte
    Slave:
    Code:
    Duty VAR WORD
    HSERIN [Duty]   '1 byte
    But now, if you're trying to debug this by sending te value of Duty to the PC terminal you need to use the DEC modifier so it converts the value to ASCII for display to us humans, HSEROUT [DEC Duty]

    /Henrik.
    Thanks again, Henrik. Can I still incorporate this into my ISR? For that to work, the interrupt on Rc would need to fire when 1 byte gets into the buffet, right? So, instead of an array for 3 'characters' (which on their own are 1 byte, right?) I can just grab the next byte after receiving the AdjCmdVal.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: Converting HSERIN Data to Number

    Hi,
    The UART RX interrupt always* fires for each byte that comes into the RX buffer, there's no way to set a "threshold" on when the interrupt fires. After all, the buffer is only two bytes deep. But yes, you could set your code up so that after proper sync, adress, command etc you grab one byte and that's it.

    * Well not always but that's details details details.....

    /Henrik.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Converting HSERIN Data to Number

    Quote Originally Posted by HenrikOlsson View Post
    Hi,
    The UART RX interrupt always* fires for each byte that comes into the RX buffer, there's no way to set a "threshold" on when the interrupt fires. After all, the buffer is only two bytes deep. But yes, you could set your code up so that after proper sync, adress, command etc you grab one byte and that's it.

    * Well not always but that's details details details.....

    /Henrik.
    Thanks again, Henrik. Much appreciated. I'm going to try all this out tonight.

  7. #7
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Converting HSERIN Data to Number

    Code:
    serialbuffer byte [4]
    serialbyte var byte
    index var byte
    
    …
    
    ‘ ooh looky here… a serial byte arrived
    for index = 1 to 3
    serialbuffer[index] = serialbuffer[index-1]
    next index
    serialbuffer[0] = serialbyte
    Now instead of checking for the first byte in the serial string,
    check for what you call the termination byte.
    I don’t know what that is, so now it’s $FE.

    Code:
    if serialbuffer[0] = $FE then
    ‘ we know the bytes serialbuffer [0,1,2,3] are the desired 4 byte string in reverse
    ‘ unless only a partial command was received
    if serialbuffer[3] = “S” then
    ‘ we know now that the entire four byte command was received if there was no serial error
    endif
    endif

  8. #8
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Converting HSERIN Data to Number

    Now if you only wanted to grab say byte 2 (beginning counting from zero).

    Code:
    counter var byte
    counter = 0
    
    'oh looky here.. a serial byte arrived
    
    if serialbyte = “S” then
    counter = 0
    else
    counter = counter + 1
    endif
    
    if counter = 2 then
    byteyoucareabout = serialbyte
    endif
    A lot of this will fall apart if another byte of the command can be the same value as the “S” or the termination character,
    which is why it’s better to go something close to the first way, and check both start and end chars if they are always the same.
    Last edited by Art; - 18th November 2015 at 05:29.

Similar Threads

  1. Replies: 3
    Last Post: - 22nd June 2015, 16:15
  2. read bytes number in Hserin
    By Rony in forum Serial
    Replies: 13
    Last Post: - 13th August 2010, 10:32
  3. Unable to use the number 8 in DATA statement
    By dbachman in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 7th December 2008, 06:34
  4. Problem converting data to what I need
    By oldcarguy85 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd November 2007, 22:43
  5. Need help converting NMEA data
    By tcbcats in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 21st August 2004, 08:28

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts