ATTENTION

This 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

Go Back   FlexSim Community Forum > FlexSim Software > Wiki Articles
Downloads

Wiki Articles Instructional documents authored collectively by the Flexsim community

  #1  
Old 04-02-2008
Anthony Johnson's Avatar
Anthony Johnson Anthony Johnson is offline
Manager of Product Development
 
Join Date: Jul 2007
Posts: 440
Downloads: 86
Uploads: 4
Thanks: 171
Thanked 899 Times in 288 Posts
Rep Power: 735
Anthony Johnson has a reputation beyond reputeAnthony Johnson has a reputation beyond reputeAnthony Johnson has a reputation beyond reputeAnthony Johnson has a reputation beyond reputeAnthony Johnson has a reputation beyond reputeAnthony Johnson has a reputation beyond reputeAnthony Johnson has a reputation beyond reputeAnthony Johnson has a reputation beyond reputeAnthony Johnson has a reputation beyond reputeAnthony Johnson has a reputation beyond reputeAnthony Johnson has a reputation beyond repute
Default 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));
All this does is get the command-line parameter named scenario and converts it to a number (all command-line parameters are returned as strings, so "5" must be converted to 5). Then if the scenario is greater than 0, it prints a message. The reason it double checks that scenario is > 0 is so that the message won't come up if you just open the model through Flexsim's open dialog.

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
Retrieving Information Back from Flexsim
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"
Code:
'This code came from somewhere on the Internet that I can't find anymore. Much thanks to the original author, though
Private Declare Function OpenProcess Lib "kernel32" ( _
    ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" ( _
    ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" ( _
    ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" ( _
    ByVal hProcess As Long, _
    lpExitCode As Long) As Long
Private Const SYNCHRONIZE = &H100000
'// Note:SYNCHRONIZE Windows NT/2000
Private Const INFINITE = &HFFFF 'OR -1&
'// INFINITE, the function’s time-out interval never elapses.
Private Const STILL_ACTIVE = &H103
Public Function ShellAndWait(ByVal BatFile As String)
   '
   ' Shells a new process and waits for it to complete.
   ' Calling application is totally non-responsive while
   ' new process executes.
   '
   Dim PID As Long
   Dim hProcess As Long
   Dim nRet As Long
   '// Unlike other Functions Shell generates an error
   '// instead of returning a 0 so handling the error
   '// = Application NOT started.
   On Error Resume Next
   PID = Shell(BatFile, vbMinimizedNoFocus)
   If Err Then
       '// handle the error here and End
       MsgBox "Could NOT execute:= " & BatFile
       End
   End If
   On Error GoTo 0
   '// SYNCHRONIZE For Windows NT/2000:
   '// Enables using the process handle in any of the wait
   '// functions to wait for the process to terminate.
   '// obviously with NT you need access rights.
   hProcess = OpenProcess(SYNCHRONIZE, False, PID)
   '// Just set the dwMilliseconds to INFINITE to initiate a Loop
   nRet = WaitForSingleObject(hProcess, INFINITE)
   Do
       GetExitCodeProcess hProcess, nRet
       DoEvents
   Loop While nRet = STILL_ACTIVE
   CloseHandle hProcess
End Function
Sub OpenFileAndWait()
   Dim sApp As String
   '// Define the Application FullPath here
   sApp = "C:\windows\system32\calc.exe"
   '// Lets DoIt
   ShellAndWait sApp
   '// Tell me if Successful
   MsgBox "Finished running task!"
End Sub
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  
Old 04-03-2008
AlanZhang's Avatar
AlanZhang AlanZhang is offline
Flexsim Super Moderator
 
Join Date: Aug 2007
Location: CA
Posts: 289
Downloads: 64
Uploads: 0
Thanks: 88
Thanked 91 Times in 47 Posts
Rep Power: 225
AlanZhang is a jewel in the roughAlanZhang is a jewel in the roughAlanZhang is a jewel in the rough
Default

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();
Am I doing anything wrong?
__________________
Best,
Alan
  #3  
Old 04-03-2008
AlanZhang's Avatar
AlanZhang AlanZhang is offline
Flexsim Super Moderator
 
Join Date: Aug 2007
Location: CA
Posts: 289
Downloads: 64
Uploads: 0
Thanks: 88
Thanked 91 Times in 47 Posts
Rep Power: 225
AlanZhang is a jewel in the roughAlanZhang is a jewel in the roughAlanZhang is a jewel in the rough
Default

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


All times are GMT -6.
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.
Copyright 1993-2018 FlexSim Software Products, Inc.