想請教各位達人我的指令或程式是不是有地方錯了,導致PLC沒有動作, 我的PLC IP : 192.168.1.20 ; Port: 9600 PC IP : 192.168.1.10
PLC 透過 Ethernet 直接與 PC 連接,中間無其他分支, Fins Command :800002001400000A00000102820001000001000176*CR
VB Code :
Public Class Form1
Public Delegate Sub SetMsg1Callback(ByVal strReceive As String) Private Sub DisplayMsg1(ByVal strReceive As String) If TextBox1.InvokeRequired Then Dim d As SetMsg1Callback = New SetMsg1Callback(AddressOf DisplayMsg1) Invoke(d, New Object() {strReceive}) Else TextBox1.Text += strReceive TextBox1.Text += Environment.NewLine End If End Sub
Dim RemoteIpEndPoint As IPEndPoint Dim sendUdpClient As UdpClient
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click RemoteIpEndPoint = New IPEndPoint(IPAddress.Parse(TextBox2.Text), TextBox3.Text) sendUdpClient = New UdpClient sendUdpClient.Connect(RemoteIpEndPoint) sendUdpClient.BeginReceive(New AsyncCallback(AddressOf Receivedata), sendUdpClient) End Sub
Sub Receivedata(ByVal pIAsyncResult As IAsyncResult)
Dim receiveBytes As Byte() receiveBytes = sendUdpClient.EndReceive(pIAsyncResult, RemoteIpEndPoint) Dim myString As String = ""
myString = Encoding.ASCII.GetChars(receiveBytes) myString = myString.TrimEnd() DisplayMsg1(myString)
sendUdpClient.BeginReceive(New AsyncCallback(AddressOf Receivedata), sendUdpClient) End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim sendBytes As Byte() sendBytes = Encoding.ASCII.GetBytes(TextBox4.Text) sendUdpClient.BeginSend(sendBytes, sendBytes.Length, New AsyncCallback(AddressOf Senddata), sendUdpClient) End Sub
Sub Senddata(ByVal pIAsyncResult As IAsyncResult) Dim OutBytes As Integer OutBytes = sendUdpClient.EndSend(pIAsyncResult) End Sub End Class
|