Hello, follow my code please.

I created this code for read values from Mitsubishi FX3G linked with FX3U-ENET i this work. I put the right port and always i put INICIAR e read all values.

Now i need to read this values automaticly , minute to minute.
I try diferent ways but i cant do. Someone knows how i can read this values automatic?????

Help!

Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Timers

Public Class ServerForm

    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim client As NetworkStream
    Dim PortNum As Integer
    Dim Ipaddress As String = "192.168.10.99"
    Dim RxResponse As String
    '*************************************'
    Dim TxCommand As String
    Dim Buffer() As Byte
    Dim InBuff(1532) As Byte
    Dim SubHeader$
    '********************************************************************************************************************************
    Private Sub PortTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles PortTextBox.Validating
        Dim deltaPort As Integer
        PortNum = Integer.Parse(PortTextBox.Text)
        Try
            clientSocket.Connect(Ipaddress, PortNum)
            RichTextBox1.Text = "PLC Connected ..."
            client = clientSocket.GetStream()
        Catch ex As Exception
            RichTextBox1.Text = "PLC Connection Failed ..."
        End Try

        If Not Integer.TryParse(PortTextBox.Text, deltaPort) OrElse deltaPort < 1 OrElse deltaPort > 65535 Then
            MessageBox.Show("Numero de porta entre 1 e 65535.", "Numero de porta invalido", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            PortTextBox.SelectAll()
            e.Cancel = True
        End If
    End Sub

    '********************************************************************************************************************************
    Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click

        TxCommand = "01ff000a4420000000641400"                                'Read D100 to D124 (5 points) with the A-compatible 1E frame command.
        Buffer = System.Text.Encoding.Default.GetBytes(TxCommand.ToCharArray) 'Sending a read command
        client.Write(Buffer, 0, Buffer.Length)
        client.Flush()

        While Not client.DataAvailable()                                       'Waiting for a response from an Ethernet block
            Application.DoEvents()
        End While

        If client.DataAvailable() Then
            client.Read(InBuff, 0, InBuff.Length)
            RxResponse = System.Text.Encoding.Default.GetString(InBuff)
            SubHeader = Mid$(RxResponse, 3, 2)
            receive_data(SubHeader)
        Else
            lstOutput.SelectedIndex = lstOutput.Items.Count - 1
        End If
    End Sub

    '********************************************************************************************************************************
    Private Sub receive_data(ByVal cabecalho As String)
        Dim Temp As String
        Dim j As Integer
        Dim Dreg(20) As Double
        Dim DregStr$

        If cabecalho = "00" Then                                             'Normal response
            Temp = ""                                                        'Initialization of an output character string
            For j = 0 To 9
                DregStr$ = Mid(RxResponse, j * 4 + 5, 4)
                Dreg(j) = Val("&H" + DregStr$)
                Temp = Temp + Format(Dreg(j), "#####0") + " "
                lstOutput.Items.Insert(lstOutput.Items.Count, Dreg(j))
                client.Flush()
            Next
        ElseIf cabecalho = "5B" Then                                         ' In an abnormal response, an abnormal code is added.
            Temp = "Terminate Code = " & cabecalho & " Error Code = " & Mid$(RxResponse, 5, 2)
            lstOutput.Items.Insert(lstOutput.Items.Count, Temp)
        Else
            Temp = "Terminate Code = " & cabecalho
            lstOutput.Items.Insert(lstOutput.Items.Count, Temp)
        End If
    End Sub

    '********************************************************************************************************************************

    Private Sub EndButton_Click(sender As Object, e As EventArgs) Handles EndButton.Click
        Application.Exit()
    End Sub

End Class

I resolved the problem. . .look if my solution is the better!

'*******************************************************************************************************************************'
'************************************************** PC DE AQUISIÇÃO DE DADOS ***************************************************'
'*******************************************************************************************************************************'
Option Explicit On
Option Strict On

Imports System.Net          'for IPAddress
Imports System.Net.Sockets  'for TcpListener
Imports System.Threading

Public Class ServerForm

    Dim PortNum As Integer
    Dim client As New TcpClient

    Private count As Integer = 0
    Private Property IPAddress As String

    '*******************************************************************************************************************************'
    '*********************************************************** Ligação ***********************************************************'
    '*******************************************************************************************************************************'
    Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click

        PortNum = Integer.Parse(PortTextBox.Text)
        'IPAddress = IpTextBox.Text
        IPAddress = "192.168.10.99"
        'If IPAddress = "192.168.10.99" Then
        'If PortNum = 5000 Then
        'server = New TcpListener(IPAddress.Any, PortNum)
        'listening.Start()
        Try
            client.Connect(IPAddress, PortNum)
            RichTextBox1.Text() = "Connection is Accepted"
            receber_dados()
            Timer1.Start()
        Catch ex As Exception
            RichTextBox1.Text() = "Connection is Failed"
        End Try
        'End If
        'End If
    End Sub

    '*******************************************************************************************************************************'
    '******************************************************** Verificação da Porta *************************************************'
    '*******************************************************************************************************************************'
    Private Sub PortTextBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles PortTextBox.Validating
        Dim deltaPort As Integer
        If Not Integer.TryParse(PortTextBox.Text, deltaPort) OrElse deltaPort < 1 OrElse deltaPort > 65535 Then
            MessageBox.Show("Port number must be an integer between 1 and 65535.", "Invalid Port Number", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            PortTextBox.SelectAll()
            e.Cancel = True
        End If
    End Sub

    '*******************************************************************************************************************************'
    '******************************************************* RECEBER DADOS *********************************************************'
    '*******************************************************************************************************************************'
    Private Sub receber_dados()

        Dim TxCommand As String
        Dim Buffer() As Byte
        Dim InBuff(1532) As Byte
        Dim RxResponse As String
        Dim Temp As String
        Dim j As Integer
        Dim Dreg(20) As Double          'Numero de valores que conseguimos ler
        Dim DregStr$
        Dim SubHeader$

        If count = 0 Then
            Timer1.Start()
            RichTextBox1.Text = ("LEITURA DE VALORES . . .")

            TxCommand = "01ff000a4420000000641400"                                'Read D100 to D124 (5 points) with the A-compatible 1E frame command.
            Buffer = System.Text.Encoding.Default.GetBytes(TxCommand.ToCharArray) 'Sending a read command
            client.GetStream().Write(Buffer, 0, Buffer.Length)

            While Not client.GetStream().DataAvailable()                          'Waiting for a response from an Ethernet block
                Application.DoEvents()
            End While

            If client.GetStream().DataAvailable() Then
                client.GetStream().Read(InBuff, 0, InBuff.Length)
                RxResponse = System.Text.Encoding.Default.GetString(InBuff)
                SubHeader = Mid$(RxResponse, 3, 2)
                If SubHeader = "00" Then                        'Normal response
                    Temp = ""                                   'Initialization of an output character string
                    For j = 0 To 9
                        DregStr$ = Mid(RxResponse, j * 4 + 5, 4)
                        Dreg(j) = Val("&H" + DregStr$)
                        Temp = Temp + Format(Dreg(j), "#####0") + " "
                        lstOutput.Items.Insert(lstOutput.Items.Count, Dreg(j))
                        client.GetStream.Flush()
                    Next
                ElseIf SubHeader = "5B" Then                    ' In an abnormal response, an abnormal code is added.
                    Temp = "Terminate Code = " & SubHeader & " Error Code = " & Mid$(RxResponse, 5, 2)
                    lstOutput.Items.Insert(lstOutput.Items.Count, Temp)
                Else
                    Temp = "Terminate Code = " & SubHeader
                    lstOutput.Items.Insert(lstOutput.Items.Count, Temp)
                End If
                lstOutput.SelectedIndex = lstOutput.Items.Count - 1
            End If

            'client.GetStream().Close()                          ' Line disconnection processing
            'client.Close()
        End If
    End Sub

    '****************************************************************************************************************************************'
    '******************************************************** CLOCK *************************************************************************'
    '****************************************************************************************************************************************'
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        count = count + 1
        Timer1TextBox.Text() = count.ToString
        If count = 5 Then                      '30 = 15 seg porque no timer de interface definimos Timer1 como 1seg
            Timer1.Stop()
            count = 0
            RichTextBox1.Text = ("FLAG BLOW!!!!!")
            receber_dados()
        End If
    End Sub

    '****************************************************************************************************************************************'
    '*********************************************************** EXIT ***********************************************************************'
    '****************************************************************************************************************************************'
    Private Sub EndButton_Click(sender As Object, e As EventArgs) Handles EndButton.Click
        Application.Exit()
    End Sub

    '****************************************************************************************************************************************'
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.