Skip to content

Local serial port / remote viewing #1512

Closed

Description

@itg-assistant

Darren asked 2018-04-26 09:52:45 UTC

Hi all,

I've built a simple web app. which opens a local serial port on the server PC & communicates with a device which is connected to the serial port.

The serial port is opened during the Window1_Load() event and then a BackGroundWorker is started.
The device data is collected within the BackGroundWorker continuous loop and shown in a label every second or so.

All good and works when I run it within Visual Studio.

Now, I can open this app. in a remote browser (another PC), and this seems to continue to work but I'm confused as to why it does.
I would expect that every new browser connection I make would run a new Window1_Load() event where an attempt to open the same port would fail (because port is already being used).
Instead, the remote browser seems to show the device data OK.

I'm completely confused and obviously not understanding the basics of what's going on here.

What I want to happen is that the serial communications be performed by a "master" process which is only called once, and all client connections are simply a viewer of what's going on in the master process.

Can someone please explain to me how I go about achieving this?

Something else is strange. Despite stopping the Visual Studio project, the remote client continues to run!? whereas I would expect it to stop once the project has stopped!?

Thank you

Regards,
Darren

Activity

  1. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Nic Adams answered 2018-04-26 11:54:29 UTC

    Darren,

    I have a similar app that uses a single tcp connection to talk to monitoring probes. The way I constructed this in WiseJ was to create the tcp connection class as a static in the window_load event - that way there is only ever one connection in the scope of my application (except when the app pool recycles so you may end up with two copies so you will need some code to deal with failures and reconnections).

    My tcp class raises events which my windows subscribe too in order to see the probe responses.

    Your app will continue to run after stopping debug in Visual Studio because IISExpress continues to run - if you look in the apptray you can see the icon. Stop IISExpress and your app will stop.

    HTH

    Nic

  2. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Darren answered 2018-04-26 15:12:36 UTC

    Hi Nic,
    I don’t know what you mean by “create a static class”. Is this a C# term? I’m using VB.
    What I need to do is only invoke the serial comms. routines if they have not already been invoked. A GLOBAL flag somewhere where all sessions can see it – but I don’t know where/how!?

    Thanks

  3. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Nic Adams answered 2018-04-26 16:01:46 UTC

    Hi Darren, you guessed right – I’m a c# developer. I did a quick google and MSDN seems to be suggesting that “shared” is the VB equivalent: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/shared – hopefully someone who knows both will be able to confirm.

  4. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Darren answered 2018-04-26 18:10:37 UTC

    Hi,

    I'm being stupid... AGAIN. Making silly mistakes that only a hardened desktop developer would make.

    The local serial comms. must only be performed by ONE (the first) client connection. Each subsequent connection must not try to perform the same comms., but should be able to read the results of the comms. being performed by the first connection.

    So, "somehow" I need to:

    1. make the first connection aware that it is the first connection (the master, if you like), & perform the comms.

    2. make each subsequent connection aware it is not the first (master), therefore don't perform the comms but rather get the data from "somewhere else".

    With the above in mind, what would be a suitable method for the master to store the data for the other sessions? A text file? (Not a database at this stage)

    Thanks

    Darren

  5. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Darren answered 2018-04-27 06:19:32 UTC

    When a new client connection is made, I understand that a new instance of the web app is created - and all variables within it are new & initialized within in the instance.

    I don't get how shared variables (within a vb module for example) aren't also initialized For each app instance/session?

    What makes shared variables different to non-shared in this context?

  6. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Luca answered 2018-04-27 16:33:05 UTC

    Shared are static, they are initialized when the type is loaded (once per AppDomain) and shared among instances withing the same AppDomain. I sent a link to the .NET documentation regarding VB.NET Shared variables in a reply to the other post.

  7. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Tiago (ITG) answered 2018-04-27 17:22:38 UTC

    Hi all,

    Just a small note on the side, so we can understand each other.

    This is not obvious but "static" in C# translates to different things in VB.NET, depending on what we are referring to, a class or a variable (field/property).

    In C#
    public static class Customer
    in VB.NET is
    Public NotInheritable Class Customer

    In C#
    private static int nextId = 0;
    in VB.NET is
    Private Shared nextId As Integer = 0

    So putting it all together, in C#
    public static class Customer
    {
    **    private static int nextId = 0;**
    }

    becames in VB.NET
    Public NotInheritable Class Customer
    **    Private Shared nextId As Integer = 0**
    End Class

  8. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Darren answered 2018-04-27 18:26:51 UTC

    Hi Luca,

    I understand how shared is used in traditional desktop apps, but there are mind-bending differences when it comes to web apps!

    I performed a simple experiment this afternoon in wisej to see how global (declared in vb module) variables behave & it seems that variables declared in a module maintain their value for every new client or session.
    Eh? Whatttt??? I wasn’t expecting that!

    I had a counter variable declared in a module, incrementing every second in a background loop.
    I then opened several browsers, both local & remote & this variable was the same value in all browsers.
    Mind… blown…

    So a vb module seems to be a place for global variables shared across all sessions.
    Please tell me this is the expected behaviour? Then I’ll be happy to accept it :-)

  9. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    edmond girardi answered 2018-04-29 22:51:40 UTC

    Make a “Shared” class in VB. You can call (and make) it’s init method to have it open the connection on the com port. Since it s a shared class you can reference it from anywhere in your application. Your first connection – set a session variable for that user and set something in the class to mark it as already assigned to the first user to prevent other connections from having certain access. All other users – check a flag in that shared class – and set their session variable to “Read” status. All users will access that same single instance of the class since it is Shared. You control who can do what via the session variable that was set.

  10. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Tiago (ITG) answered 2018-04-30 00:15:14 UTC

    VB “Shared” variables are what we call in c# “static” and are shared among all threads in the AppDomain. This means “Shared” variables have the same value in all Wisej session/instances. This is the expected behaviour..

  11. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Tiago (ITG) answered 2018-04-30 00:16:15 UTC

    We call it “static” in C# because the value is the same for all instances of the class. But sometimes you need a variable to have a value shared among all instances of the class, but different on each Wisej session. In that case, you should store your value on a Session variable. See https://wisej.com/docs/html/Session.htm for details.

  12. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Darren answered 2018-04-30 08:31:30 UTC

    Hi Tiago,
    Thanks, but my issue is not with the terminology or differences between VB & C#, it is with the scope of variables within a web app.
    Web app variable scope seems to be different to traditional desktop apps.
    For example, a traditional desktop app. may have a variable declared in a module. This variable is global to the app. instance ONLY. If you run another instance of the app., then that will have it own instance of the variable.
    With web apps., this is not so – it seems.
    A global variable (in a VB module) is global not just to the session, but ALL sessions – this is what was throwing me. I expected each session to have it’s own instance of the global variables.
    Am I correct?
    Thanks

  13. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Matthew Ferry answered 2018-05-03 04:34:59 UTC

    Hi Darren – I like to look at it this way. Think of the web app running in IIS as a single instance of a desktop app – but magically multiple people can use that same instance, i.e. when a new user starts up the app – they are using the same instance you are already using. I’m a hardened desktop developer too – and only a squishy web developer, and it took me a while to get my head around that too.

  14. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Darren answered 2018-05-04 19:05:06 UTC

    Hi Mathew,
    Your post has confused me even further, lol.
    The trouble is I’m not getting enough time to experiment at the moment. Let’s face it – best way to learn is to experiment.
    What I found last week was that variables declared in a vb module are shared between all clients/sessions.
    This is completely alien to the desktop developer &, as far as I know isn’t actually documented?
    It would great if there was a document which details the scope of variables in a wisej app.?
    What happens if multiple clients try to change the variable at the same time?
    Ugghhh… head spinning!

  15. itg-assistant commented on Jun 13, 2026

    @itg-assistant
    Author

    Darren answered 2018-06-13 18:30:59 UTC

    Hi,
    It’s been a while but I’m back to this issue again.
    I’ve seen how ashx handlers can receive data from client POSTs, but being able to send a message back to each client (i.e. full duplex comms. between web app & desktop clients) is way over my head.
    I’m not sure if it’s even possible, especially without an extra websocket – and even then websockets don’t exist in Windows 7, which we would have to support.

    Nic, are you still around? I’d love to be able to chat privately about your TCP solution.
    I assume you’re using a different port number for this and that there’s some configuring to do between clients & server (e.g. IP’s and ports).
    I was hoping that I could avoid all the configuring by having all comms. embedded in the web app.

    Wisej offers a simple chat example. I was wondering whether I could adapt that, but then the desktop apps. will need to replicate the comms… somehow.

    Nightmare for desktop developers! lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GeneralQuestions, how-tos and everything else.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions