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