Author Topic: Read String From Holding Registers  (Read 6310 times)

MrPike

  • Sr. Member
  • ****
  • Posts: 297
    • View Profile
Re: Read String From Holding Registers
« Reply #15 on: March 23, 2018, 09:37:44 AM »
Ah ha!  So we are using the read method.  This was my next step since I couldn't get the BeginRead to work.  I assume I call the BackgroundWorker1.RunWorkerAsync() and this will treat it as Asynchronous?  Thank you. 

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Read String From Holding Registers
« Reply #16 on: March 23, 2018, 10:28:37 AM »
I assume I call the BackgroundWorker1.RunWorkerAsync() and this will treat it as Asynchronous?
RunWorkerAsync will return from the call immediately after starting the background thread. The alternative would block the call until the background thread is completed.

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: Read String From Holding Registers
« Reply #17 on: March 23, 2018, 12:07:08 PM »
"I missed something critical in the code above" Archie, you cant miss that, lol.

Kidding aside, I believe  the BW Progress or Completed will allow you to set text change from original UI thread without delegate and invoke
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Read String From Holding Registers
« Reply #18 on: March 23, 2018, 02:10:43 PM »
"I missed something critical in the code above" Archie, you cant miss that, lol.

Kidding aside, I believe  the BW Progress or Completed will allow you to set text change from original UI thread without delegate and invoke
I just did a quick test and the BackgroundWorker will definitely throw a cross-thread operation exception if trying to set GUI control properties:
Code: [Select]
    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        While Not StopThread
            '* Will result in cross thread operation exception
            Label2.Text = "Abc"

            '* Prevent from using excessive CPU
            Threading.Thread.Sleep(50)
        End While
    End Sub

bachphi

  • Hero Member
  • *****
  • Posts: 642
    • View Profile
Re: Read String From Holding Registers
« Reply #19 on: March 23, 2018, 04:39:04 PM »
Archie, not in the DoWork  sub,  put the text code in  Progress or Completed.

https://www.advancedhmi.com/forum/index.php?topic=1431.msg7591#msg7591
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Read String From Holding Registers
« Reply #20 on: March 23, 2018, 05:13:18 PM »
Archie, not in the DoWork  sub,  put the text code in  Progress or Completed.

https://www.advancedhmi.com/forum/index.php?topic=1431.msg7591#msg7591
My mistake, I totally missed that you stated the Progress or Completed events. That gets away from the invoke. So my modified code would look like this:
Code: [Select]
    Private StopThread As Boolean
    Private StringResult As String
    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        While Not StopThread
            Dim MyValues() As String = ModbusTCPCom1.Read("40001", 10)
            StringResult = MfgControl.AdvancedHMI.Drivers.Common.CalculationsAndConversions.WordsToString(MyValues)

            BackgroundWorker1.ReportProgress(0)

            '* Prevent from using excessive CPU
            Threading.Thread.Sleep(50)
        End While
    End Sub

    Private Sub MainForm_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        StopThread = True
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Label1.Text = StringResult
    End Sub

Then you have to make sure to set the property WorkerReportsProgress to True

Steve Etter

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Read String From Holding Registers
« Reply #21 on: March 26, 2018, 07:35:02 AM »
When I try to download the file from reply #11, I get an "Internal Server Error" and a message to "contact the server administrator, webmaster@downloads.advancedhmi.com and inform them of the time the error occurred".

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Read String From Holding Registers
« Reply #22 on: March 26, 2018, 08:03:25 AM »
When I try to download the file from reply #11, I get an "Internal Server Error" and a message to "contact the server administrator, webmaster@downloads.advancedhmi.com and inform them of the time the error occurred".
See this for the latest beta version:

https://www.advancedhmi.com/forum/index.php?topic=2058.0

Steve Etter

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Read String From Holding Registers
« Reply #23 on: March 26, 2018, 10:55:33 AM »
Got it.  Thanks