1

我需要帮助将 Modbus 从服务器与 VB.Net 通信。我可以在 VB6 中向从机读取和写入数据,但是在尝试使用 VB.net 向从机读取和写入数据时,这是不可能的。我正在附加代码参考资料。等待一个肯定的答复。

com-com3 in my case
slave id-1
br-19200
databit-8
parity-even
stopbit-1

Imports System.IO.Ports
Imports System.Text

Public Class Form1
    Dim TxMessage As String
    Dim RxMessage As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim spOpen As String
        SerialPort1.Open()
        spOpen = SerialPort1.PortName
        Label1.Text = spOpen
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Timer1.Enabled = True
        Timer_time()
    End Sub

    Private Sub Timer_time()
        Dim CRCWord As Long
        Dim iread As Integer = 7
        Dim ch() As Char = New Char(iread) {}

        TxMessage = Chr(1) & Chr(3) & Chr(0) & Chr(100) & Chr(0) & Chr(1)
        'call the CRC calculation:'
        CRCWord = GetCRC(TxMessage)
        'add the CRC bytes to the message:
        TxMessage = TxMessage & Chr(CRCWord - Int(CRCWord / 256) * 256)
        TxMessage = TxMessage & Chr(Int(CRCWord / 256))
        'clear status text display:
        'clear the receive data buffer:

        SerialPort1.Write(TxMessage)


        RxMessage = ""
        Dim tcnt As Integer = 0
        Do
            'DoEvents()
            tcnt = tcnt + 1
            '''''''''''''''''''''''''''''''
        Loop Until SerialPort1.BytesToRead >= 7 Or tcnt >= 30000
        'RxMessage = SerialPort1.ReadExisting
        SerialPort1.Read(ch, 0, iread)

        ''  SerialPort1.Read(ch, 0, 7)
        '' Dim a1 As New StringBuilder
        '' Dim ch1 As Char
        '' For Each ch1 In ch
        '' a1 = a1.Append(ch1)
        '' Next
        'Dim bb As String = SerialPort1.ReadExisting
        '' Dim newbuffer As String = a1.ToString
        '' RxMessage = RxMessage & newbuffer

        'RxMessage = SerialPort1.ReadLine
        'Check RxMessage to see if it's complete - the length of each expected reply
        'can be calculated before the TxMessage is sent, when RxMessage contains
        'the expected message length, then reception is complete.
        'Check for valid message CRC:

        If Len(RxMessage) >= 7 Then
            Dim RxCount As Long = Len(RxMessage)
            Dim CRCValue As Long = GetCRC(Mid(RxMessage, 1, RxCount - 2))
            If (CLng(Asc(Mid(RxMessage, RxCount - 1, 1))) + _
                (CLng(Asc(Mid(RxMessage, RxCount, 1))) * 256)) = CRCValue Then
                TextBox1.Text = btod(splitbit(Asc(Mid(RxMessage, 4, 1))) & splitbit(Asc(Mid(RxMessage, 5, 1))))
            End If
        End If
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived


    End Sub

    Private Sub plcwrite()
        Dim CRCWord As Long
        Dim art As String
        Dim varo As Long
        Dim art1 As String
        Dim art2 As String
        art = splitbit(TextBox3.Text)
        varo = Len(art)
        varlen = Strings.Right(art, 8)
        varlen1 = Strings.Left(art, varo - 8)
        art1 = btod(varlen1)
        art2 = btod(varlen)
        TxMessage = Chr(1) & Chr(6) & Chr(0) & Chr(TextBox2.Text) & Chr(art1) & Chr(art2)

        CRCWord = GetCRC(TxMessage)
        'add the CRC bytes to the message:
        TxMessage = TxMessage & Chr(CRCWord - Int(CRCWord / 256) * 256)
        TxMessage = TxMessage & Chr(Int(CRCWord / 256))
        'clear status text display:
        'clear the receive data buffer:
        RxMessage = ""

        'send the message out the COM port:
        Dim tcnt As Integer
        tcnt = 0
        Do
            Application.DoEvents()
            tcnt = tcnt + 1
        Loop Until SerialPort1.BytesToRead >= 7 Or tcnt >= 30000
        Dim a1 As Integer
        a1 = Len(TxMessage)
        SerialPort1.Write(TxMessage, 0, a1)
        SerialPort1.DiscardOutBuffer()

        MsgBox("Controller Setting update")
    End Sub
4

0 回答 0