Remove String with a Hex D
TurnipTruck
Posts: 1,485
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.
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.
0
Comments
Kevin D.
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.
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
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)
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
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>