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

590 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

15 Replies

520 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

476 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

474 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

460 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

 

418 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

321 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

311 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

306 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

288 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

269 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

216 Views
Ahmed_Abd_El-Hafez
Contributor III

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

0 Kudos

196 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

131 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

69 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

18 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