Announcement

Collapse
No announcement yet.

Script to parse data returned from serial port - help needed

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Script to parse data returned from serial port - help needed

    I am in need of assistance on how to parse data returned from serial port. I am using vb.net.

    Command sent is "?10"

    The response is:
    #>1100010000200710100100
    #>1200010000200710100100
    #>1300010000200710100100
    #>1400010000200710100100
    #>1500010000200710100100
    #>1600010000200710100100

    I get a response with 6 strings. In each string each 2 digits mean something. For example on the first response #>110001....the 11 means Amp1 Zone 1. The 00 means something, the 01 means something...etc.

    I need to parse all of this data into correct items for HS3. Any ideas?

    #2
    Substring() will be your friend.
    See for examples http://www.dotnetperls.com/substring-vbnet
    - Bram

    Send from my Commodore VIC-20

    Ashai_Rey____________________________________________________________ ________________
    HS3 Pro 3.0.0.534
    PIugins: ZMC audio | ZMC VR | ZMC IR | ZMC NDS | RFXcom | AZ scripts | Jon00 Scripts | BLBackup | FritzBox | Z-Wave | mcsMQTT | AK Ikea

    Comment


      #3
      Whenever I do something like that I test the length of the string (if you absolutely know it is fixed) first, then possibly check the starting two characters are the #> characters before I started to manipulate it.

      It is unlikely but just in case you only got a half string back from the device and then trying to get a substring failed, you can just use substring but you could also then check the component parts to make it valid. Like SubString(2,3) might be the amp zone you could then send that to a function to check if it is a valid zone (say 01 to 12 or whatever)...

      You don't have to error check and you can probably get away with it but it depends on how critical your data is and whether you could live with it if it came up with an unexpected error and how you deal with that error.

      Comment


        #4
        I will try substring().

        I think I have a different problem as well. I am trying to figure out how to put each line of the response into a buffer. I have tried several ways with no luck. Pls keep in mind that I am a complete novice programmer and just keep trying different things. Any help would be appreciated.

        Thanks,
        Chris

        Comment


          #5
          Please note that I am also a paste and pray kind of guy...

          This example might help you or just confuse you. Hope it helps
          PHP Code:
          Public Class Form1
              
          Private _SerialCommunicationThread As Thread
              
          Private _DataQueue As Queue Queue.Synchronized(New Queue())

              Private 
          Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandles MyBase.Load
                  Me
          ._SerialCommunicationThread = New Thread(AddressOf THREAD_SerialCommunication)
                  
          Me._SerialCommunicationThread.Priority ThreadPriority.AboveNormal
                  Me
          ._SerialCommunicationThread.Start()

                  
          TIMER_Main.Enabled True
              End Sub

              
          Private Sub THREAD_SerialCommunication()
                  Try
                      
          Dim SerialPort As New SerialPort("COM1"9600Parity.None8StopBits.One)
                      
          SerialPort.ReadTimeout 1000
                      SerialPort
          .Open()

                      
          Dim Buffersize As Integer 1024
                      Dim Buffer
          (Buffersize 1) As Byte
                      
          Do
                          
          Dim DataRead SerialPort.Read(Buffer0Buffer.Length)

                          
          Dim BufferCut(DataRead 1) As Byte
                          
          For As Integer 0 To BufferCut.Length 1
                              Buffer
          (i) = BufferCut(i)
                          
          Next

                          Me
          ._DataQueue.Enqueue(BufferCut)
                      
          Loop
                  
          Catch ex As Exception
                      MsgBox
          ("An error has occured:" vbCrLf vbCrLf ex.ToString)
                  
          End Try
              
          End Sub

              
          Private Sub TIMER_Main_Tick(ByVal sender As System.ObjectByVal e As System.EventArgsHandles TIMER_Main.Tick
                  
          While Me._DataQueue.Count 0
                      Dim Bytes 
          Me._DataQueue.Dequeue
                      Dim Text 
          Encoding.Default.GetString(Bytes)

                      
          RTF_Text.AppendText(Text)
                  
          End While
              
          End Sub
          End 
          Class 
          Don

          Comment


            #6
            Originally posted by Dr_Crash View Post
            I will try substring().

            I think I have a different problem as well. I am trying to figure out how to put each line of the response into a buffer. I have tried several ways with no luck. Pls keep in mind that I am a complete novice programmer and just keep trying different things. Any help would be appreciated.

            Thanks,
            Chris
            There may be no need for a buffer, as long as you are doing simple operations with the data and don't tie it up you might be able to get away with instantly manipulating the data.

            Comment


              #7
              mrhappy- I understand what you are saying. The problem is I don't exactly understand the response from the serial port. It is formatted exactly as the response was given in the first post. Is this considered to be streaming? In other words should I be thinking about processing data as if it were one string that looked like #>110001..... #>1200001... #>1300001...etc?

              Each line contains information about power status for that zone, volume, bass, treble, source...I want to use this information to set correct values in HStouch and certain device indicators.

              Comment


                #8
                Originally posted by Dr_Crash View Post
                mrhappy- I understand what you are saying. The problem is I don't exactly understand the response from the serial port. It is formatted exactly as the response was given in the first post. Is this considered to be streaming? In other words should I be thinking about processing data as if it were one string that looked like #>110001..... #>1200001... #>1300001...etc?

                Each line contains information about power status for that zone, volume, bass, treble, source...I want to use this information to set correct values in HStouch and certain device indicators.
                Some of this might need the documentation to be examined to say what is coming from the unit. I'm unsure what stage you are at in terms of getting this data into HomeSeer, if you have a script already running and these separate lines are coming into the HomeSeer log then it is likely that the strings are split by using the carriage return and line feed characters and that is running the callback script every time a new line of it applies (this is ideal because you can manipulate it in this script).

                If not then it is not necessarily the end of the world, if all of that does arrive in one block and it does not have anything between the sets of data then you can use the String.Split function and split the strings on the basis of lets say the # character, from there you can fill an array with the data and then work on it to update devices etc.

                Comment


                  #9
                  I'm unsure what stage you are at in terms of getting this data into HomeSeer, if you have a script already running and these separate lines are coming into the HomeSeer log then it is likely that the strings are split by using the carriage return and line feed characters and that is running the callback script every time a new line of it applies (this is ideal because you can manipulate it in this script).
                  I finally got the response to hit the log exactly as shown. I guess you are saying I can then process each line as stand alone and shouldn't have a problem.

                  I'll give it a shot. You guys have been a huge help thus far.

                  Comment


                    #10
                    Originally posted by Dr_Crash View Post
                    I finally got the response to hit the log exactly as shown. I guess you are saying I can then process each line as stand alone and shouldn't have a problem.

                    I'll give it a shot. You guys have been a huge help thus far.
                    This is what I think will work and be easier, when each line arrives then process it one at a time - if you did something that tied up the script (big loop/big database or something like that, your average setting of a few devices would be fine) I honestly don't know what would happen as to whether HS would queue up the data and run the script after it finished to go through the next line of data or it would not. I imagine you will be fine doing it one row at a time...

                    Comment


                      #11
                      Got it working.

                      Substring() was the way to go. It appears the data is processed line by line as you had suggested. Not all responses will be this form so I have applied an If Then statement that checks length of string before I do anything with it. Seems to work.

                      Now for the coding of setting devices values based on what comes back. My goal is to always query the amps and use the real values to set data shown in HSTouch. This way I know for sure that the events happened as expected.

                      I will see how it goes. If you have a better idea please share.

                      Thanks for the help.

                      Comment


                        #12
                        Interesting... I'm doing something similar with my Denon amp. I trap for a specific device, say 'Volume' and if the incoming string contains the substring that identifies itself as volume, I set the device string and value to that. If it is something non numeric, like what imput I set the string to that and the value to a 1. When Homeseer starts up I have a script that quieries the receiver for the corresponding values. In my case a 'XM?<CR>' asks the amp what's up with all the XM stuff, and populate the devices with the results.
                        Don

                        Comment


                          #13
                          Don,

                          I have pretty much the same plan. I replaced all Denon Amps with 2 Monoprice 6x6 amps. 12 zones independently controlled with 6 sources available. In the old installation I would request to turn on a device and status would change but I didn't always get result expected. For my new installation I plan to query the device for all information and set status accordingly.

                          Comment

                          Working...
                          X