Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

  1. #1
    Join Date
    Oct 2016
    Posts
    5

    Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    I am communicating between two computers using only strings. I send the host computer "ST.1(2,3)" . The host computer needs to convert it to the function ST.1(2,3), to drive a motor controller board.
    The way I do it now is with statements like this -

    If InStr(CommandRecivedTxt, "ST") > 0 Then
    NewSpeed = Val(Mid(CommandRecivedTxt, InStr(CommandRecivedTxt, "SI") + 3))
    Drive.SI(NewSpeed)
    Exit Sub
    End If

    Which works but is cumbersome for a lot of different functions.
    Is there a way to tell the compiler what I want?
    Using VB.Net 2010
    Thank You!
    rderkis

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,161

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    No you can not convert it to a function.
    You could possibly add a script control and process it as code but it would have to be in a valid syntax for the script engine used.

    More likely you simply need to parse the string and then run the code needed.
    If all of your expected inputs are going to start with a 2 character prefix then a select case block is probably in order. Would need more detail to advise futher
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Oct 2016
    Posts
    5

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    Quote Originally Posted by DataMiser View Post
    No you can not convert it to a function.
    You could possibly add a script control and process it as code but it would have to be in a valid syntax for the script engine used.

    More likely you simply need to parse the string and then run the code needed.
    If all of your expected inputs are going to start with a 2 character prefix then a select case block is probably in order. Would need more detail to advise futher
    Unfortunately the don't all use 2 letters. I started off using case statements but gave up on them and just used if then statements.

    I was able to convert a string into a function in vb code a long time ago using vb6? or perhaps a script, I can't remember. But I thought it was by using some kind of compiler directive.

    As far as more detail. Not sure what I can say other than I want to run a string as a function. This code works just to unwieldy with so many commands.

    If InStr(CommandRecivedTxt, "ST") > 0 Then
    NewSpeed = Val(Mid(CommandRecivedTxt, InStr(CommandRecivedTxt, "SI") + 3))
    Drive.SI(NewSpeed)
    Exit Sub
    End If

    Th Function here is Drive.SI(NewSpeed)
    The string the host received is "Drive.SI(100)"

    Another Example
    Received String "turn.GetPI()"
    Function turn.GetPI()

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,161

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    Well the way you showed your example it is not possible. ST.1(2,3) would not be a function at all. It could possibly be a function call if it were used as script but that function would also have to be in the script and formatted correctly.

    i.e.
    Code:
    Private Function ST.1(a as integer,b as integer) as String  'or whatever your function should return
       ' code that actually processes the values passed into the function
       ' set a return value and return to the calling routine
    End Function
    Of course I have my doubts that a period can be used as part of a function name. Definitely should not be.

    A case block sounds like it may be best but again not enough info to go on to say for sure.
    As a general rule if you are going to be sending commands from one program to another there should be a defined structure for them.
    Could be fixed length, delimited or whatever. Should never be just sending data willy nilly using different lengths of command codes and no delimiters.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Oct 2016
    Posts
    5

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    Quote Originally Posted by DataMiser View Post
    Well the way you showed your example it is not possible.
    Ok,I will be specific. I can tell by your comments you are smart enough to more than understand

    Hardware involved
    2 computers, communicating over wifi.
    1 Robot with a sabertooth 2X32 using a usb connection to one of the computer that will be mounted on board. That sabertooth relays the commands it receives from the USB connected computer to a device that is called a kangaroo x2, that is physically connected to it.

    I call the computer that is mounted on the robot and connected to the sabertooth by USB, the "host computer".

    The other computer I call the "client computer" which I control sends the commands to the host computer, using some moded WiFi Chat code, which only sends strings back and forth.

    I send a variety of both get and move commands. Since it is a differential drive robot it uses 2 channels. One channel I call "Drive" and the other I call "Turn". It gets the functions from a dll like this.
    Imports DimensionEngineering.Kangaroo

    Here is how I process some of the commands by the host computer, sent by client computer.
    These clips work perfect. This is only a snippet. There are many more command and get statements.

    Code:
    'Start of Drive Incremental Position Drive Forward Or Reverse Commands
            If InStr(CommandRecivedTxt, "Drive") > 0 Then
                'if Drive.PI Incremental
                If InStr(CommandRecivedTxt, "PI") > 0 Then
                    NewPosition = Val(Mid(CommandRecivedTxt, InStr(CommandRecivedTxt, "PI") + 3))
                    If InStr(CommandRecivedTxt, ",") > 0 Then
                        NewSpeed = Val(Mid(CommandRecivedTxt, InStr(CommandRecivedTxt, ",") + 1))
                        LabelNewSpeed.Text = NewSpeed
                        LabelNewPosition.Text = NewPosition
                        LabelNewSpeed.Refresh()
                        LabelNewPosition.Refresh()
                        If InStr(CommandRecivedTxt, ":Continuous") > 0 Then
                            Drive.PI(Math.Round(NewPosition * 1.15), NewSpeed)
                            ContinuousMovement()
                            LabelLoopNumber.Text = "Next Loop"
                            LabelLoopNumber.Refresh()
                            Exit Sub
                        End If
                        If InStr(CommandRecivedTxt, ":Normal") > 0 Then
                            Drive.PI(NewPosition, NewSpeed)
                            Do While Drive.GetP().IsDone = False
                            Loop
                            Exit Sub
                        End If
                    End If
                    If InStr(CommandRecivedTxt, ":Continuous") > 0 Then
                        Drive.PI(Math.Round(NewPosition * 1.15), NewSpeed)
                        ContinuousMovement()
                        LabelLoopNumber.Text = "Next Loop"
                        LabelLoopNumber.Refresh()
                        Exit Sub
                    End If
                    If InStr(CommandRecivedTxt, ":Normal") > 0 Then
                        Drive.PI(NewPosition)
                        Do While Drive.GetP().IsDone = False
                        Loop
                        Exit Sub
                    End If
                    Drive.PI(NewPosition)
                    Exit Sub
                End If
    
                'Start Of Incremental Speed Drive Forward Or Reverse Commands
                If InStr(CommandRecivedTxt, "SI") > 0 Then
                    NewSpeed = Val(Mid(CommandRecivedTxt, InStr(CommandRecivedTxt, "SI") + 3))
                    Drive.SI(NewSpeed)
                    Exit Sub
                End If
                If InStr(CommandRecivedTxt, "S") > 0 Then
                    NewSpeed = Val(Mid(CommandRecivedTxt, InStr(CommandRecivedTxt, "S") + 2))
                    Drive.S(NewSpeed)
                    Exit Sub
                End If
            End If
    Now I know this is messy code but I am a self taught amateur and 69 years olds, building my first robot. By the time I am done it will be running autonomously.
    This works perfectly as seen here on a YouTube video I made for my granddaughters eyes only.
    The program is far from done but I can control it from the Client's screen and keyboard, Xbox controller,or voice recognition. All three are demonstrated in this VARY amateurish video. :-)

    https://youtu.be/5rTuZNyxmr4

    This was before I mounted encoders on the motors. Which gives me much better control. The encoders are the part the kangaroo processes.
    Last edited by 2kaud; October 18th, 2016 at 07:38 AM. Reason: Code tags added

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    4,751

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    When posting code, it is useful if you use code tags so that the code is formatted properly and easier to read. [code tags added]

    Impressive video. Sorry I can't be more helpful as I don't program using VB only c++.

    This code works just to unwieldy with so many commands
    As DataMiser said in post #4, there should be a defined structure to the commands used and that structure should be easy to generate and parse. One of the issues here is that the commands used are verbose and seemingly unstructured. Though its probably a bit too late down the development path to change.
    Last edited by 2kaud; October 18th, 2016 at 08:12 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
    C, C++ Compiler: Microsoft VS2015

  7. #7
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,161

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    Do you by chance have a list of the command codes that are to be sent and received? Some examples of what they look like when received?
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Oct 2016
    Posts
    5

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    Quote Originally Posted by 2kaud View Post
    When posting code, it is useful if you use code tags so that the code is formatted properly and easier to read. [code tags added]
    Wow, it does look better with the code tags! Thank you.

    Impressive video. Sorry I can't be more helpful as I don't program using VB only c++.
    I tried learning c++ but am just too old. I figured the main disadvantage of c++ over VB, is VB is much easier to use and learn. On the other hand C++ runs much faster :-) I figured with the speed of computer advancing so fast, that that speed advantage of C++ will be of less importance in real world situations.

    As DataMiser said in post #4, there should be a defined structure to the commands used and that structure should be easy to generate and parse. One of the issues here is that the commands used are verbose and seemingly unstructured. Though its probably a bit too late down the development path to change.

    Like I said I am a armature and self-taught. Since I don't know or talk to any programmers, I probably use the wrong words when trying to communicate a question.
    Since a function receives a return value, I guess the commands, except for the get statements, are not functions.
    Here is a list.
    Drive.P(Position as integer, Speed as integer)
    Turn.P(Position as integer, Speed as integer)
    Drive.PI(Position as integer, Speed as integer)
    Turn.PI(Position as integer, Speed as integer)
    Drive.S(Speed as integer)
    Turn.S(Speed as integer)
    Drive.SI(Speed as integer)
    Turn.SI(Speed as integer)
    Drive.GetP()
    Turn.GetP()
    Drive.GetPI()
    Turn.GetPI()
    Drive.GetS()
    Turn.GetS()
    Drive.GetSI()
    Turn.GetSI()

    There are others but this pretty much covers them.
    The client sends these strings like these to host

    "Turn.S(21)"
    "Drive.SI(22)"
    "Turn.SI(23)"
    "Drive.GetP()"
    "Turn.GetP()"
    There are other string extensions I use for other things, like when I am recording or playing a macro. Example. "Turn.GetP(): Normal”
    The code no matter how sloppy works. What I was asking is how to turn this "Drive.SI(22)" into Drive.SI(22) , on the host computer ‘s VB program. So that it works like a kangaroo command/function.
    I can already do it with case statements or if statements. I thought a long time ago I did it using ether command directives, script or some other means. Which made it really easy, without the messy code.
    Last edited by rderkis; October 18th, 2016 at 12:58 PM.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,403

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    There are better ways of doing this, but if you are committed to your approach, you can use reflection.

    Search bing or google for "invoke a class method using reflection in vb.net".

  10. #10
    Join Date
    Oct 2016
    Posts
    5

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    Quote Originally Posted by Arjay View Post
    There are better ways of doing this, but if you are committed to your approach, you can use reflection.

    Search bing or google for "invoke a class method using reflection in vb.net".
    Thank you!
    It's hard for me to learn new things anymore but I like trying. You speak of better ways, can you give me an example (Please)?
    BTW Once I have this figured out I will be consolidating my code by using function calls to subroutines for repeated code.
    Last edited by rderkis; October 18th, 2016 at 01:49 PM.

  11. #11
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,161

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    Based on what you have showed I think I would use a nested select case
    Code:
    Select Case CommandType
    Case "Drive"
         Select Case CommandCode
              Case "PI"
              Case "P"
              Case "SI"
              ' an so on
         End Select
    Case "Turn"
        Select Case CommandCode
              Case "PI"
              Case "P"
              Case "SI"
              ' an so on
         End Select
    End Select
    You would need to parse the string coming in taking whatever is there before the . and place it in commandtype and then whatever is between the . and the ( and place that in the command code.

    You could then call whatever sub routine or function you need within your code to process the data or command.

    btw in general a Function is a sub routine that returns a value where as a sub routine does not return a value

    Code:
    Private Function SomeFunction() as String
        'do some stuff   
        'Return some string value
    End Function
    
    Private Sub SomeSubRoutine()
        'Do Some Stuff
    End Sub
    Always use [code][/code] tags when posting code.

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,403

    Re: Converting a string to a function. Example "ST.1(2,3)" to ST.1(2,3)

    Quote Originally Posted by rderkis View Post
    You speak of better ways, can you give me an example (Please)?
    Instead of 'better', I should have said 'alternate'.

    Not sure you can do this, but a different way to do this is to abstract the robot commands. Rather than have commands that attempt to mimic visual code, try to move to a format that specifies the command and the data (isn't there a standard of something like this that exists for robots?).

    Then in VB, you convert these commands and call the appropriate class methods. The equivalent conversion(s) are done for the robot code as well.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center