Jump to content

VB.net client template from trevor


EASY

Empfohlene Beiträge

Hi trevor,

as i could see you used my template for "MBS_Controller"... but you changed something for sending commands and getting events.
When i look at your VB project i think it is not the first time you are writeing programms.
I do not understand exactly what you have done but I am curious and i like learning how to do in different ways...
... so i want to ask you if you can make a template for testing and understanding "your way" for sending commands and getting events...

EASY

Link zu diesem Kommentar
Auf anderen Seiten teilen

2 hours ago, EASY said:

Hi trevor,

as i could see you used my template for "MBS_Controller"... but you changed something for sending commands and getting events.
When i look at your VB project i think it is not the first time you are writeing programms.
I do not understand exactly what you have done but I am curious and i like learning how to do in different ways...
... so i want to ask you if you can make a template for testing and understanding "your way" for sending commands and getting events...

EASY

Hi Easy,

Hmm... I could take a little time to do that sure.

Yes, you are right, I started out with your template. However, I found it a little slow for doing real time simulation control, so I fine tuned it a bit to improve the performance and add a few new features like prioritized command queues and asynchronous sending. Latest edition also greatly improves block command handling.

Regards
Trevor

(BTW: Been coding for 40 years.. so ya... it's not my first time LOL)

 

Link zu diesem Kommentar
Auf anderen Seiten teilen

Yes, that's why I tried to make the class self contained with a simple interface outside of the main form. ;)

And don't knock yourself down like that, I'm always learning new stuff too, even after 40 years.

In fact I picked up a couple of new new tricks writing that class.

If you need more help feel free to PM me or even Skype if u have it.

Link zu diesem Kommentar
Auf anderen Seiten teilen

  • 2 Wochen später...

Command thread philosophy changed in light of the fact that MBS effecctively only handles one command or command group per frame....
Who knew ?????

Command thread now uses a polling system.
It continually sends a group command (10,11) at MBS and waits for the 11 response to repeat.
Since we can figure MBS does one command / group per frame, I'm going to call that a Frame-Command.
Commands requested from whatever is using the class are queued within the class.
The commander thread dequeues those commands, if there are any, and inserts them into the next frame command.

The class user STILL has the option to create a Command-Group for special functionality,
eg. looking up the location of N locomotives.
When sent to the class, command groups are queued in a separate queue.
If the commander thread detects a group in that queue, it will be sent it INSTEAD of the frame command.

The response thread has been removed and integrated into the command_thread. 

DUE TO THE SYNCHRONISM ARRISING FROM THE MBS LIMITATION,
COMMAND PRIORITY IS NO LONGER A RELEVANT FEATURE AND HAS BEEN REMOVED.

Because sending individual group begin and end commands, 10 and 11, would mess things up, these commands are now blocked.
Use the Command_Group feature instead!

Added event delegate Frame_Clock_Handler in case you need to use the frame clock, or something related to it..., to synchronize something.

MBS_TEMPLATE_20161116.zip

Bearbeitet von trevor
Link zu diesem Kommentar
Auf anderen Seiten teilen

Hi trevor,

Zitat
Am 16.11.2016 um 18:00 schrieb trevor:

Command thread philosophy changed in light of the fact that MBS effecctively only handles one command or command group per frame....
Who knew ?????

... i knew (for one command but i think not for command group(?))

great work! ... got some questions:

When i need the response of a command to decide my next steps i have to send "MBS.Send_Command_And_Wait(CMD)" and not "MBS.Send_Command(CMD)"?

I have a problem with my "German" PC:
        CMD = New cls_MBS.Command(cls_MBS.Command_ID.Get_Object_XYZ, "Quader")
        MBS.Send_Command_And_Wait(CMD)
        Dim x, y, z As Single
        x = CSng(CMD.Results(0))
        y = CSng(CMD.Results(1))
        z = CSng(CMD.Results(2))
        MsgBox(CMD.Results(0) & vbCrLf & x)

... if CMD.Results(0)="10.479" -> x=10479

Can you make an example to use " Frame_Clock_Handler " i.e something moves 1mm/frame?

EASY

Link zu diesem Kommentar
Auf anderen Seiten teilen

4 hours ago, EASY said:

Hi trevor,

... i knew (for one command but i think not for command group(?))

great work! ... got some questions:

When i need the response of a command to decide my next steps i have to send "MBS.Send_Command_And_Wait(CMD)" and not "MBS.Send_Command(CMD)"?

I have a problem with my "German" PC:
        CMD = New cls_MBS.Command(cls_MBS.Command_ID.Get_Object_XYZ, "Quader")
        MBS.Send_Command_And_Wait(CMD)
        Dim x, y, z As Single
        x = CSng(CMD.Results(0))
        y = CSng(CMD.Results(1))
        z = CSng(CMD.Results(2))
        MsgBox(CMD.Results(0) & vbCrLf & x)

... if CMD.Results(0)="10.479" -> x=10479

Can you make an example to use " Frame_Clock_Handler " i.e something moves 1mm/frame?

EASY

99.9% of the time one should use "Send_Command_And_Wait" or "Send_Group_And_Wait" if you need to use the returned data.
You can think of those as Function calls.

"Send_Command" or "Send_Group" you can think of as Subs.

 

An alternative method you can use the commands "Command_Executed" event, or the groups "Group_Executed" events. 
You would have to declare a variable like

Private Withevents Fetch_XYZ as cls_MBS.Command

Then, assign the variable to the appropriate command when you need to use it, and trap the event as an event handler.
However, that gets messy pretty quickly, and I am not sure when you would ever need do it that way.

-------------- 

Your issue with the CSng method is likely because your Windows regional settings are set to use a comma for the decimation character.
If so, it appears MBS does not send the data regionalized that way. You need to talk to NEO about that one.

For now you can use 

Dim DecimalSymbol as string = Strings.mid(Cstr(1.1), 2, 1) ' Get whatever windows is currently using as a period symbol

CSng(Replace(CMD.Results(0), ".", DecimalSymbol))

as a quick and dirty fix. (though if thousands separators appear it gets messier)
Actually I'd make my own global...     
             Function StringToDouble(TextValue as string) as Double
that would handle all that.


Thanks for finding that though... I need to take that into account in my code too I guess.

-----------------

Without generating code, it might be useful to use the Frame_Clock_Handler to rotate a turntable, or something, smoothly in time to the simulation.
However, don't confuse FPS with mm/s they are only loosely related.
Low FPS just means things JUMP in bigger steps.

Regards
T
 

 

Bearbeitet von trevor
Link zu diesem Kommentar
Auf anderen Seiten teilen

Hi trevor,

to avoid the "," problem i have this code in my plugins in the "FormLoad" event:

        Dim cAktiveRegion As System.Globalization.CultureInfo
        cAktiveRegion = CType(System.Globalization.CultureInfo.CurrentCulture.Clone(), Globalization.CultureInfo)
        cAktiveRegion.NumberFormat.NumberDecimalSeparator = "."
        Threading.Thread.CurrentThread.CurrentCulture = cAktiveRegion

.... i found this as code snipet but i do not know if it is the best way... it works and never causes problems... but i do not know if it works on an "English" PC...
... (if i put it in your template it also works)...

... an example for " Frame_Clock_Handler " still would be nice:/

EASY

Link zu diesem Kommentar
Auf anderen Seiten teilen

6 hours ago, EASY said:

Hi trevor,

to avoid the "," problem i have this code in my plugins in the "FormLoad" event:

        Dim cAktiveRegion As System.Globalization.CultureInfo
        cAktiveRegion = CType(System.Globalization.CultureInfo.CurrentCulture.Clone(), Globalization.CultureInfo)
        cAktiveRegion.NumberFormat.NumberDecimalSeparator = "."
        Threading.Thread.CurrentThread.CurrentCulture = cAktiveRegion

.... i found this as code snipet but i do not know if it is the best way... it works and never causes problems... but i do not know if it works on an "English" PC...
... (if i put it in your template it also works)...

... an example for " Frame_Clock_Handler " still would be nice:/

EASY

Cute... thanks, that's a new one to me.

Putting it in the MBS class would be the wrong place since that is not really a thread.
The threads within there are all string based so I don't see those changing anything.

btw: Sending numbers and dates as strings is ALWAYS a great big pain in the nether regions.

The other issue is, although you need to communicate with MBS in that format, you may not want to communicate with YOUR user in that manner. So use that method with care.

Also, be aware, that the setting ONLY applies to the selected thread... Unfortunately, culture is not propagated to any threads spawned by the current thread. That makes it complicated if you do not know which particular thread is calling or adjusting which variable. As such I prefer the custom converter methods.


 

 



 

Bearbeitet von trevor
Link zu diesem Kommentar
Auf anderen Seiten teilen

A better method may be to use.

 

Dim D as Double = Double.Parse(MBS_STRING, New CultureInfo("en-US"))
When receiving

and
Dim D as double = 1/3
DIM DSstring as string = D.ToString(
"F", New CultureInfo("en-US"))

or

Dim I as integer = 1234567
DIM IString as string = I.ToString("D", New CultureInfo("en-US"))
when sending

as required.

That's what I am currently changing the template to do since there are three or four places I actually do conversions.

 

Link zu diesem Kommentar
Auf anderen Seiten teilen

Here is the new version with culture handling corrected.
You still need to do your own conversions when reading and sending strings at it though.

I also updated the MBS project file to add a turntable that should rotate smoothly when connected since it uses the Frame_Clock_Handler.
(Well... as smoothly as MBS gets... LOL)

MBS_TEMPLATE_20161118.zip

Bearbeitet von trevor
Link zu diesem Kommentar
Auf anderen Seiten teilen

Erstelle ein Benutzerkonto oder melde dich an, um zu kommentieren

Du musst ein Benutzerkonto besitzen, um einen Kommentar verfassen zu können

Benutzerkonto erstellen

Neues Benutzerkonto für unsere Community erstellen.

Neues Benutzerkonto erstellen

Anmelden

Du hast bereits ein Benutzerkonto? Melde dich hier an.

Jetzt anmelden
×
×
  • Neu erstellen...