SQE 發問時間: 電腦與網際網路程式設計 · 7 年前

請問VB 2010與C++通訊 delta PLC的寫法

小弟初學 最近看書 要用serialport去控制 但是仍然不解 請教程式寫法範例

請問如果要開啟M0及讀取X0的寫法如何寫?PLC 是 delta DVP-14SS2

通訊參數 COM6,9600,e,7,1

已更新項目:

plc的站號是01 請指教

2 個已更新項目:

標題有錯 請問VB 2010的寫法 謝謝

1 個解答

評分
  • 匿名使用者
    7 年前
    最佳解答

    Public Class Form1

    Private Function LRC(ByVal str As String) As String

    Dim c As Integer = 0

    Dim l As String = Len(str)

    Dim c_data As String

    Dim d_lrc As Double

    Dim h_lrc As String

    For c = c + 1 To l

    c_data = Mid$(str, c, 2)

    d_lrc = d_lrc + Val("&H" + c_data)

    c = c + 1

    Next c

    If d_lrc > &HFF Then

    d_lrc = d_lrc Mod &H100

    End If

    h_lrc = Hex(&HFF - d_lrc + 1)

    If Len(h_lrc) > 2 Then

    h_lrc = Mid(h_lrc, Len(h_lrc) - 1, 2)

    End If

    LRC = h_lrc

    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    SerialPort1.PortName = "COM6"

    SerialPort1.BaudRate = 9600

    SerialPort1.Parity = IO.Ports.Parity.Even

    SerialPort1.DataBits = 7

    SerialPort1.StopBits = 1

    SerialPort1.Open()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim msg As String = ":01050800FF00" & LRC("01050800FF00") & Chr(13) & Chr(10)

    Me.SerialPort1.Write(msg)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim msg As String = ":010508000000" & LRC("010508000000") & Chr(13) & Chr(10)

    Me.SerialPort1.Write(msg)

    End Sub

    End Class

    開啟M0

還有問題?馬上發問,尋求解答。