Announcement

Collapse
No announcement yet.

Is it wise to leave COM ports open in scripts

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

    Is it wise to leave COM ports open in scripts

    Hi,

    I have a lighting system that a customer wants to integrate with Homeseer. There is no plug-in for the lighting system. It's called Wattstopper by Legrand. It does have a serial module and I've got a list of the commands for. Its' using ASCII. My plan is to use scripts to send and receive data to the Wattstopper serial module. My question is is it wise to leave the COM port open at all times when not sending a command or receiving a response. Also, there is a command for requesting the status of a giving light. To get a "real-time" like feedback it is wise to keep sending the command to request light status every second or two? By doing this will I crash HS3?

    Thanks

    #2
    I have 3 devices connected via serial and I modified HS startup script to open all 3 ports on startup and they remain open all the time. Been like this for several years with no issue. Before this I had issues sometimes that I would get error saying port is already in use by another device. This has solved it. I don't see any downside to doing this, someone else might.

    Comment


      #3
      Originally posted by freeon View Post
      I have 3 devices connected via serial and I modified HS startup script to open all 3 ports on startup and they remain open all the time. Been like this for several years with no issue. . . . I don't see any downside to doing this. . .
      I do the same. No issues at all. I would definitely recommend you open the port with your Startup script and leave it open as long is HS is running.
      Mike____________________________________________________________ __________________
      HS3 Pro Edition 3.0.0.548, NUC i3

      HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

      Comment


        #4
        Thanks you guys!

        I will do the same

        Comment


          #5
          How do I define which port to open in the startup script?

          Is it COMPort.PortName = "COM2"? I running HS3 on a Windows PC. If that matters.

          Also, in the event that two commands gets sent through the serial port at the same time from two different scripts what would happen?

          Is it wise to call for the com port to open if it's "suppose to open" already. How do I know if the port is open or not?

          Comment


            #6
            Here some info how to do it in a VB script
            Code:
               Dim e As String = ""
                'the third hs.OpenComPortTerm parameter you specify either a 1 or a 0 to 
                'return data either in raw or string mode
            
                '0 = raw mode
                'In this mode, each character that is received on the COM port causes 
                'the specified script and function to be called.  It is up to the called 
                'function to callGetComPortData to actually get the characters.
            
                '1 = strings mode
                'This mode buffers up characters until a terminator is received.  
                'At this point the specified script and function are called with the data.  
                'This mode makes it easy to deal with devices that send text data terminated 
                'with known characters.  To specify the terminator characters, 
                'see the term parameter description below.  
                'If you do not specify a terminator, the default terminator of 
                'carriage return and line-feed (CrLf) are used.
            
                e = hs.OpenComPortTerm(SERIALPORTNUMBER, CONNECTIONSTRING, 1, strActionScriptName, "Handle_Event", VbCrLf)
            - 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


              #7
              Originally posted by huntley1 View Post
              How do I define which port to open in the startup script?
              In the HS Help file, under Scripting, Computer, Serial Port Communication, see instructions for "OpenComPort". The port number is the first term in the command.
              Also, in the event that two commands gets sent through the serial port at the same time from two different scripts what would happen?
              I'm not sure what you mean. Can you describe how you envision this happening? What is the structure of the data stream you want to receive?
              Is it wise to call for the com port to open if it's "suppose to open" already. How do I know if the port is open or not?
              The port has to be explicitly 'opened' by a process. Do you expect some other process to open the port besides HS? Under normal circumstances only one process can have access to the the port at one time. Attempting to open a port that is already open in another process will result in an error, and there will be a message in the log.
              Mike____________________________________________________________ __________________
              HS3 Pro Edition 3.0.0.548, NUC i3

              HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

              Comment


                #8
                I'm not sure what you mean. Can you describe how you envision this happening? What is the structure of the data stream you want to receive?
                My thoughts is to have two scripts. One sending commands. For example, setting light 1 to 50%. The other is to send a command to receive the status of each light and wait for the responses. My plan is to have the second script execute or loop every second to achieve a "real-time" like feedback status of the lights. In the event a command is sent to set the light and recieve the status of the lights at the same, what would happen? Would I have you disable or stop the loop of the second script when trying to set/control a light? Am I over thinking this?

                The port has to be explicitly 'opened' by a process. Do you expect some other process to open the port besides HS? Under normal circumstances only one process can have access to the the port at one time. Attempting to open a port that is already open in another process will result in an error, and there will be a message in the log.
                No, the port will not be used my another device or program. I was planning to open the port in the startup script, but also open it in the the lighting scripts. Just to make sure that it is opened. Or is the a check that I can do in my lighting script to make sure the port is open before trying to send commands?

                Comment


                  #9
                  Originally posted by huntley1 View Post
                  My thoughts is to have two scripts. One sending commands. For example, setting light 1 to 50%. The other is to send a command to receive the status of each light and wait for the responses. My plan is to have the second script execute or loop every second
                  It might be prudent to pause your recurring query when executing a command. One way to do that would be to turn on a virtual device before sending a command, then turn if off after the command is sent, and use the state of the device as a condition on the recurring event to query status. (Getting status every second may be overkill. It will also depend on the response time of your external device to your query.)
                  I was planning to open the port in the startup script, but also open it in the the lighting scripts. Just to make sure that it is opened.
                  In my experience, once HS opens the port it remains open reliably.
                  Mike____________________________________________________________ __________________
                  HS3 Pro Edition 3.0.0.548, NUC i3

                  HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

                  Comment


                    #10
                    Thanks for all your help!

                    So basically I opened the COM port in the startup script as suggest. I have a script that listens for any response from the device. If the response is what I'm looking for I parse the string to pickout the data I need.

                    Instead of polling for a response every second, my script only executes when the device sends a command. It works pretty slick

                    Comment


                      #11
                      Originally posted by huntley1 View Post
                      Instead of polling for a response every second, my script only executes when the device sends a command. It works pretty slick
                      That's great. From your earlier posts I assumed you had to initiate the data send from your device with a query from HS. Since you don't, waiting for the data to arrive and then parsing it is definitely the way to go. That's how I handle my serial link as well.

                      Does your device send a termination string? If it does, using 'hs.OpenComPortTerm' and 'String mode' (1) makes the process even easier.
                      Mike____________________________________________________________ __________________
                      HS3 Pro Edition 3.0.0.548, NUC i3

                      HW: Stargate | NX8e | CAV6.6 | Squeezebox | PCS | WGL 800RF | RFXCOM | Vantage Pro | Green-Eye | Edgeport/8 | Way2Call | Ecobee3 | EtherRain | Ubiquiti

                      Comment


                        #12
                        From your earlier posts I assumed you had to initiate the data send from your device with a query from HS.*
                        Yes, in my startup script, after I open the port I send a command to start the feedback

                        Does your device send a termination string? If it does, using 'hs.OpenComPortTerm' and 'String mode' (1) makes the process even easier.
                        Yes, the device sends a CR, so I am using the "hs.OpenComPortTerm" command. This opens a whole new world of possibilities for me with devices that don't have a plug-in
                        Last edited by huntley1; June 22, 2018, 08:58 AM.

                        Comment

                        Working...
                        X