RobotStudio event

String Manipulation



Hi,
I'm creating a RAPID program that communicates thru RS232 with a PC. The PC is gonna send some information that will end with an Enter (
) and I need to extract the string before the "
".
The information sent by the PC is not always going to be the same lenght.
What command should i use to read to the port if I donA't know the lenght of the data?
Which command should i use to search for this characters "
" and extract the data?
 
So far this is what i have to open and read:
    VAR iodev puerto;
    VAR string stRead
    Close puerto;
    Open "com1:", puertoBin;
    stRead:=ReadStrBin (puerto, ??);
 
Thank you for your kind support.
 
   
 
Zasil Medina

Comments

  • Hi.

    I have just written some code that should do exactly what you are looking for. I have only done some limited testing of the code though, but at least it seems to work for delimiters of length 1.

    I'm not sure of the performance of the code as it reads 1 character at the time but RS232 isn't terribly fast to begin with so hopefully that wont be a problem.


    [CODE]    LOCAL FUNC string ReadStrMab(VAR iodev IODevice,string Delim)
            VAR string sBuffer:="";
            VAR string sBuf:="";
            VAR string sChar:="";
            VAR string sDelimiter:="A";
            VAR num nDelimLength:=1;

            IF (Present(Delim)) THEN
                sDelimiter:=Delim;
                nDelimLength:=StrLen(sDelimiter);
            ENDIF

            WHILE (sBuf<>sDelimiter) DO
                sChar:=ReadStrBin(IODevice,1);
                sBuf:=sBuf+sChar;

                IF (StrLen(sBuf)>nDelimLength) THEN
                    sBuffer:=sBuffer+StrPart(sBuf,1,1);
                    sBuf:=StrPart(sBuf,2,StrLen(sBuf)-1);
                ENDIF
            ENDWHILE

            RETURN sBuffer;
        ENDFUNC[/CODE]

    If you dont supply your own delimiter A will be used (hex for newline).