Home AMX User Forum NetLinx Studio

Small Parsing Problem

I was just messing around with some strings yesterday and could not get this working.....Could anyone take a quick look and let me know what Im doing wrong?? If you have some advice about my form LMK.
Nothing makes it into the msgQue...It makes it into vBuffer. Im sure it something retarded on my part...again.

LOCAL_VAR vBuffer[70]
LOCAL_VAR msgQue[70]
vBuffer = "vBuffer,DATA.TEXT"
msgQue=left_string(vBuffer,7)
while(find_string(vBuffer,"'P'",1))
{
msgQue=remove_string(vBuffer,1,7)
switch(msgQue)
{
case 'PowerOn':
{
PowerOn();
}
}
}

}

Comments

  • ondrovicondrovic Posts: 217
    LOCAL_VAR vBuffer[70]
    LOCAL_VAR msgQue[70]
    vBuffer = "vBuffer,DATA.TEXT"
    msgQue=left_string(vBuffer,7)
    while(find_string(vBuffer,"'P'",1))
    {
    msgQue=remove_string(vBuffer,1,7)
    switch(msgQue)
    {
    case 'PowerOn':
    {
    PowerOn();
    }
    }
    }

    }

    Try this
    vBuffer = Data.Text
    
  • Spire_JeffSpire_Jeff Posts: 1,917
    SCOTTYP wrote: »
    msgQue=remove_string(vBuffer,1,7)
    switch(msgQue)
    {
    case 'PowerOn':
    {
    PowerOn();
    }
    }
    }

    }
    The problem is your use of remove_string(). Remove_string() is looking for (I think) decimal value 1 (unprintable character) in the string starting at position 7. If it finds a 1, it will remove EVERYTHING before the 1 and the 1 and assign them to your variable. Try get_buffer_string(vBuffer,7). I am assuming that you are trying to get the first 7 chars in the queue. Keep in mind that this relies heavily on proper communication and no noise. If the protocol that you are parsing provides beginning and terminating chars, you would be better parsing based on those characters.

    Jeff
  • I thought data.text is only available in data_events under the string or command handler.
  • mpullinmpullin Posts: 949
    kbeattyAMX wrote: »
    I thought data.text is only available in data_events under the string or command handler.
    The original poster gave this code block totally out of context but since DATA.TEXT is present it's okay to assume it's in a data event. I think.
  • SCOTTYPSCOTTYP Posts: 32
    This is a data event under a command header...sorry for not sharing.
  • Spire_JeffSpire_Jeff Posts: 1,917
    ondrovic wrote: »
    Try this
    vBuffer = Data.Text
    

    The only problem with this is if the data comes in broken. If this is in fact in the command section tho, the AMX processor should not break commands.

    As for my original reply, you could insert this code before the get_buffer_string() to strip out everything before the 'P':

    get_buffer_string(vBuffer,(find_string(vBuffer,"'P'",1)-1));

    The only problem that might come up is if the 'P' is the first character. I'm not sure how a get_buffer_string with a 0 value will work.

    Jeff
  • mpullinmpullin Posts: 949
    Had to do a ninja edit there, for a second I thought Jeff had forgotten about the one-based arrays, should have known better -_-

    I'd try it. GET_BUFFER_STRING(any_string,0) should have absolutely no effect, based on the description of what GET_BUFFER_STRING does. If it does something unexpected, like gobble up your whole string and result it as the function's result, well, we'll learn something :p
  • SCOTTYPSCOTTYP Posts: 32
    Well........Apparently msgQue is working?? For some reason I cant see whats in it. The loop went to the function and stays in the Loop??? Anyone know why I would be able to see vBuffer and not msgQue? It does not let me choose the type (ascii, hex, etc.) in debug view. Im thinking I need to extract whats in msgQue to a garbage variable, at least thats what Im gonna try. Heres a thankyou to everyone for helping me out. Here is the code:

    Command:
    {
    LOCAL_VAR vBuffer[70]
    STACK_VAR msgQue[70]
    STACK_VAR x
    vBuffer = DATA.TEXT
    msgQue=left_string(vBuffer,7)
    while(find_string(msgQue,"'P'",1))
    {

    switch(msgQue)
    {
    case 'PowerOn':
    {
    PowerOn(); //power on function
    }
    }
    }

    }
  • Spire_JeffSpire_Jeff Posts: 1,917
    The problem lies within your use of the remove_string function. It is NEVER going to get anything if the commands being sent are ASCII character strings. Try switching to the get_buffer_string() command I posted. In doing that, the while loop should function properly.

    If you are really worried about the while loop getting stuck, you could change it to be this:
    STACK_VAR nWhileCnt;
    LOCAL_VAR vBuffer[70]
    LOCAL_VAR msgQue[70]
    vBuffer = "vBuffer,DATA.TEXT"
    nWhileCnt = 1;
    while(find_string(vBuffer,"'P'",1) and nWhileCnt<100)
    {
    nWhileCnt++;
    get_buffer_string(vBuffer,(find_string(vBuffer,"'P '",1)-1));
    msgQue=get_buffer_string(vBuffer,7);
    switch(msgQue)
    {
    case 'PowerOn':
    {
    PowerOn();
    }
    }
    }
    
    }
    

    Try that code and see if it works.

    Jeff
  • SCOTTYPSCOTTYP Posts: 32
    Just to keep you updated..I tried that last code block Jeff and I actually feel as though Im getting somewhere. For whatever reason I can now see whats in all the varibles now. (if someone knows what I was doing wrong LMK) I added a variable to flag if it made it into the loop and didnt execute the function. That works and that is where Im at...Im gonna mess around with that for a few. Thanx alot Jeff!
  • Spire_JeffSpire_Jeff Posts: 1,917
    If it is having problems with the switch()..case stuff, try switching to a select..active. Also, remember that case sensitivity comes into play.

    here is how the select..active would look:
    select{
      active(Compare_string(msgQue,'PowerOn')):
         PowerOn();
    
    }
    
    

    Jeff
  • SCOTTYPSCOTTYP Posts: 32
    Alright it seems to be working...I took out (get_buffer_string(vBuffer,(find_string(vBuffer,"'P '",1)-1));), from Jeffs post and it works......I think it all came back to something with my syntax? Maybe my usage of the local and stack var?? Until I used jeffs block nothing besides vBuffer was updating, but it would turn the plasma on and get stuck in the loop. Im gonna play around with this for a while and see what happens. Thanxs again.
  • What is feeding the vbuffer? If your just sending commands like you mentioned and it's being handled with COMMAND:

    I would just do this:
    DEFINE_EVENT
    
    button_event[someTPdevice,1]
    {
      push: send_command someVirtualDevice,'PowerOn'
    }
    
    data_event[someVirtualDevice]
    {
      command:
    	{
    	  switch (data.text)
    		{
    		  case 'PowerOn': PowerON()
    		}
    	}
    }
    

    The reason why. It highly unlikely that you are going to overrun DATA.TEXT especially if you know what you are sending to it. Every subsequent send_command will overwrite DATA.TEXT and not append the information so there is no need to parse the buffer because it's not buffering. Again, I'm not too sure if this is what you are trying to do.
Sign In or Register to comment.