ATTENTIONThis FlexSim Community Forum is read-only. Please post any new questions, ideas, or discussions to our new community (we call it Answers) at https://answers.flexsim.com/. Our new Question & Answer site brings a modern, mobile-friendly interface and more focus on getting answers quickly. There are a few differences between how our new Q&A community works vs. a classic, threaded-conversation-style forum like the one below, so be sure to read our Answers Best Practices. |
flexsim.com |
|
Downloads |
Wiki Articles Instructional documents authored collectively by the Flexsim community |
#1
|
||||
|
||||
Controlling Flexsim Remotely
This article explains different methods that can be used to control Flexsim remotely.
There are many methods by which Flexsim can be controlled remotely. Some of these methods have yet to be explored in detail, so while a lot of capability exists in Flexsim already, knowledge is somewhat limited right now in various areas. This article hopes to gather this knowledge into one area for users to refer to. Batch Files and VBS/VBA Command-line Execution One method for controlling Flexsim remotely is through executing shell commands to open a Flexsim model, passing command-line parameters through the shell that the Flexsim model can then interpret and make decisions from. This can be done either through .bat (batch) files, or through things like VBScript or VBA. Attached is a set of files that performs this method: Remote Flexsim Exec.zip Included in the attachment are three files: a flexsim model, a .bat file, and a vbscript file. You must unzip the flexsim model into your userprojects directory, as the script files assume that the model is in that directory. Once you've extract the files, just double-click on either Remote Flexsim Exec.bat or on Remote Flexsim Exec.vbs. This will open the flexsim model (it's just a regular post office model; one source, one queue, one processor, and one sink.) and print a message stating the "Model Scenario" as "5". Batch File The code in the batch file is as follows: Code: "C:\Program Files\Flexsim4\program\flexsimrunner.exe" "C:\Program Files\Flexsim4\userprojects\remotetest.fsm" /scenario 5 This is just a shell command that says, open flexsimrunner.exe with remotetest.fsm as the model to open, and pass an extra parameter called scenario with the value of "5". VBScript File The code in the vbscript file is as follows: Code: Dim WshShell,oExec Set WshShell = wscript.createobject("wscript.shell") Set oExec = WshShell.Exec("C:\Program Files\Flexsim4\program\flexsimrunner.exe ""C:\Program Files\Flexsim4\userprojects\remotetest.fsm"" /scenario 5") This vb script creates an object of the class wscript.shell. This is basically an object that gives you access to shell commands. Then if calls the Exec method of that object, which opens flexsimrunner.exe with the specified model, and the scenario parameter as 5. Model Implementation The model is a simple "post office" model, but it also has an On Model Open trigger implemented, as follows: Code:
int scenario = stringtonum(commandlineparam("scenario")); if(scenario > 0) msg("Model Scenario",numtostring(scenario, 0,0)); Passing Command-line Parameters You may pass any and as many command line parameters as you want, but they must follow a certain format. The parameter should be preceded either by the windows-standard forward slash: /, or the unix-standard hyphen: -. Then the command-line parameter's name should be a sequence of alpha-numeric characters followed by a space. Then the value of the parameter should be again a series of alpha-numeric characters. For example: Good: /scenario firstscenario Good: -scenario 6 Bad: /the scenario 5 Running the Model To automatically get the model running on startup, add the following lines of code to the On Model Open trigger (again, you'll probably want to first check that the model is being opened through a batch/vb script so that it doesn't run when you open the model from the open dialog). Code:
resetmodel(1);// reset the model runspeed(100000);// set the model runspeed to a sufficiently high speed go();// start the model running If you are using a command-line control method to retrieve information back from Flexsim, for example by writing VBA script in excel, the retrieval of information back from Flexsim is somewhat limited. The best way of doing this is to write to a file (or an excel spreadsheet) once a scenario/experiment has finished, then exit out of Flexsim with the cmdexit() command. The following VBA code defines the Windows API functions needed and implements them in order to execute a command line command from Excel and have Excel wait until the command finishes. The ShellAndWait sub takes in a command line command and freezes Excel until the command finishes. The OpenFileAndWait sub is an example for running the ShellAndWait sub. You would pass the command-line execution parameter into this sub: "C:\Program Files\Flexsim4\program\flexsimrunner.exe ""C:\Program Files\Flexsim4\userprojects\remotetest.fsm"" /scenario 5" Controlling Flexsim using COM You can also control Flexsim using Microsoft's COM specification. COM (Component Object Model) allows you to create an instance of a Flexsim application from other applications, and treat the application like a regular object in whatever language you're using, i.e. VB, C++, javascript, C#, etc., calling methods on the object like you would with any other object. Also, COM allows for better communication back and forth between the two applications, so that the client application can directly retrieve information about a simulation run, etc., instead of having to write to a file from Flexsim's side, and then read it from the client application's side. A 'beta' version of a Flexsim COM object has been created and is available in the downloads section, here. TO-DO Add information about controlling Flexsim through sockets TO-DO Add information about using the ticker mechanism? Last edited by Anthony Johnson; 01-02-2009 at 12:36 PM. |
#2
|
||||
|
||||
Looks like the On Model Open trigger should do exactly described in this thread:
http://www.flexsim.com/community/for...=On+Model+Open However, when I added a go() command into the On Model Open, the model does not run either if I open it from Flexsim or using the bat file. The code that I added is: Code:
int scenario = stringtonum(commandlineparam("scenario")); if(scenario > 0) msg("Model Scenario",numtostring(scenario, 0,0)); go();
__________________
Best, Alan |
#3
|
||||
|
||||
Never mind. I found the solution. I need to call a resetmodel() command before the go() command. So the code would be something like this:
Code:
int scenario = stringtonum(commandlineparam("scenario")); if(scenario > 0){ msg("Info", "Model start from external program"); msg("Model Scenario",numtostring(scenario, 0,0)); } else msg("Info", "Model start from Flexsim"); resetmodel(1); go();
__________________
Best, Alan |
Thread | Thread Starter | Forum | Replies | Last Post |
control Flexsim remotely | Martin Saler | Q&A | 1 | 12-14-2007 04:35 AM |
Controlling the camera view to be focused on a moving object in your model. | Regan Blackett | Tips and Tricks | 0 | 08-03-2007 10:12 AM |