[三菱] UDP傳送Ffins指令

[复制链接]
查看1315 | 回复4 | 2012-7-23 16:18:25 | 显示全部楼层 |阅读模式

想請教各位達人我的指令或程式是不是有地方錯了,導致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


loveplc | 2012-7-23 18:13:41 | 显示全部楼层
800002001400000A00000102820001000001000176*CR
可以看到是16进制的,CR是回车符吧,所以也要转成16进制。
所以要保证VB发出的是16进制啊
fwt0209 | 2012-7-24 08:57:37 | 显示全部楼层
是回車符沒錯,我打在 VB 裡用 vbCr 代替,
我用這語法「 Dim sendBytes As Byte() '宣告傳送字串的 Byte 陣列
        sendBytes = Encoding.ASCII.GetBytes("800002001400000A00000102820001000001000176*" & vbCr)」可以很順利,不過 Byte 陣列裡裝的是 10 進制的 Ascii 碼,
但當我用迴圈(For......Next) sendBytes = Hex(sendBytes) 將陣列轉成 16 進制時,* 號那邊出錯了  ,
* 號在 16 進制為 2A,電腦執行時出現了"從字串 "2A" 至型別 'Byte' 的轉換是無效的。"的錯誤訊息~~

請問怎麼了?
星空一点roy | 2013-3-12 22:42:06 | 显示全部楼层
谢谢楼主分享!
星空一点roy | 2013-3-13 14:55:50 | 显示全部楼层
谢谢楼主分享!
您需要登录后才可以回帖 登录 | 注册哦

本版积分规则