Home AMX User Forum NetLinx Studio

Remove String with a Hex D

Greetings,

I have a device that sends a $0D as an end of transmission. I would like to remove all before the $0D as my message from the device.

REMOVE_STRING (cMESSAGE,$0D,1) does not work. I am assuming because $0D is not a printable ASCII character.

Does anyone have another suggestion?

System is Netlinx.

Thank you.

Comments

  • Is it sending hex or ASCII that's hex codes? If it's sending HEX, have you tried (cMESSAGE,"$0D",1)?

    Kevin D.
  • champchamp Posts: 261
    Try REMOVE_STRING (cMESSAGE,"$0D",1)

    or to debug it

    IF (FIND_STRING (cMESSAGE,"$0D",1)
    {
    SEND_STRING 0, 'I found a carriage return'
    REMOVE_STRING (cMESSAGE,"$0D",1)
    }

    Also if you are reading DATA.TEXT, then you could not get all the text at once as it overwrites itself every time new data comes in.
    If you use a buffer then the string concatenates itself with new incoming text

    ...removing strings and characters takes more processor power but probably only matters when using an Axcent.
  • TurnipTruckTurnipTruck Posts: 1,485
    It's sending a hex 0D which is equivalent to a decimal 13 or an ASCII carriage return. I do not know how to specify that in a REMOVE_STRING command.
  • champchamp Posts: 261
    You've got it right

    specify it as "$0d" or "13"

    by surrounding it with "" you make it a character string literal of length 1, whereas without adding the "" the program is looking for an integer which it'll never find in a serial string
  • Joe HebertJoe Hebert Posts: 2,159
    I have a device that sends a $0D as an end of transmission. I would like to remove all before the $0D as my message from the device.
    REMOVE_STRING returns everything up to and including the search string in question. So if you want everything before the $0D, you?ll have to add one more line of code like SET_LENGTH_ARRAY, SET_LENGTH_STRING, or RIGHT_STRING to lop off the terminating carriage return of your message.
  • this is one way to clean it up ...

    define_constant
    char EOL_CHAR[1] = $0D
    integer MAXBUFFER = 128

    define_function char[MAXBUFFER] RemoveEOL(char in_string)
    {
    stack_var integer EOL_LOCATE

    EOL_LOCATE = FIND_STRING(in_string,EOL_CHAR,1)
    if(EOL_LOCATE) SET_LENGTH_STRING(in_string,EOL_LOCATE-1)

    // no need to return variable string seperately, it was modified directly
    // within this function. but we will return it as an example

    return in_string
    }

    .. now from anywhere in your program state

    RemoveEOL(SourceString)
  • this is one way to clean it up ...
    EOL_LOCATE = FIND_STRING(in_string,EOL_CHAR,1)
    if(EOL_LOCATE) SET_LENGTH_STRING(in_string,EOL_LOCATE-1)

    And kiss anything that might be past EOL_LOCATE goodbye, so don't do this to an incoming buffer attached to an RS232 port, right? :)

    - Chip
  • yeah, your right, it does truncate the string, and i forgot to mention that.

    what i sometimes do is use the result EOL_LOCATE to actually split the string. (compare LENGTH_STRING(in_String) to EOL_LOCATE).

    you can create the tail string with something like this in the RemoveEOL()..

    if(EOL_LOCATE<LENGTH_STRING(in_String)
    tail_string = in_string[EOL_LOCATE+1]
    else
    tail_string = ''

    return tail_string // instead of return in_string
    }

    before you set the new length of in_string.

    the function could then return the tail string, as it had modified the source string internally. then, when the function returns, check length of tail_string, and do it again if need be (do-while-loop).

    char tail_string[MAXBUFFER]
    char check_string[MAXBUFFER]

    check_string = SourceString

    do {
    tail_string = RemoveEOL(check_string) // new version, must return tail from example above (i've place full example at the bottom)

    .... do something with check_string here....
    .... and then .....

    if(LENGTH_STRING(tail_string))
    check_string = tail_string

    } while(LENGTH_STRING(tail_string))


    i had left it simpler in the other example, i based it on an assumption of not expecting more in the line, that we just wanted to remove EOL.

    so Chips warning is quite valid. watch out for truncating :)


    <code>
    define_constant
    char EOL_CHAR[1] = $0D
    integer MAXBUFFER = 128

    define_function char[MAXBUFFER] RemoveEOL(char in_string)
    {
    stack_var integer EOL_LOCATE

    EOL_LOCATE = FIND_STRING(in_string,EOL_CHAR,1)
    if(EOL_LOCATE<LENGTH_STRING(in_String)
    tail_string = in_string[EOL_LOCATE+1]
    else
    tail_string = ''

    if(EOL_LOCATE) SET_LENGTH_STRING(in_string,EOL_LOCATE-1)

    return tail_string
    }
    </code>
Sign In or Register to comment.