Local serial port / remote viewing

0
0

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

  • You must to post comments
0
0

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

 

  • Darren
    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
  • Nic Adams
    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.
  • Darren
    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
  • You must to post comments
0
0

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

  • edmond girardi
    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.
  • You must to post comments
0
0

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?

 

  • Tiago (ITG)
    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..
  • Tiago (ITG)
    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.
  • Darren
    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
  • Matthew Ferry
    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.
  • Darren
    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!
0
0

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

 

  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.