Controlling two FreeMaster instances from one MATLAB script

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Controlling two FreeMaster instances from one MATLAB script

Jump to solution
4,184 Views
Ahmed_Abd_El-Hafez
Contributor III

Hello NXP team,

I want to make a MATLAB script file that can access, monitor and control two FreeMaster instances. I read the section talking about this in the user guide but still have problems and the example attached was for one FreeMaster and didn't work with me for the two FreeMaster instances. The .m file that I am trying to build , is till now only opens the two files but can't write or read from them. It gives this error:

Dot indexing is not supported for variables of this type.

Error in SingleScriptControlling2FM (line 15)
bSucc = pcm1.ReadVariable('SpeedActRpm');

Does any one have a script for this purpose or can help with this error?

Thank you in advance.

Ahmed

1 Solution
2,860 Views
MichalH
NXP Apps Support
NXP Apps Support

Hello Ahmed, 

I confirm your script (the mmult_test() function) works as expected on my side. I tried on two Windows 11 systems. On a corporate PC with strict security rules, the Excel will not let the script to launch the FreeMASTER processes, so I had to run them manually first:

pcmaster /sharex TRQ
pcmaster /sharex SPD

Then the script was able to get access to running instances. Followed by a message:

MichalH_0-1718367771180.png

...which is obvious as I have an empty project in the TRQ instance.

 

In the 2nd experiment using my home PC, the Excel was able to launch and control the instances. I'm really not sure what can be blocking the access on your side. Can you try the same on a different PC?

Regards,
Michal

 

View solution in original post

0 Kudos
Reply
24 Replies
3,630 Views
Ahmed_Abd_El-Hafez
Contributor III

Hello,

This Problem highlighted in red color was solved by initializing pcm as an object sothat I can use dot indexing with it. But now, the script can read and write in one FreeMaster not two. How can I use mult = actxserver('MCB.MULT');  to make it listen to two different FreeMaster instances with two boards on two COMs? does it need the ProgID as I read? if so, how can I obtain the ProgID of each board? Please, can any one help with this?

Ahmed 

 
0 Kudos
Reply
3,586 Views
MichalH
NXP Apps Support
NXP Apps Support

Hello Ahmed,

the MCB.MULT is an identifier of an object which helps you to manage multiple running instances of FreeMASTER. It only has one method GetAppObject, but this is quite versatile. Read User Manual section 6.9 for more information.

With the GetAppObject you can:

  • Get an interface object as an ActiveX interface to a "named" running instance of FreeMASTER
  • Run a named instance if not yet running.
  • Run a named instance and load a project.

Steps to control two or more FreeMASTER instances:

  1. Run instance A manually by starting pcmaster.exe process. Give it a name using command-line parameter "/sharex A". When FreeMASTER starts, configure board connection, open project etc.
  2. Similarly, run the instance named "B" and connect to a second board.
  3. In matlab, call
    • pcmA = mult.GetAppObject("A")
    • pcmB = mult.GetAppObject("B")
  4. Now you can use each object to read variables and do other control specific to each board.

Alternatively, you can save the projects to projectA.pcmx and projectB.pcmx. Make sure to check the "Save settings to project file" so the communication settings is saved. Then you can pass the name of each project as a 3rd argument of GetAppObject and it will take care about running the FreeMASTER and loading the project automatically along with communication settings.

Yet another alternative is to start instances A and B from Matlab as indicated above (without opening a project) and then orchestrate everything from script using StartComm and OpenProject methods executed on pcmA and pcmB objects.

Regards,
Michal

3,584 Views
Ahmed_Abd_El-Hafez
Contributor III

Thank you Michal for your response. I wrote these lines :

mult = actxserver('MCB.MULT');
pcm1=mult.GetAppObject('SpdControl')
pcm2=mult.GetAppObject('TrqControl')
pcm1.WriteVariable('SPEED_CMD',700);
 
but it gives me this error: 

[]

Dot indexing is not supported for variables of this type.

Error in SingleScriptControlling2FM (line 9)
pcm1.WriteVariable('SPEED_CMD',700);

is there anything that I have missed?

Ahmed

3,570 Views
MichalH
NXP Apps Support
NXP Apps Support

Hello,

this must be some shortcoming of Matlab. It does not recognize the VARIANT value returned by GetAppObject is actually an IDispatch ActiveX interface. 

In other scripting languages (e.g. VBA, VBScript, JScript) this approach works fine.

Let me investigate more. According to this page, Matlab shall handle it correctly.  I do not have a solution right now.

Regards,
Michal

 

3,528 Views
Ahmed_Abd_El-Hafez
Contributor III

Ok, I am waiting for you to investigate this more. Thanks a lot for your effort.

I am trying to make it by Excel using Visual Basic and I started by this code :

Sub mmult_test()
Dim a As McbPcm
Dim b As McbPcm
Dim mult As McbMult

Set mult = New McbMult
Set a = mult.GetAppObject("A")
Set b = mult.GetAppObject("B")

Dim bSucc As Boolean

