Threading in VB.net

MertensS

New member
Joined
Oct 6, 2006
Messages
2
Programming Experience
Beginner
Hello,

I have a problem with my project.

I am trying to send/read data from a plc true a certain protocol (modbus for those that want to know). This i can get to work but the actual problem is that my computer gets slow and lags alot when i send/recieve data to the plc.

Someone told me that threading would be usefull to implement so that the project keeps responding.

How can i implement threading?

I have a sub that checks wich item is selected in a combobox (read or write) which is in an infinitive loop. With an if statement i check which of the 2posibillities is selected and then i jump to the corresponding sub.

VB.NET:
Private Sub cmbkeuze_SelectedIndexChanged
do
If cmbkeuze.SelectedIndex = "0" Then
writeshort()
End If
If cmbkeuze.SelectedIndex = "1" Then
ReadShort()
End If
loop
End Sub

Do i create 2threads like threadRead and threadWrite or how can i best do this?

thx
 
Last edited by a moderator:
Running a method asynchronously by it's own thread is simple to begin with, just create an instance and start it and off it goes on its own, the main thread continues to next statement immediately.
VB.NET:
Dim t As New Threading.Thread(AddressOf writeshort)
t.Start()
'main thread continues here right away..
 
personally, i would run a ReadShort thread (which uses an event on the main form when data is recieved) and leave the WriteShort on the main window thread, cause it's only needed when data needs to be sent
 
Well maybe it is wise to explain a little bit of the modbus protocol.

Modbus protocol is Master- slave based. The slave can only respond to the master his request (slave cant send a message to the master by itself).
In my case the master (PC) will send out a read request to the slave (plc) or a write request.


So this rules out the readshort thread that was based on a data recieved event because i need to send the request first.

I will paste the important parts of my code so you get a better idea on how i try to get it to work (I haven't used any threading commands here exept the thread.sleep(..)

form:
see attachment



declarations
VB.NET:
Public BusState As Short
    Public M_handle, j As Integer 
    Public Ready As Boolean 'true of false
    Public Data(126) As Short
    Public a As Short
    Public b As Short = 1
    Public Address_Ptr As IntPtr = Marshal.AllocHGlobal(8)
    Public Data_Ptr As IntPtr = Marshal.AllocHGlobal(128)
this part is to determan wether i want to write or read.
VB.NET:
Private Sub btnVoeruit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVoeruit.Click
        If cmbkeuze.SelectedIndex = "0" Then
            ReadShort()
        End If
        If cmbkeuze.SelectedIndex = "1" Then
            WriteShort()
        End If
    End Sub
Then the 2 subs can be called up:
VB.NET:
Private Sub ReadShort()
        Dim i As Integer
        Dim strdata As String

        Do
            Thread.Sleep(500)
            Call MBRead(M_handle, j, 1, mbShort, CInt(txtStartadres.Text), CInt(txtAantalreg.Text))
            Ready = MBGetShorts(M_handle, j, BusState, Address_Ptr, Data_Ptr)

            While (Ready = False)
                System.Windows.Forms.Application.DoEvents()
               Ready = MBGetShorts(M_handle, j, BusState, Address_Ptr, Data_Ptr)
            End While

            For a = 0 To 126 Step 2
                Data((a / 2) + 1) = Marshal.ReadInt16(Data_Ptr, a)
            Next a
            Application.DoEvents()
            If BusState > 0 Then 'when no error occured
                strdata = ""
                For i = 1 To CInt(txtAantalreg.Text)
                    strdata &= Data(i) & vbNewLine
                Next
                txtdata.Text = strdata
               
            End If
            Application.DoEvents()
        Loop
    End Sub
VB.NET:
Private Sub WriteShort()
        Dim i As Integer
        Dim stotal As String
        Dim stemp As String
        Dim cDelims() As Char = {","c, " "c, vbCr, vbLf, vbTab, vbNullChar}  ' data value delimiters
        Dim index As Integer

        Do

            Application.DoEvents()

            Thread.Sleep(500)
            If txtdataW.Text.Length <> 0 Then
                stotal = txtdataW.Text  ' De volledige data
                stemp = ""

                For i = 0 To CInt(txtAantalreg.Text) - 1
                 
                    stotal = stotal.TrimStart(cDelims)   

                    index = stotal.IndexOfAny(cDelims, 0)
                    If index = -1 Then index = stotal.Length 
                    If index <> -1 Then

                        stemp = stotal.Substring(0, index)
                        Try
                            a = CInt(stemp)
                            Data(i) = a ' zet de omgezetten waarde in de array
                        Catch
                         
                            Data(i) = 0

                        Finally
                        End Try
                     
                        stotal = stotal.Remove(0, index)
                    Else
                      
                        Data(i) = 0
                    End If

                    Application.DoEvents()
                Next
            End If
            Marshal.Copy(Data, 0, Data_Ptr, CInt(txtAantalreg.Text))
            MBWriteShort(M_handle, j, 1, mbShort, CInt(txtStartadres.Text), CInt(txtAantalreg.Text), Data_Ptr)
            Ready = MBGetShorts(M_handle, j, BusState, Address_Ptr, Data_Ptr)
            lblBusstate.Text = BusState
            ShowState((BusState))
            Application.DoEvents()
        Loop
    End Sub
Can i keep the basic like this if i want to add the threading part to it or do i need to change my structure to something like this:

I make the readshort sub my main thread which i do over and over again (do ... loop) and in that sub i call up the writeshort sub if a button is pressed.
This way I will always ask for the data from the slave and only write when i press the button.

What do you guys think?
 

Attachments

  • mbtester.JPG
    mbtester.JPG
    16.2 KB · Views: 28
Back
Top