簡體   English   中英

串行端口在帶有打包消息的VB.NET中發送數據

[英]Serial Port Send Data in VB.NET with Packed message

我有《編程手冊》協議的手冊,但是在編寫代碼時,vb.net返回錯誤消息。

以下是字節數組的表結構

位置1 2 3 4 5 6 7 8

名稱SOH LEN SEQ CMD DATA后同步碼BCC ETX

長度/字節1 1 1 1 0-200 1 4 1

值(01h)(20h-FFh)(20h-FFh)(20h-FFh)(20h-FFh)(05h)(30h-3Fh)(03h)

縮略語:

“ SOH” -(標題開始)打包消息的開始

“ LEN” -從位置2到位置6的字節總數,加上20h的固定偏移量。

“ SEQ” -數據包的序列號。 SLAVE在回復消息中放置相同的“ SEQ”。 如果SLAVE接收到的消息與上次正確接收的消息一樣具有相同的“ SEQ”和“ CMD”,則設備將忽略該消息並重復發送到主機的最后一個數據包。

“ CMD” –命令代碼

“ DATA” -數據,根據命令。 如果沒有數據,則長度字段為零。

“ BCC” –控制和(0000h-FFFFh)。 從位置2到位置6的數據字節總和。控制總和以ASCІІ類型傳輸(12АВ作為31h 32h3Аh3Вh傳輸)。

“ ETX” –(TeXt的結尾)打包消息的結尾。

代碼示例:

公開課表格1

Private WithEvents sp As System.IO.Ports.SerialPort

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

    For Each y As String In System.IO.Ports.SerialPort.GetPortNames
        Me.ComboBox1.Items.Add(y)
    Next
    sp = New IO.Ports.SerialPort("COM4")
    sp.StopBits = IO.Ports.StopBits.One
    sp.BaudRate = 9600
    sp.DataBits = 8
    sp.Parity = IO.Ports.Parity.None
    sp.Handshake = IO.Ports.Handshake.None

    sp.Open()
    Dim by As Byte = &H20

    ' MsgBox(&H20)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Dim By(9) As Byte
        By(0) = 1
        By(1) = &H2 + &H20
        By(2) = &H58
        By(3) = &H45
        By(4) = &H5
        Dim S As String = Hex(&H58 Xor &H45)
        '  MsgBox(S)
        By(5) = &H30
        By(6) = &H30
        By(7) = &H31
        By(8) = &H3D
        By(9) = &H3

        sp.Write(By, 0, By.Length - 1)
       ' MsgBox(sp.ReadByte)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
    Dim rec As System.IO.Ports.SerialPort = sender
    MsgBox(Hex(rec.ReadByte))
End Sub

末級

Annael您可以通過調用更新功能並提供500ms的延遲來做到這一點。 現在,在接收數據時,您可以接收輸入數據

Dim inputData As String = ""// Declare this as a global variable in the program
    Private WithEvents sp As System.IO.Ports.SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   For Each y As String In System.IO.Ports.SerialPort.GetPortNames
            Me.ComboBox1.Items.Add(y)//the code you have written to add Serial ports

SerialPort1.PortName = ComboBox1.Text//this will access your com port as your button
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim by As Byte = &H20    
                By(0) = 1
                By(1) = &H2 + &H20
                By(2) = &H58
                By(3) = &H45
                By(4) = &H5
                Dim S As String = Hex(&H58 Xor &H45)
                '  MsgBox(S)
                By(5) = &H30
                By(6) = &H30
                By(7) = &H31
                By(8) = &H3D
                By(9) = &H3
            SerialPort1.BaudRate = 115200
            SerialPort1.Open()
            SerialPort1.Write(By)
        Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

            Threading.Thread.Sleep(500) ' 500ms delay induced to receive the email
            inputData = SerialPort1.ReadExisting 'or SerialPort1.ReadLine


            Me.Invoke(New EventHandler(AddressOf DoUpdate))

    Public Sub DoUpdate()

                MsgBox(inputData)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM