Announcement

Collapse
No announcement yet.

Reading Com Port Error

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

    Reading Com Port Error

    Recently a smart electricity meter is installed in my house. This meter is connected to my HomeSeer pc through serial to USB. I can read the data coming in with Putty.

    The data looks like this. Every 10 sec this data is updated.

    Code:
    /KFM5KAIFA-METER
    
    1-3:0.2.8(40)
    0-0:1.0.0(140207214844W)
    0-0:96.1.1(4530303039313030303031363435353133)
    1-0:1.8.1(000029.567*kWh)
    1-0:1.8.2(000025.374*kWh)
    1-0:2.8.1(000000.000*kWh)
    1-0:2.8.2(000000.000*kWh)
    0-0:96.14.0(0002)
    1-0:1.7.0(01.645*kW)
    1-0:2.7.0(00.000*kW)
    0-0:17.0.0(999.9*kW)
    0-0:96.3.10(1)
    0-0:96.7.21(00007)
    0-0:96.7.9(00005)
    1-0:99.97.0(1)(0-0:96.7.19)(000101000001W)(2147483647*s)
    1-0:32.32.0(00000)
    1-0:32.36.0(00000)
    0-0:96.13.1()
    0-0:96.13.0()
    1-0:31.7.0(007*A)
    1-0:21.7.0(01.651*kW)
    1-0:22.7.0(00.000*kW)
    0-1:24.1.0(003)
    0-1:96.1.0(4730303136353631323034323832383133)
    0-1:24.2.1(140207210000W)(00026.831*m3)
    0-1:24.4.0(1)
    !D6BF
    I trying to make this data available in Homeseer. The script runs fine until after the first line is read. Then I get the following error in the log: Length cannot be less then zero (see attached). The script I use is:

    Code:
    ' place in Homeseer Startup.txt and replace X with com-port #
    
    ' hs.closecomport X 
    '  e=hs.OpenComPort(X,"115200,N,8,1",1,"P1.vb","P1_event", "!")
    '  if e <> "" then 
    '    hs.writelog "P1", "Setup error " & e
    '  else 
    '    hs.writelog "P1", "Setup complete"
    '  end if 
    
    ' Place the following in the Shutdown script
    ' e=hs.CloseComPort("X")
    ' if e<> "" then
    '   hs.writelog "Error closing COM1",e
    ' else
    '   hs.writelog "COM1", "Port was closed"
    ' end if
    
    'Create these virtual devices in Homeseer
    Const CURRENT_POWER_USAGE = "E1"
    Const TOTAL_POWER_USAGE_T1 = "E2"
    Const TOTAL_POWER_USAGE_T2 = "E3"
    Const TARIFF_INDICATOR = "E4"
    Const TOTAL_GAS_USAGE = "E5"
    Const GAS_USAGE_UPDATE = "E6"
    
    Dim v1, v2, device_value, device_string, y, m, d, h
    
    Const DEBUG_LOG = True 'set to False when running without error
    
    Sub P1_event(data)
    
        If DEBUG_LOG Then
            hs.writelog("P1", "Data: " & CStr(data))
        End If
    
        ' Current Power Use
        ' 1-0:1.7.0(0001.05*kW)
        If left(data, 10) = "1-0:1.7.0(" Then
            v1 = Split(data, "(")
            v2 = Split(v1(1), "*")
            device_value = CDbl(replace(v2(0), ".", ",")) * 1000
            'device_string = device_value & "W" 
            If DEBUG_LOG Then
                hs.writelog("P1", "Current Power Use: " & CStr(device_value) & " Watt")
            End If
            hs.SetDeviceString(CURRENT_POWER_USAGE, device_string)
            hs.SetDeviceValue(CURRENT_POWER_USAGE, device_value)
            hs.setDeviceLastChange("E1", now)
            
        End If
    
    
    End Sub
    Can somebody point me in the right direction on how to solve this or where to start looking for an answer?
    Attached Files

    #2
    Are you sure this error is coming from this script?
    💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

    Comment


      #3
      If I stop the script the error disappears, so I am sure it comes from this script.

      Comment


        #4
        Hum. I'm not sure where the Length test is coming from but you could try adding an "On Error Resume Next" to the loop to see if it continues. You may also have to add some debug statement to the script to narrow in on where the error is being generated.
        💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

        Comment


          #5
          I brought the script down to a minimum:

          Const DEBUG_LOG = True 'set to False when running without error

          Code:
          Sub P1_event(data)
          
              If DEBUG_LOG Then
                  hs.writelog("P1", "Data: " & CStr(data))
              End If
          
              If left(data, 1) = "/" Then
                  hs.SetDeviceString("E1", data)
                  hs.SetDeviceLastChange("E1", now)
              End If
          The result is attached. This first line of data can be processed. The next line however seems to create the problem.
          Attached Files
          Last edited by Romac; February 10, 2014, 03:35 PM.

          Comment


            #6
            Did you try adding the On error resume next in the first debug statement?
            💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

            Comment


              #7
              Did it like to this, the result is the same.

              Code:
              Const DEBUG_LOG = True 'set to False when running without error
              
              Sub P1_event(data)
              
                  If DEBUG_LOG Then
                      hs.writelog("P1", "Data: " & CStr(data))
                      On Error Resume Next
                  End If
              
                  If left(data, 1) = "/" Then
                      hs.SetDeviceString("E1", data)
                      hs.SetDeviceLastChange("E1", now)
                  End If

              Comment


                #8
                Try moving the on error above the debug statement.
                💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

                Comment


                  #9
                  Not much luck yet.. The result is the same. Could the "blank" line contain any (hidden) codes that throw of the script?

                  Code:
                  Const DEBUG_LOG = True 'set to False when running without error
                  
                  Sub P1_event(data)
                  
                      On Error Resume Next
                  
                      If DEBUG_LOG Then
                          hs.writelog("P1", "Data: " & CStr(data))
                      End If
                  
                      If left(data, 1) = "/" Then
                          hs.SetDeviceString("E1", data)
                          hs.SetDeviceLastChange("E1", now)
                      End If

                  Comment


                    #10
                    Possibly. Try putting the On error here:



                    If DEBUG_LOG Then
                    On Error Resume Next
                    hs.writelog("P1", "Data: " & CStr(data))
                    End If

                    If left(data, 1) = "/" Then
                    hs.SetDeviceString("E1", data)
                    hs.SetDeviceLastChange("E1", now)
                    End If
                    💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

                    Comment


                      #11
                      What com port number are you trying to open? What file extension do you have on this file? Please show the code you are using to open the serial port.
                      💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

                      Comment


                        #12
                        Originally posted by Romac View Post
                        Not much luck yet.. The result is the same. Could the "blank" line contain any (hidden) codes that throw of the script?

                        Code:
                        Const DEBUG_LOG = True 'set to False when running without error
                        
                        Sub P1_event(data)
                        
                            On Error Resume Next
                        
                            If DEBUG_LOG Then
                                hs.writelog("P1", "Data: " & CStr(data))
                            End If
                        
                            If left(data, 1) = "/" Then
                                hs.SetDeviceString("E1", data)
                                hs.SetDeviceLastChange("E1", now)
                            End If
                        Suggest you first test whether the length of the data is >= size of the substring you are trying to compare it with, which in your case = 10. Never used the function "left" before but had similar issue using "mid" on a string smaller than my mid function and changed it to a "Indexof" <> -1 check

                        Dirk

                        Comment


                          #13
                          Originally posted by Rupp View Post
                          Possibly. Try putting the On error here:



                          If DEBUG_LOG Then
                          On Error Resume Next
                          hs.writelog("P1", "Data: " & CStr(data))
                          End If

                          If left(data, 1) = "/" Then
                          hs.SetDeviceString("E1", data)
                          hs.SetDeviceLastChange("E1", now)
                          End If
                          Thanks for the idea, but the result is the same.

                          Comment


                            #14
                            Originally posted by Rupp View Post
                            What com port number are you trying to open? What file extension do you have on this file? Please show the code you are using to open the serial port.
                            I have this in the startup script:
                            Code:
                            hs.closecomport 4 
                            	e=hs.OpenComPort(4,"115200,N,8,1",1,"P1.vb","P1_event")
                            	if e <> "" then 
                            	    hs.writelog "P1", "Setup error " & e
                            	  else 
                            	    hs.writelog "P1", "Setup complete using Comport 4"
                            	end i
                            f

                            Comment


                              #15
                              Originally posted by dcorsus View Post
                              Suggest you first test whether the length of the data is >= size of the substring you are trying to compare it with, which in your case = 10. Never used the function "left" before but had similar issue using "mid" on a string smaller than my mid function and changed it to a "Indexof" <> -1 check

                              Dirk
                              Thanks I will try this later today.

                              Comment

                              Working...
                              X