Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to process data from serial device

How to process data from serial device

Scheduled Pinned Locked Moved Visual Basic
tutorialquestiondatabaseiotdata-structures
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fu Manchu
    wrote on last edited by
    #1

    Hi all, i have been thinking on this problem for a while now - How do i process data from a serial device? i have a serial connetion to a fire alarm system that sends ascii characters to the computer, i have esablished comms to the device, and have a method that is fired on every byte of data received. Problem is the device sends the data in three byte wide blocks, so for example status,channel and then the checksum which is (status+channel). i have tried various method of circular buffers and pop-queues to try and process this but with no success! for example the device would send ....14 then 1 then 15. 14 01 15 - this would represent fire on sensor 1 i am trying to write a method that every time a byte is received it stores it to a buffer and then processes it to try and put them in seperate variables. example... abnormality = buffer(x) channel= buffer(x+1) checksum = buffer(x+2) Here is some rough example code but its not working correctly, does anyone have any ideas? Thanks Dim queue(6) As Integer Dim startPos As Integer = 2 Private Sub ProcessData(ByVal data As Integer) Dim test As Boolean = False Dim checksum As Integer, channel As Integer, abnormality As Integer Dim alphaBuffer As Integer, betaBuffer As Integer, gammaBuffer As Integer Dim mytxt As String 'step1 fill queue at index 2 - starting point queue(startPos + index) = data index = index + 1 If index = 3 Then 'step2 test bytes alphaBuffer = queue(2) betaBuffer = queue(3) gammaBuffer = queue(4) If gammaBuffer = CalculateChecksum(alphaBuffer + betaBuffer) Then abnormality = alphaBuffer channel = betaBuffer checksum = gammaBuffer Me.Label2.Text = abnormality & " " & channel & " " & checksum mytxt = Me.Label2.Text & " " & Me.ListBox1.Items.Count Me.ListBox1.Items.Add(mytxt) 'LeftShift(queue) Else LeftShift(queue) startPos = 1 End If End If If index = 4 Then 'step2 test bytes alphaBuffer = queue(2) betaBuffer = queue(3) gammaBuffer = queue(4) If gammaBuffer = CalculateChecksum(alphaBuffer + betaBuffer) Then abnormality = alphaBuffer

    L 1 Reply Last reply
    0
    • F Fu Manchu

      Hi all, i have been thinking on this problem for a while now - How do i process data from a serial device? i have a serial connetion to a fire alarm system that sends ascii characters to the computer, i have esablished comms to the device, and have a method that is fired on every byte of data received. Problem is the device sends the data in three byte wide blocks, so for example status,channel and then the checksum which is (status+channel). i have tried various method of circular buffers and pop-queues to try and process this but with no success! for example the device would send ....14 then 1 then 15. 14 01 15 - this would represent fire on sensor 1 i am trying to write a method that every time a byte is received it stores it to a buffer and then processes it to try and put them in seperate variables. example... abnormality = buffer(x) channel= buffer(x+1) checksum = buffer(x+2) Here is some rough example code but its not working correctly, does anyone have any ideas? Thanks Dim queue(6) As Integer Dim startPos As Integer = 2 Private Sub ProcessData(ByVal data As Integer) Dim test As Boolean = False Dim checksum As Integer, channel As Integer, abnormality As Integer Dim alphaBuffer As Integer, betaBuffer As Integer, gammaBuffer As Integer Dim mytxt As String 'step1 fill queue at index 2 - starting point queue(startPos + index) = data index = index + 1 If index = 3 Then 'step2 test bytes alphaBuffer = queue(2) betaBuffer = queue(3) gammaBuffer = queue(4) If gammaBuffer = CalculateChecksum(alphaBuffer + betaBuffer) Then abnormality = alphaBuffer channel = betaBuffer checksum = gammaBuffer Me.Label2.Text = abnormality & " " & channel & " " & checksum mytxt = Me.Label2.Text & " " & Me.ListBox1.Items.Count Me.ListBox1.Items.Add(mytxt) 'LeftShift(queue) Else LeftShift(queue) startPos = 1 End If End If If index = 4 Then 'step2 test bytes alphaBuffer = queue(2) betaBuffer = queue(3) gammaBuffer = queue(4) If gammaBuffer = CalculateChecksum(alphaBuffer + betaBuffer) Then abnormality = alphaBuffer

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I need some more info before I can propose an alternative approach. Hence: 1. Is there a minimal time gap between consecutive messages ? 2. How many bytes do you expect per second ? 3. What is the baud rate ? 4. "but its not working correctly" please explain ? :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      F 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I need some more info before I can propose an alternative approach. Hence: 1. Is there a minimal time gap between consecutive messages ? 2. How many bytes do you expect per second ? 3. What is the baud rate ? 4. "but its not working correctly" please explain ? :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        F Offline
        F Offline
        Fu Manchu
        wrote on last edited by
        #3

        thanks for your reply 1. Is there a minimal time gap between consecutive messages ? 2. How many bytes do you expect per second ? The device responds to ENQ (ascii char 5), when its first switched on it sends the same three bytes three times and then shutdown again or around 6 seconds if no ACK signal is sent, if the device is sent ACK (ascii char 6) then it starts to sent data. so it sends status byte - send ACK back it then sends channel byte and so on. 3. What is the baud rate ? maximum 4800, 2 stop bits and odd parity 4. "but its not working correctly" please explain ? I meant my code, when i have received the data...status,channel,checksum - how do ensure the data is processed correctly in the correct order and not out of sync. for example sometimes when comms is etablished, if the device is already sending data, the first character at this point may be the channel rather than the status the processing method has to weed this out and sync to the correct byte. Thanks

        L 1 Reply Last reply
        0
        • F Fu Manchu

          thanks for your reply 1. Is there a minimal time gap between consecutive messages ? 2. How many bytes do you expect per second ? The device responds to ENQ (ascii char 5), when its first switched on it sends the same three bytes three times and then shutdown again or around 6 seconds if no ACK signal is sent, if the device is sent ACK (ascii char 6) then it starts to sent data. so it sends status byte - send ACK back it then sends channel byte and so on. 3. What is the baud rate ? maximum 4800, 2 stop bits and odd parity 4. "but its not working correctly" please explain ? I meant my code, when i have received the data...status,channel,checksum - how do ensure the data is processed correctly in the correct order and not out of sync. for example sometimes when comms is etablished, if the device is already sending data, the first character at this point may be the channel rather than the status the processing method has to weed this out and sync to the correct byte. Thanks

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, thanks for the info. This seems to be a very nice device, it allows for very safe data transmission. These are my ideas now, they relate to the SerialPort class, not to your current code; I prefer the synchronous read mode, that is: some thread does all the communication, both sending and receiving, in lock step, there is no need to have asynchronous reads since you can be in charge of everything. SYNCHRONOUS COMM MODE 1. clear the serial input buffer (with DiscardInBuffer) 2. send command to start the communication 3. wait some time (say 1 sec) 4. read available bytes, no more than 9 are required; if they are not present, reject all (there is a Read that fills a byte buffer, I'll call it b) 5. send command to stop the communication DATA PROCESSING 1. check that 9 bytes obey xycabCABz pattern; i.e. make sure bytes 5to7 are same as 2to4; if not reject everything we will use xyc or yca or cab (normally x=a, y=b, z=c but we dont need that) 2. begin loop three times, with i=0/1/2 3. check b(i+2)=b(i)+b(i+1), if so goto step 6 4. next loop iteration (this means if for some reason a strange byte got in, ignore it) 5. we failed to find a valid checksum, reject everything 6. we got valid data in bytes i,i+1,i+2 IMPLEMENTATION IDEAS I would have: - a byte array b holding 9 bytes (it can be reused over and over) - a method doing the comm part, i.e. filling b - a method doing postprocessing, returning the index i of the first useful byte, or -1 to indicate rejection Hope I explained it well.

          Luc Pattyn [Forum Guidelines] [My Articles]


          this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


          F 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, thanks for the info. This seems to be a very nice device, it allows for very safe data transmission. These are my ideas now, they relate to the SerialPort class, not to your current code; I prefer the synchronous read mode, that is: some thread does all the communication, both sending and receiving, in lock step, there is no need to have asynchronous reads since you can be in charge of everything. SYNCHRONOUS COMM MODE 1. clear the serial input buffer (with DiscardInBuffer) 2. send command to start the communication 3. wait some time (say 1 sec) 4. read available bytes, no more than 9 are required; if they are not present, reject all (there is a Read that fills a byte buffer, I'll call it b) 5. send command to stop the communication DATA PROCESSING 1. check that 9 bytes obey xycabCABz pattern; i.e. make sure bytes 5to7 are same as 2to4; if not reject everything we will use xyc or yca or cab (normally x=a, y=b, z=c but we dont need that) 2. begin loop three times, with i=0/1/2 3. check b(i+2)=b(i)+b(i+1), if so goto step 6 4. next loop iteration (this means if for some reason a strange byte got in, ignore it) 5. we failed to find a valid checksum, reject everything 6. we got valid data in bytes i,i+1,i+2 IMPLEMENTATION IDEAS I would have: - a byte array b holding 9 bytes (it can be reused over and over) - a method doing the comm part, i.e. filling b - a method doing postprocessing, returning the index i of the first useful byte, or -1 to indicate rejection Hope I explained it well.

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            F Offline
            F Offline
            Fu Manchu
            wrote on last edited by
            #5

            Thanks for your advice, i will try and look into implementing your method. thanks again

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups