Announcement

Collapse
No announcement yet.

Script for 2 Way Control over RS232

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

    Script for 2 Way Control over RS232

    Hi all,

    I wonder if this has been done or any help would be appreciated. I want to control my z-wave devices sitting in HS3 via a rs232 link to an HVAC controller.

    HS3 will need to open the com port and keep it open listening until it gets an ASCII string like LITEZ120050\x0d then action it and go back to listening for and others but i also need feedback so if anyone used HS app to turn the lights on etc then HS3 will need to send out something like the above.

    The ASCII string broken down is LITExxxyyy\x0d xxx=hs3 ref yyy= value for light to go to.

    #2
    If you look in the help file there are a number of scripting calls for serial ports, basically you can call hs.opencomport and then when it receives data with a specified termination string it will run a script and pass the received data in. You can then pick the data out and do with it what you wish, sending data is much the same with hs.sendtocomport. There is quite a few scripts out there if you search for hs.opencomport or similar.

    Comment


      #3
      Thanks for your reply, i have put opencomport in my startup.vb so this is working ok.

      Am i going down the right sort of line with the below?


      Sub Main(ByVal Parm As Object)

      Dim rxref As Integer
      Dim rxval As Integer
      Dim data As String

      Do
      data = hs.GetComPortData()
      rxref = Mid(data, 5, 3)
      rxval = Mid(data, 8, 3)
      ' set the value of device whose reference ID is 1234 to 60.54
      hs.SetDeviceValueByRef(rxref, rxval, True)
      data = ""

      Loop
      End Sub

      Comment


        #4
        If you have a termination string (like Cr/Lf) then I would highly recommend using the hs.opencomportterm (my fault for not mentioning this, it changed in HS3). What then happens is HS does the leg work in the background and gathers the data, when it reaches the string it runs a script and also passes the data into that script by parameter.

        GetComPortData is used when you don't have the termination string and you would have to run this script frequently to determine whether or not there is data to be read in the buffer. I would personally avoid this if at all possible, you might end up getting partial strings and the like.

        Then in the script I would do very much what you are looking at but personally I would look at ensuring that the string was the right length before you started to do mid/substrings - reason being if you only get back a bit of the data for whatever reason you will get errors.

        Comment


          #5
          Thanks again for the info i will change my com port to that, if i get rid of getcomportdata, how do i then get the string received in to the script?

          what can i use to send the data back out when a device status is changed?

          really appreciate your help

          Comment


            #6
            i have changed my com port to this

            Dim e
            e = hs.opencomportterm(5, "19200,n,8,1", 1, "mainprogram.vb", "main")
            If e <> "" Then
            hs.writelog("Error Opening Interface", e)
            Else
            hs.writelog("COM 5 ", "Interface Setup Complete")
            End If

            Comment


              #7
              The data will be passed in via the parameter so what you can try is something like this instead;

              Sub Main(ByVal Parm As String)

              hs.writelog("", "Serial Data: " & Parm)

              The latter is probably best if you explore the RegisterStatusChangeCB function, if you wish you can have a script run in HS when ANY device changes status. You can then in the script determine first whether or not you are interested in the device and then if you are you can use the hs.sendtocomport to send data back to the port.

              It can get a bit of a mess doing two way comms by script but by no means impossible, plugins whilst a steeper learning curve can sometimes allow you a greater deal of control.

              Comment


                #8
                right i am mostly there on getting the script correct for send status out. please see below

                Sub StatusChangeCB(ByVal Parm As Object())
                If Parm Is Nothing Then Exit Sub
                If Parm.Length < 5 Then Exit Sub
                Dim Code As String = ""
                Dim Address As String = ""
                Dim OldVal As Double
                Dim NewVal As Double
                Dim Ref As Integer
                Try
                Code = Parm(0).ToString
                Address = Parm(1).ToString
                NewVal = Parm(2)
                OldVal = Parm(3)
                Ref = Parm(4)
                Catch ex As Exception
                hs.WriteLog("senddata.VB", "Error, Exception parsing Parm: " & ex.Message)
                Exit Sub
                End Try
                hs.SendToComPort(2, ref & newval & chr(13) & chr(10))

                End Sub

                This works fine but i think it keeps running, see homeseer log even when no devices are changing.

                Aug-23 17:35:59 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:59 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:58 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:58 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:58 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:58 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:58 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:58 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:58 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:58 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:57 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:57 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:57 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:57 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:28 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:28 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:28 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:28 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:28 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:28 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:27 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:27 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:27 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:27 Event Event Trigger "CRESTRON update"
                Aug-23 17:35:27 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:35:27 Event Event Trigger "CRESTRON update"
                Aug-23 17:34:58 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:34:58 Event Event Trigger "CRESTRON update"
                Aug-23 17:34:58 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:34:58 Event Event Trigger "CRESTRON update"
                Aug-23 17:34:58 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:34:58 Event Event Trigger "CRESTRON update"
                Aug-23 17:34:57 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:34:57 Event Event Trigger "CRESTRON update"
                Aug-23 17:34:57 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:34:57 Event Event Trigger "CRESTRON update"
                Aug-23 17:34:57 Event Running script in background: C:/Program Files (x86)/HomeSeer HS3/scripts/change.vb
                Aug-23 17:34:57 Event Event Trigger "CRESTRON update"


                Also i want to always make the ref 3 digits so i can use 000-999 and the same for status 3 digits 000-255. I guess i can put some if's in to add the extra zero's this is my overall problem to using the built in ascii interface as it changes the size of its messages. Do you have a suggestion on this?

                Comment


                  #9
                  I have sorted the formatting so just the repeating and receiving in to hs3

                  Comment


                    #10
                    Thanks for the help in getting this far

                    I have the sending on the 232 working with out any issues now although i sometimes see this warning.
                    Aug-23 18:19:58 Warning In CommitDeviceStatusNow-Inner Loop at line: Collection was modified; enumeration operation may not execute.

                    I need to work out how to the a message that is sent in to HS3, i have no idea what todo here, how do i make HS3 always listening for a message?

                    i have this script in the script folder but it does work and when does it know to run?

                    Sub Main(ByVal Parm As Object)

                    Dim rxref As Integer
                    Dim rxval As Integer
                    Dim data As String
                    hs.writelog("", "Serial Data: " & Parm)
                    Do
                    rxref = Mid(data, 5, 3)
                    rxval = Mid(data, 8, 3)
                    ' set the value of device whose reference ID is 1234 to 60.54
                    hs.SetDeviceValueByRef(rxref, rxval, True)
                    data = ""

                    Loop
                    End Sub

                    i know you said to get rid of the mids which i will do once i have got basics working.

                    Comment


                      #11
                      Originally posted by Matt Powell View Post
                      Thanks for the help in getting this far

                      I have the sending on the 232 working with out any issues now although i sometimes see this warning.
                      Aug-23 18:19:58 Warning In CommitDeviceStatusNow-Inner Loop at line: Collection was modified; enumeration operation may not execute.

                      I need to work out how to the a message that is sent in to HS3, i have no idea what todo here, how do i make HS3 always listening for a message?

                      i have this script in the script folder but it does work and when does it know to run?

                      Sub Main(ByVal Parm As Object)

                      Dim rxref As Integer
                      Dim rxval As Integer
                      Dim data As String
                      hs.writelog("", "Serial Data: " & Parm)
                      Do
                      rxref = Mid(data, 5, 3)
                      rxval = Mid(data, 8, 3)
                      ' set the value of device whose reference ID is 1234 to 60.54
                      hs.SetDeviceValueByRef(rxref, rxval, True)
                      data = ""

                      Loop
                      End Sub

                      i know you said to get rid of the mids which i will do once i have got basics working.
                      When you call hs.opencomportterm in your startup.vb file (or you can call it from wherever but you only need to call it once) then HomeSeer will monitor the serial port for data. When the termination character is received it will then pass the data to the script for you to parse the data. So in answer to know when to run then it is HomeSeer that does this for you.

                      Comment


                        #12
                        Ok, so from what your saying if i put the vb and the sub in this string

                        hs.OpenComPortTerm(2, "19200,N,8,1", 0, "rx.vb", "main")


                        Then homeseer should run rx.vb when it receives data on the comport

                        I think my next hurdle is going to be CAPI as i believe from what i am reading i not going to be able to use hs.setdevicevaluebyref

                        Comment


                          #13
                          so i have this in my startup.vb

                          e = hs.OpenComPortTerm(2, "19200,N,8,1", 0, "rx.vb", "main", chr(13))

                          then my rx.vb

                          Sub main(ByVal data)
                          hs.writelog("", "Serial Data: " & data)
                          End Sub


                          this is what i am sending via docklight at the moment

                          24/08/2017 09:35:14.858 [TX] - LITE067099<CR>

                          And i get nothing in the log? do you see a problem?

                          My com port is open as i get my log message saying so and the tx.vb is working a treat

                          Comment


                            #14
                            Originally posted by Matt Powell View Post
                            Ok, so from what your saying if i put the vb and the sub in this string

                            hs.OpenComPortTerm(2, "19200,N,8,1", 0, "rx.vb", "main")


                            Then homeseer should run rx.vb when it receives data on the comport

                            I think my next hurdle is going to be CAPI as i believe from what i am reading i not going to be able to use hs.setdevicevaluebyref
                            Try changing 0 to 1 in that line and see how you go, the default termination character is CrLf - is this what your unit is sending?

                            Comment


                              #15
                              Afternoon,

                              I am trying to send messages with just cr and messages with crlf none seem to get through

                              this is know the opening string changing the 0 to a 1

                              e = hs.OpenComPortTerm(2, "19200,N,8,1", 1, "rx.vb", "main", chr(13))

                              Any thoughts?

                              Comment

                              Working...
                              X