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
已解决! 转到解答。
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:
...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
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
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:
Steps to control two or more FreeMASTER instances:
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
Thank you Michal for your response. I wrote these lines :
[]
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
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
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
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).
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
Hello Michal,
Thank you for your help. It opens two new FreeMaster files momentarily and then gives this error.
on this line:
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.
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
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.
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
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
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.
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.
Regards,
Michal
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.
Thanks,
Michal
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
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:
Thanks,
Michal
It just runs the two instances and cannot access them and gives the error in the figure below.
The examples you mentioned tries to access one FreeMaster only not two. Accessing one FreeMaster had worked with me before.
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