Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Text Files??

Hi Folks,
Could somebody Help?

I would like to pass info from a single app

To multiple apps

One program creates a text file while other programs can read
How do Know if the writing app is writing so the looking apps dont read until the writing app is done?

Thanks
PicBoy1
[322 byte] By [PicBoy1] at [2007-11-11 8:03:20]
# 1 Re: Text Files??
Well how about searching for this text file through a Timer. If its found them open it. After its been read, delete it. ;)

Or use DDE (Dynamic Data Exchange) to read the data from a TextBox in the other app.
Keithuk at 2007-11-11 17:26:00 >
# 2 Re: Text Files??
Thank for the reply

I Took a quick peek at the DDE Not bad but is it limited to one app @ a time?

What I have is a program that pulls info from PLC's VIA serial Gateway

Up to 200 COM ports the program works, I now need to push the running info to Front end GUI apps that just display info pcs/minute qty of product etc..
So Ithought write a text file with the single app. Open it with the other apps read it in display the info. by the way
This is also over a network
Kinda works had to use

Single app writes using the following

Open gFileName For Output Shared As #filehandle

Some code

Close #filehandle

So far this lets the reading app to open and read using a timer
Was crashing when I opened just normaly but the "shared" Seemed to help

Havent tried 2 apps reading or 3 could be up to 15 ~20 apps trying to read
the text file.

I was going to write something that looks to see if the singal app was writing I would just wait for it to be done but the data coming in from the plcs is fast Could be as low as 2 ms to 20 ms depending on comevent hits
Doesnt leave much time for reading apps to look in

Anyway Trail and Error

Just thought there might be a way apps could plug in apps and exchange data keeping in mind there could be 200 plcs pluging in data to singal app that a lot of data to pass to reading apps will DDE be up to the challenge I dont know!

Thanks
Keep the Ideas Coming
PicBoy1
PicBoy1 at 2007-11-11 17:27:02 >
# 3 Re: Text Files??
You can solve this by writing a file that holds true or false.
So before the applications start to read => check if true or false
If the application is still writing => false => don't read
If the app has done the writing => set it back to true so they can read

Jus an idea of course.

Benjamin
Benjamin at 2007-11-11 17:28:12 >
# 4 Re: Text Files??
Thanks Benjamin

I tried your Idea which I got to work after some tricky timming

App 1 Sets a false in busy.txt file the other apps open read it then query the busy.txt file every .5 second till we get a true
When App 1 is done writing flips it over to True and bang the other apps look in and read the data.

Problems that had to be over came.

1) The file that held the true/false, if app 1 was writing state and app2 would open for read app2 would crash!
Fix was look at the file to see if it was open if not then decide to read it

Any way all this finaly started to work with some handshaking going on but fell apart when I open 5 apps to read the file. worked for about 30 mins then 1 app froze locking the Busy.txt read file open which froze all the other app from looking in. Cant have that!
The above would probably work great with single front end app

So know Im going to try ADO and read A data base file that app 1 pushs to, still not sure seems to me I should be able to create a connection bettween apps some kind of pipeline that Apps can plug into like The ADO jet engine not sure how to do this?? Im heading down a dark path of hard learning which is ok! So I like to post my stumbles maybe they help others or humor them :) Or somebody shines a light on the problem

App1 has variables that need to be passed to multiple apps that plug in
and read there values?? This would be the goal

The First app directs data that comes in from Production lines
Folks on the production floor would like to veiw it. 2 to 15 apps could be viewing. I would like to pass the values the first app calculates to the other apps(GUIs) for display

The text file aproach sounded simple but fell apart when I started to lanch other apps.

So I might try the Database approach ??
So far a few bugs to work out

Picboy1
PicBoy1 at 2007-11-11 17:29:06 >
# 5 Re: Text Files??
Hi,

thanks for posting the outcome. Learned something :)
I didn't knew this could give problems, I think using a database should solve the issue. I'll check if I find something about passing variables to another app and come back to you.

Benjamin
Benjamin at 2007-11-11 17:30:12 >
# 6 Re: Text Files??
Hi,

I found an example that passes values to another application. The only problem here is that the two applications must be running on the same computer. (I don't think you can use it.)

The best solution is a database I think.

Here are the code files, not verry easy if you ask me. (2 VB6.0 projects)

Benjamin
Benjamin at 2007-11-11 17:31:11 >
# 7 Re: Text Files??
Another method would be to use different file names (or different extensions).

So while your writing the file, use a myapp.bsy extension. When your done writing it, rename it to myapp.txt (or whatever extensions you want to use).

Your GUI app monitors for the .txt version... when it sees it, it can take it and process it. If you want, the GUI can delete it when finished. This way your back-end app knows when it can write a new file.
edburdo at 2007-11-11 17:32:14 >
# 8 Re: Text Files??
using files as a way to pass data does not work well (as you saw...) when multiple applications access the file(s). There will always be timing access issues.
Basically what you want is a client-server application. The server (written as an Active exe) will be registered to run in the machine with the PLC connection. The clients will connect to this server and ask for data, and you can also provide events from the server to notify clients when new data is coming (but do not trust events, because they drop easily when the applications are busy) Of course the main class of the server must be declared MultiUse, so it can be shared by all applications. Well, this is really in a nutshell... if you want to go on this way (I strongly recommand it) I suggest you "Developing COM/ActiveX Components with VB6", by Daniel Appleman, SAMS press.
Marco
mstraf at 2007-11-11 17:33:07 >
# 9 Re: Text Files??
Some light in the tunnel

Thanks Mstraff

But could we crack the nut shell a bit though?

So I Understand the Client/Server Idea! Guess I need to Understand the active X a bit better and how it fits in bettween apps This definetly feels right

I work with PIC Chips hence the PicBoy and understand their small networks I2C etc. and how they talk to each other but I Have never ventured into the PC land this is a first.

Maybe some structure would be good to start with

First the role of the active x component?
Second how do apps use activeX (I will start to research their funtion! hear lots about them just never used them)

So far this has been Great feed bacK
Please keep it going
Picboy1
PicBoy1 at 2007-11-11 17:34:17 >
# 10 Re: Text Files??
one more crack in the nut...

start with this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconcreatingoleserver.asp
there are also excellent examples. Use the navigator to the left to browse for similar pages. At the beginning looks hard, but it is not bad at all.

Basically a ActiveX exe is an application that runs "on demand" (unless is a Windows service, but let's ignore it for the time being). The main body are public classes, that the clients connect to.

Start with a simple ActiveX exe, you will see that it creates one class by default. Change the name of the class and of the project as you wish. Add a simple method to the class. Build the project. Now change the project compatibility to binary (this is important) in the Component tab of the Project Properties dialog. Close the project.

Create a new standard exe, and click Project->References: you will see the name of the component you just built in the list, check it. Now you can "talk" to the server using its classes (if there are more than one). Write something like:

dim c as MyActiveX.MyClass
set c = new MyActiveX.MyClass
c.NameOfMyMethod

In the second line of code, the ActiveX application starts (if not started already) and a class MyClass (or whatever) is created. At this point you can ask that the class to do something for you (like start getting data), ask for its status (is data present) and get the data. Or use events. Multiple application can connect to the same class, if declared for MultiUse.

Finally, the server can run in another computer.

I realize that this is only the top of the iceberg... and that I put a lot of buzzwords without explaining them, but you can find all very well documented in the MSDN.

Let me know, we are here to help
Marco
mstraf at 2007-11-11 17:35:17 >
# 11 Re: Text Files??
Alright

Im going to look at this much deeper! after looking over the MSDN tut
this looks familiar. Im starting to see relation to comunication in Hardware(PIChips).

just a much more involved methode of comunicating and different syntax. I will work on creating a small active exe and get back in few days. Post my results or questions :WAVE:

This has been a very responsive forum and great feedback folks,

Please keep an Eye on this thread!

PicBoy1
PicBoy1 at 2007-11-11 17:36:16 >
# 12 Re: Text Files??
...just a much more involved methode of comunicating and different syntax...
yes, but do not remember that behind communication via COM there is still RPC... COM (and DCOM) is just a nice wrapper (between other things...)
Good luck!
Marco
mstraf at 2007-11-11 17:37:21 >
# 13 Re: Text Files??
Ok Hi again folks,

So, I re-thought my approach and had my Front-end apps make a connection with the access database move to the last row and read the lastest update to the databse.

This again works great when one app looks at the database even work great when 5 apps are looking at the database, however when one app closes it hung up the database connection and again everybody just hung up.

now I guess I should explian I bit .

Using the adoc I create a connection to *.mdb all is good
Do my work in the database when a timer fires every .5 second

frmProductionNumbers.datPrimaryRS.Recordset.MoveLast
FsTrimmer1_count = frmProductionNumbers.datPrimaryRS.Recordset.Fields("FSTrimmer")

Now as long as the App is running so is the connection
this doesnt feel right should I be closing the connection???

then reopening after each timer event?
or should I be closing only when I exit the app?
Rules that applyto ADO would be nice
Best practice

Now I have taken a reasonable look at ActiveX even created one as per Mstraf instruction, This is a nice way to allow other apps use methods/functions but is the activeX going to cuase the same problems when reading the database? if there are more then one looking in
Also now that I kinda know some thing about ActiveX and I say that with the minimal amount of knowledge! If a database existed on a network drive would the activeX reside there for apps to call or would one(you) package the activeX local to the front end apps!
My thinking is to have the activex on the network have only one! that logs the instances of who is asking it do work!
A guess this would only ever let one "thing" enter the database The activeX

now the next level

Once I create an activeX that creates a connection with the database, gets the data needed.

How does the activeX pass the data to the app that called it??

now in the MSDN tut they talk about coffee yep like lots of coffee! you can create event(s) that tell the app its done! this is good and understood

But the data how does it pass it to the app? do we revert back to a Text.file specific to the app that called the activeX ?? this way one app looks at one file!!

or when the coffee is done can the app pass a string or variable to the app
directly?

Sorry lots of ??

hope folks are still tuned in

Picboy1
PicBoy1 at 2007-11-11 17:38:17 >
# 14 Re: Text Files??
using an ActiveX you don't need a database at all...
The ActiveX will read the data via PLC, and store it locally (arrays, collections, even a recordset if you want), So you will have only one ActiveX running in the network.
This data will be "exposed" by some methods. Let's say you store the data as an array of double, the activeX will have methods like:

public event OnNewData(byval numData as long) '' fire when new data is coming
public property get NumData() as Long '' how many data I have
public property get MyData(byval index as long) as Double '' get the data by index

clients can start polling after they receive the OnNewData event.
Marco
mstraf at 2007-11-11 17:39:22 >
# 15 Re: Text Files??
Hi Mstraf,

You keep sending me on these Quest for Knowledge :)
A couple of things we need to do is store data from our equipment (PLC's Programble Logic Controlers) Our IS folk recomended Sql Data base
For know I push to Acess.

The data comes in using the MSOncomm event
I guess there is nothing stoping this from being in an activeX?? Maybe MSCOMM32.OCX would be a problem? Remember we are serial over IP so we get data using up to 256 virtual comports that are set up on the pcstation www.sena.com dont know if they have something that would work on a server? have to check or maybe they dont use MSCOMM32 at all rather their own


Oh this is clear now! :D becuase I have declard the activeX in my client the event will be passed to it ok. I will continue with ActiveX work
and try to get get data passing bettween apps
Thanks mstraff took a bit of stumbling but I have enough to play with now

Picboy1
PicBoy1 at 2007-11-11 17:40:17 >