dcsimg
Results 1 to 16 of 16

Thread: Reading writing from to the same txt file "rs232"

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    6

    Question Reading writing from to the same txt file "rs232"

    Hello,
    I have 2 VB6 Applications one that reads a value of counting from a device every 1s using serial com "rs232", and the 2nd one doesn't use serial comm.
    What I want to do is I want the 1st application to write the value of counting to a file txt and overwrite it every 1s, and the 2nd application to read that value from the Same file txt every 1s also and to show it, is that Possible with VB6?
    Thanks in advance.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,947

    Re: Reading writing from to the same txt file "rs232"

    Have you tried using the shared option in your open statements?

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    6

    Re: Reading writing from to the same txt file "rs232"

    Quote Originally Posted by DataMiser View Post
    Have you tried using the shared option in your open statements?
    Thanks for your reply,
    I don't know what u mean,I'm not that good with VB6, If you can explain more I would be grateful.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,509

    Re: Reading writing from to the same txt file "rs232"

    Rather than write a string to a text file, I would just write the number to a binary file at the first offset, and the second application could just read the number in from that offset. You don't have to specify the file is shared when using binary, and you are less likely to get a corrupted value if one file is writing and the other reading at the same time.

    As a quick test, I have the first program increment a number and put it to the binary file.
    Code:
    Option Explicit
    
    Dim counter As Long
    
    Private Sub Form_Load()
      Open "c:\c\tst.cnt" For Binary As #1
      Timer1.Interval = 1000
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      counter = counter + 1
      Put #1, 1, counter
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      Close 'close all open files
    End Sub
    I have the second application open the same binary file, and read the value and update the form's caption with the number read.
    Code:
    Option Explicit
    Dim counter As Long
    
    Private Sub Form_Load()
      Open "c:\c\tst.cnt" For Binary As #1
      Timer1.Interval = 1000
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      Get #1, 1, counter
      Me.Caption = counter
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      Close 'close all open files
    End Sub
    If you deal with multiple files, or are creating a routine for reuse, you would likely want to use FreeFile to get a file handle number, rather than hardcode #1 as I have. But, if your application is only going to open the one file, then it doesn't really matter.
    Last edited by passel; Oct 15th, 2019 at 09:20 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    269

    Re: Reading writing from to the same txt file "rs232"

    Here's an alternative approach that forgoes writing to a text file. This simple solution makes use of VB6's built-in support for DDE instead.



    1. Launch VB6 and open a new Standard EXE project.
    2. In the Properties window of the Form that was created by default, change its LinkMode property to "1 - Source".
    3. Add a Label and a Timer control to the Form. Change the Label's Name to "SourceLabel".
    4. Change the Timer control's Interval property to 1000 (1 second) via its Properties window.
    5. Click the Project menu and select Project1 Properties... and then change the Application Title in the Make tab to "SourceAppTitle" (or any other unique app title that you like).
    6. Paste this line in the Timer1's Timer event:
      Code:
      Option Explicit
      
      Private Sub Timer1_Timer()
          Label1.Caption = Time
      End Sub


    1. Launch another instance of VB6 and open a new Standard EXE project.
    2. Add a Label and a Timer control to the newly created Form.
    3. In Label1's Properties window, enter "SourceAppTitle|Form1" for the LinkTopic property and "SourceLabel" for the LinkItem property.
    4. Change the Timer control's Interval property to 1000 (1 second) via its Properties window.
    5. Paste the following code in the Code window:
      Code:
      Option Explicit
      
      Private Sub Timer1_Timer()                  'The Timer's job is to periodically attempt to initiate a DDE conversation
          On Error Resume Next
          Label1.LinkMode = vbLinkAutomatic       'Attempt to establish an automatic DDE link
          If Err = 0& Then Timer1.Enabled = False 'If successful, the Timer's job is done, so disable it for now
          On Error GoTo 0
      End Sub
      
      Private Sub Label1_LinkClose()
          Timer1.Enabled = True                   'Re-enable the Timer once the DDE conversation terminates
      End Sub
      
      Private Sub Form_Unload(Cancel As Integer)
          Label1.LinkMode = vbLinkNone            'Close the DDE conversation, if still active
      End Sub

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    6

    Re: Reading writing from to the same txt file "rs232"

    Quote Originally Posted by passel View Post
    Rather than write a string to a text file, I would just write the number to a binary file at the first offset, and the second application could just read the number in from that offset. You don't have to specify the file is shared when using binary, and you are less likely to get a corrupted value if one file is writing and the other reading at the same time.

    As a quick test, I have the first program increment a number and put it to the binary file.
    Code:
    Option Explicit
    
    Dim counter As Long
    
    Private Sub Form_Load()
      Open "c:\c\tst.cnt" For Binary As #1
      Timer1.Interval = 1000
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      counter = counter + 1
      Put #1, 1, counter
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      Close 'close all open files
    End Sub
    I have the second application open the same binary file, and read the value and update the form's caption with the number read.
    Code:
    Option Explicit
    Dim counter As Long
    
    Private Sub Form_Load()
      Open "c:\c\tst.cnt" For Binary As #1
      Timer1.Interval = 1000
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      Get #1, 1, counter
      Me.Caption = counter
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
      Close 'close all open files
    End Sub
    If you deal with multiple files, or are creating a routine for reuse, you would likely want to use FreeFile to get a file handle number, rather than hardcode #1 as I have. But, if your application is only going to open the one file, then it doesn't really matter.
    thank you so much for your reply, what I'm doing is reading fro serial port asci data and I want to overwrite it to a file every 1s and read it from the same file every 1s with another vb6 app,
    I tried your way I create a txt file on the directory you typed and nothing is written to that file and sometimes a random character is written and when I'm using the 2nd VB6 to read from the file I got an error type mismatch
    I'm a newbie to visual basic I only know some simple stuff.

  7. #7
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,580

    Re: Reading writing from to the same txt file "rs232"

    Quote Originally Posted by Victor Bravo VI View Post
    Here's an alternative approach that forgoes writing to a text file. This simple solution makes use of VB6's built-in support for DDE instead.
    Fascinating piece of archeology and Bonus points for DDE predating the very COM foundations VB6 is based upon! I was about to throw in file monikers with GetObject for "cheap" IPC but this is so much more bizarre.

    Sure *everything* IPC is based on windows msgs + shared memory underneath but it's the additional abstraction layers that bring the extra coins for the players here :-))

    cheers,
    </wqw>

  8. #8
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    269

    Re: Reading writing from to the same txt file "rs232"

    Quote Originally Posted by wqweto View Post
    I was about to throw in file monikers with GetObject for "cheap" IPC but this is so much more bizarre.
    Well, your recent ROT routines did cross my mind as another possible solution while I was reading this thread. However, I tried to see if I could present an alternate solution by re-reading Interprocess Communications and that's when I remembered about VB6's oft-forgotten built-in support for DDE.

    One caveat with that approach though is that the destination process must be running at the same or higher integrity level as the source process or the WM_DDE_* messages will be blocked by the UIPI message filter. For the uninitiated, what this means is that if the source process is running elevated (running as administrator), the destination process must be elevated as well in order for the DDE conversation to successfully take place. I haven't yet verified if ChangeWindowMessageFilter[Ex] can help unblock the WM_DDE_* messages, but from a quick Google search, it seems that it can.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,947

    Re: Reading writing from to the same txt file "rs232"

    Quote Originally Posted by LRH18 View Post
    Thanks for your reply,
    I don't know what u mean,I'm not that good with VB6, If you can explain more I would be grateful.
    Here is a link to the options that can be used with the VB Open statement

    https://docs.microsoft.com/en-us/off...open-statement

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,947

    Re: Reading writing from to the same txt file "rs232"

    Depending on what you are actually trying to do it may be better to just not use a file at all and send the data using UDP

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    6

    Re: Reading writing from to the same txt file "rs232"

    thank you so much for this,
    I tried your program it worked, I'll try this with the 2 apps & serial com, and I believe it'll work, it's a simpe and effective way to link to separated programs without writing & reading to a file , thanks again.

  12. #12
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,580

    Re: Reading writing from to the same txt file "rs232"

    Quote Originally Posted by Victor Bravo VI View Post
    One caveat with that approach though is that the destination process must be running at the same or higher integrity level as the source process or the WM_DDE_* messages will be blocked by the UIPI message filter.
    Security is hard. With ROT there is the ROTFLAGS_ALLOWANYCLIENT flag on Register but it needs the server .exe to be registered w/ an AppID and a special "LocalService" flag under its AppID in registry.

    I'm using this flag to succesfully expose a custom file moniker from an NT Service that runs under LocalSystem account (or any other account) in the linked project above, so that after GetObject the client app gets a reference to an object that executes its methods under different security context which is a powerful option for an already rather convenient IPC.

    cheers,
    </wqw>

  13. #13
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,638

    Re: Reading writing from to the same txt file "rs232"

    We also have the Mailslots facility. Similar to UDP in some ways but part of Microsoft Networking for a very long time.
    Attached Files Attached Files

  14. #14

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    6

    Re: Reading writing from to the same txt file "rs232"

    Thanks for your reply , I want to use a file because the other app which will read the vale can be programmed with another language not just VB6

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,203

    Re: Reading writing from to the same txt file "rs232"

    That's not an argument against UDP. Much like TCP, UDP is pretty fundamental stuff, and if you are using a language that knows anything at all about communicating over a network, then it will certainly have UDP functionality. UDP was the basis for LAN games in the 90s and back into the days of DOS. It's light, fast, and can broadcast.

    On the other hand, reading and writing files is something that everybody learns early on, so if you are starting out, dropping to a file is the simplest route to follow.
    My usual boring signature: Nothing

  16. #16

    Thread Starter
    New Member
    Join Date
    Oct 2019
    Posts
    6

    Re: Reading writing from to the same txt file "rs232"

    thanks for this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width