Results 1 to 7 of 7

Thread: [RESOLVED] Serial Port receiving hex data.

  1. #1

    Thread Starter
    Junior Member dragonguy's Avatar
    Join Date
    Nov 2012
    Posts
    25

    Resolved [RESOLVED] Serial Port receiving hex data.

    Hi, everyone! i got some problem about vb.net Serial port, hope you guys can help me

    i want to write a program to receive hex code from machine. The machine non stop every second send out 48byte hex code.
    so i need to capture all 48 hex code per second. the hex look like below:

    986700272700000000800080000000000000000000000000986800272700000000800002000000000000000000000000

    how can i receive all 48 hex code, and show in textbox1?
    and then how can i capture 10th, 11th and 12th hex code show in textbox2?

    i'm write the code can capture the hex code but cant store complete 48 byte hex code.
    here is my vb.net code:
    Code:
    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
            Dim n As Integer = SerialPort1.BytesToRead 'find number of bytes in buf
            comBuffer = New Byte(n - 1) {} 're dimension storage buffer
            SerialPort1.Read(comBuffer, 0, n) 'read data from the buffer
            Me.Invoke(UpdateFormDelegate1) 'call the delegate   
        End Sub
    
        Private Sub UpdateDisplay()
            On Error GoTo handle
            Dim num1, num2, num3, str1, str2, str3 As String
            Dim lngpos As Long
            num2 = ""
            For i = LBound(comBuffer) To UBound(comBuffer)
                num1 = Hex(comBuffer(i))
                If Len(num1) = 1 Then
                    num2 = "0" & num1
                Else
                    num2 = num1
                End If
            Next
    
            TextBox1.Text &= num2 'store all hex code
            num3 = TextBox1.Text
            lngpos = InStr(num3, 80)
            str1 = Mid$(num3, lngpos, 6)
            TextBox2.Text = str1 
    end sub
    please help me.

    thank!

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Serial Port receiving hex data.

    Try it this way:

    Code:
        Dim rcvQ As New List(Of Byte)
        Dim rcvQlock As New Object
    
        Private Sub SerialPort1_DataReceived(sender As System.Object, _
                                             e As System.IO.Ports.SerialDataReceivedEventArgs) _
                                         Handles SerialPort1.DataReceived
            Dim n As Integer = SerialPort1.BytesToRead 'find number of bytes in buf
            Dim comBuffer(n - 1) As Byte
            SerialPort1.Read(comBuffer, 0, n) 'read data from the buffer
            Threading.Monitor.Enter(rcvQlock) 'add to q
            rcvQ.AddRange(comBuffer)
            Threading.Monitor.Exit(rcvQlock)
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            If rcvQ.Count >= 48 Then
                Threading.Monitor.Enter(rcvQlock)
                Dim buf() As Byte = rcvQ.GetRange(0, 48).ToArray()
                rcvQ.RemoveRange(0, 48)
                Threading.Monitor.Exit(rcvQlock)
                'at this point buf has 48 bytes
            End If
        End Sub
    
        Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
            Timer1.Interval = 1000 'one second
            Timer1.Start()
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Junior Member dragonguy's Avatar
    Join Date
    Nov 2012
    Posts
    25

    Re: Serial Port receiving hex data.

    can i check the combuffer(0)=98 then start store 48byte. so i can know i store the right 48byte hex code. i exactly wanna store the right position of the hex
    code. Start with98 then 67 00 27 27 00 00 00 00 80 00 80 00 00 00 00 00 00 00 00 00 00 00 00 98 68 00 27 27 00 00 00 00 80 00 02 00 00 00 00 00 00 00 00 00 00 00 00

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Serial Port receiving hex data.

    Quote Originally Posted by dragonguy View Post
    can i check the combuffer(0)=98 then start store 48byte. so i can know i store the right 48byte hex code. i exactly wanna store the right position of the hex
    code. Start with98 then 67 00 27 27 00 00 00 00 80 00 80 00 00 00 00 00 00 00 00 00 00 00 00 98 68 00 27 27 00 00 00 00 80 00 02 00 00 00 00 00 00 00 00 00 00 00 00
    Yes you can. At the beginning of the timer, before you check for 48 bytes, check to see if the first byte in the list is 98. If it isn't remove it.

    Code:
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Dim strt As Integer = rcvQ.IndexOf(98)
            If strt < 0 Then
                'no 98
            ElseIf strt > 0 Then
                Threading.Monitor.Enter(rcvQlock)
                rcvQ.RemoveRange(0, strt)
                Threading.Monitor.Exit(rcvQlock)
                strt = 0
            End If
            If rcvQ.Count >= 48 AndAlso strt = 0 Then
                Threading.Monitor.Enter(rcvQlock)
                Dim buf() As Byte = rcvQ.GetRange(0, 48).ToArray()
                rcvQ.RemoveRange(0, 48)
                Threading.Monitor.Exit(rcvQlock)
                'at this point buf has 48 bytes
            End If
        End Sub
    Last edited by dbasnett; Nov 15th, 2012 at 09:04 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Junior Member dragonguy's Avatar
    Join Date
    Nov 2012
    Posts
    25

    Re: Serial Port receiving hex data.

    Thanks a lot!! it usefull. already solve my problem.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Serial Port receiving hex data.

    Can you show us how you resolved it?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Junior Member dragonguy's Avatar
    Join Date
    Nov 2012
    Posts
    25

    Re: [RESOLVED] Serial Port receiving hex data.

    i'm just modified little bit only. here is the code:

    Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    Dim n As Integer = SerialPort1.BytesToRead 'find number of bytes in buf
    Dim comBuffer(n - 1) As Byte
    UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
    SerialPort1.Read(comBuffer, 0, n) 'read data from the buffer
    Threading.Monitor.Enter(rcvQlock) 'add to q
    rcvQ.AddRange(comBuffer)
    Threading.Monitor.Exit(rcvQlock)
    Me.Invoke(UpdateFormDelegate1)
    End Sub

    Private Sub UpdateDisplay()
    Dim strt As Integer = rcvQ.IndexOf(152)

    tmr_status.Enabled = Enabled
    tmr_status.Start()
    status.BackColor = Color.Blue


    If strt < 0 Then
    'no 98
    ElseIf strt > 0 Then
    Threading.Monitor.Enter(rcvQlock)
    rcvQ.RemoveRange(0, strt)
    Threading.Monitor.Exit(rcvQlock)
    strt = 0
    End If
    If rcvQ.Count >= 23 AndAlso strt = 0 Then
    Threading.Monitor.Enter(rcvQlock)
    Dim buf() As Byte = rcvQ.GetRange(0, 23).ToArray()
    rcvQ.RemoveRange(0, 23)
    Threading.Monitor.Exit(rcvQlock)
    'at this point buf has 23 bytes
    TextBox1.Text = buf(0)
    TextBox2.Text = buf(9)
    TextBox3.Text = buf(10)
    TextBox4.Text = buf(11)
    ..........'do what i want to do
    end sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width