'ReadVariable uses FreeMASTER variable object from current project. Use
'ReadUIntVariable to access the memory directly using a symbol name.
Set bSucc = a.ReadVariable("SPEED_CMD", vValue, tValue, bsRetMsg)
If bSucc Then
sht.Range("B1").Value = vValue
End If

If b.ReadVariable("TrqCtrlSwitch", "B3") Then
MsgBox ("Variable from instance B " + t2)
Else
MsgBox ("Error reading from instance B: " + m2)
End If
End Sub

That I saw in the question here:https://community.nxp.com/t5/FreeMASTER/Multiple-instances-using-ActiveX-in-VB/td-p/1340033

but I got a compiler error at this line: Set bSucc = a.ReadVariable("SPEED_CMD", vValue, tValue, bsRetMsg) the error says: "Object Required"  and when I removed "Set" it gives me : "Object variable or with block variable not set" I think it can't see the object returned by GetAppObject also. If you have tested it before to control two FreeMaster instances using Excell please help me in this.

Regards,

Ahmed

 

0 Kudos
Reply
3,431 Views
MichalH
NXP Apps Support
NXP Apps Support

Hello Ahmed,

the script below works in my test Excel file. Make sure you have the objects referenced in VBA. Go to Tools/References menu and check both FreeMASTER and also mmaster object (you may need to browse for mmaster.dll in the FreeMASTER installation).

 

MichalH_2-1714978633536.png

 

 

Dim mult As McbMult

Sub start_A()
    If mult Is Nothing Then
        Set mult = New McbMult
    End If
        
    Set a = mult.GetAppObject("A", 1)
End Sub

Sub start_B()
    If mult Is Nothing Then
        Set mult = New McbMult
    End If
    
    Set b = mult.GetAppObject("B", 1)
End Sub

Sub mmult_test()

    If mult Is Nothing Then
        Set mult = New McbMult
    End If
    
    Dim a As McbPcm
    Dim b As McbPcm
    
    Set a = mult.GetAppObject("A", 1)
    Set b = mult.GetAppObject("B", 1)
    
    If a.ReadVariable("var16", v1, t1, m1) Then
        MsgBox ("Variable from instance A " + t1)
    Else
        MsgBox ("Error reading from instance A: " + m1)
    End If
    
    If b.ReadVariable("var16", v2, t2, m2) Then
        MsgBox ("Variable from instance B " + t2)
    Else
        MsgBox ("Error reading from instance B: " + m2)
    End If

End Sub

 

0 Kudos
Reply
3,421 Views
Ahmed_Abd_El-Hafez
Contributor III

Hello Michal,

Thank you for your help. It opens two new FreeMaster files momentarily and then gives this error.

err1.PNG

on this line:

errr1.PNG

 

 

 

I replaced "A" & "B" by the names of my files written like "SpdControl.pmp" and tried also to write the total pathes of files since I want to open these two files not to open two new instances. I had the objects referenced in VBA.

errrr1.PNG

0 Kudos
Reply
3,416 Views
MichalH
NXP Apps Support
NXP Apps Support

Can you try exactly my code? Just to compare the behaviors. Note that there may be difference in 32bit and 64bit version of Excel. I'm testing 64bit version.

You can also put breakpoint to examine a and b variables if they are indeed set to "Nothing". 

Just to be fully sure the installation is correct, please run "register.bat" as administrator in the FreeMASTER installation folder (c:\NXP\FreeMASTER 3.2\FreeMASTER\register.bat)

Thanks,
Michal

 

0 Kudos
Reply
3,398 Views
Ahmed_Abd_El-Hafez
Contributor III

Same error in the same line when I used exactly your code without any change.

Yes a & b are set to nothing.

When I run register.bat it gives black window momentarily and then disappears. I don't know what this means.

The Excel I use is 32 bit. It seems that this causes the difference. 

 

0 Kudos
Reply
3,379 Views
MichalH
NXP Apps Support
NXP Apps Support

Hello Ahmed, 

I will retry with 32bit Excel, this will need some time. 

Please try to do one more experiment and start the applications manually. Locate the pcmaster.exe file in the installation and run it from command line with an argument /sharex "A"  and the other with /sharex "B". Then try the script with "A" and "B" names.

The question is if your Excel would locate the two running instances or if it will start another two new instances.

Thank you for you cooperation,
Michal

 

0 Kudos
Reply
3,326 Views
Ahmed_Abd_El-Hafez
Contributor III

ok I will try this and wait for your try also.Thank you

0 Kudos
Reply
3,306 Views
MichalH
NXP Apps Support
NXP Apps Support

Hello, 

I have tested the Excel script on a PC with fresh Windows 11 and the latest Excel 32bit. All seem to work properly. Is it possible for you to test on another PC?

In the meanwhile, I have noticed that on some of our corporate PCs, the GetAppObject fails to start the FreeMASTER process due to a security policy. The failed start can be seen in the "Protection History" log in Windows Security panel (picture attached).

Anyway, if FreeMASTER is started manually with the proper "/sharex" option, then the VBA scripts runs normally - so this is must be a different issue from yours. 

 

So far, no answer to your issue. Let's continue investigating. Please finish your experiment with manual start of the shared instances and also please try on a different PC.

Thank you,
Michal

 

MichalH_0-1715256758369.png

 

 

0 Kudos
Reply
3,241 Views
Ahmed_Abd_El-Hafez
Contributor III

Ok I will try on another PC.

I didn't understand this sentence: "Anyway, if FreeMASTER is started manually with the proper "/sharex" option, then the VBA scripts runs normally - so this is must be a different issue from yours." 

and in this : Please try to do one more experiment and start the applications manually. Locate the pcmaster.exe file in the installation and run it from command line with an argument /sharex "A"  and the other with /sharex "B". Then try the script with "A" and "B" names.

Do you mean to simply rename the instances by A& B or what ? I feel that I misunderstand something. How to use this  /sharex ?

Thank you for your cooperation.

 

0 Kudos
Reply
3,179 Views
MichalH
NXP Apps Support
NXP Apps Support

The /sharex is a command-line option which you can use as a parameter when running the FreeMASTER process (pcmaster.exe). You need to run it manually from command line prompt, or you can create a launcher icon with the parameter.

Steps to run it manually.

  1. Open Windows Explorer and browse to "c:\NXP\FreeMASTER 3.2" 
  2. Right click the FreeMASTER folder and select "Open in Terminal"
  3. Type .\pcmaster.exe /sharex NAME
  4. The NAME is your name which later use in the GetAppObject() call 

 

MichalH_2-1715596200192.png

 

MichalH_1-1715595731028.png

 

Regards,
Michal

0 Kudos
Reply
3,128 Views
Ahmed_Abd_El-Hafez
Contributor III

When I write this command as shown, it opens a new FreeMaster project for moments and close it away. It doesn't open the already created project "SpdControl.pmp" why?

Ahmed_Abd_ElHafez_0-1715783886736.png

 

0 Kudos
Reply
3,102 Views
MichalH
NXP Apps Support
NXP Apps Support

Hello Ahmed,

the first parameter after /sharex option is NOT the name of the pmp project file, it is a name under which the running instance will be identified for mult.GetAppObject. If you want to open a project too, you need to specify it after the name.

Examples:

pcmaster.exe C:\path\SpdControl.pmp    // will just open the project file
pcmaster.exe /sharex SPD       // will run the FreeMASTER named SPD,
// so you can attach to it using GetAppObject("SPD")
pcmaster.exe /sharex SPD C:\path\SpdControl.pmp // will do both

So, in all your examples you passed just one argument after /sharex option, which means it should have started the FreeMASTER without opening any project. You say that the application closes immediately after started... this may be an indication of a problem with installation.

  • Can you make sure you are running version 3.2.3.2? (see in the Help/About dialog).
  • Can you try the same on a different PC?
  • Can you try to start just "pcmaster.exe" without any argument to see if it will run properly?

Thanks,
Michal

3,074 Views
Ahmed_Abd_El-Hafez
Contributor III

 

Thank you very much for your effort, Michal.

1-I installed the version 3.2.3.2 and the problem of getting away from the FreeMaster window was solved.

2-I applied the way of giving sharex name for a specific instance in a specific path like your example above and then pass this sharex name to the GetAppObject () method and this way works properly with MATLAB script. So the problem of MATLAB is solved now.

But when doing the same with Excell it can't locate the two running FreeMaster instances instead of this it opens two new instances why?

Regards 

Ahmed

0 Kudos
Reply
2,963 Views
MichalH
NXP Apps Support
NXP Apps Support

Hello Ahmed, 

good that you got it working from Matlab. Let's focus the Excel issue now. When it starts two new instances - does the VBA script access the instances then? I mean can the Excel VBA use the FreeMASTER objects to read&write variables etc?

If yes, then there will some simple issue with instance names. If it just runs the instances, but still cannot access them, then there might be some issue with access rights.

Experiments to try:

  • Run the pcmaster.exe manually and give it some trivial name such as A or B. Do not use any special characters etc. Then use the same name in the VBA script.
  • Run the Excel as administrator.
  • Try the simple Excel examples in located in "c:\NXP\FreeMASTER 3.2\FreeMASTER\examples\scripting\Visual Basic Application - Excel". Do they work?

Thanks,
Michal

 

0 Kudos
Reply
2,955 Views
Ahmed_Abd_El-Hafez
Contributor III

It just runs the two instances and cannot access them and gives the error in the figure below.

 

pic1.PNG

 

The examples you mentioned tries to access one FreeMaster only not two. Accessing one FreeMaster had worked with me before.

0 Kudos
Reply
2,945 Views
MichalH
NXP Apps Support
NXP Apps Support

Hmm, this seems the "mult" object living in Excel VBA has a problem to launch FreeMASTER with correct instance name as argument. I think you have a problem in the way you pass the instance name ("A" or "B") when you call "GetAppObject". You can upload the Excel file as an attachment so I could take a look at the script.

Anyway, if you run the instances manually from command-line with the name "A" (pcmaster.exe /sharex A) or with the name "B" (pcmaster.exe /sharex B). Then the Excel VBA should be able to locate it.

Regards,
Michal

0 Kudos
Reply